Two days back I wrote Part 112- Windows Phone - Setting Lock Screen Background From App article. One of my friend pinged me in facebook asking how can we implement this feature in Windows Store app without using file picker. So I thought to write this article which will set the lock screen background image with the image in app itself.
Step 1: Open VS2012 and select New Project from File Menu.
Step 2: Select template type as Windows Store under Visual C#.
Step 3: Create two button like below inside Grid of MainPage.xaml.
On click of Lock Screen by URL will download the image from web and set it as lock screen background.
On click of Lock Screen by App Image button will use the image added in the app to set the lock screen background.
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <Button Margin="400,0,0,0" x:Name="btnLockScreenByURL" Content="Lock Screen by URL" Click="btnLockScreenByURL_Click_1"></Button> <Button Margin="600,0,0,0" x:Name="btnLockScreenByImage" Content="Lock Screen by App Image" Click="btnLockScreenByImage_Click_1"></Button> </Grid>
Step 4 Add Windows.Networking.BackgroundTransfer, Windows.Storage and Windows.System.UserProfile in MainPage.Xaml.cs.
using Windows.Networking.BackgroundTransfer; using Windows.Storage; using Windows.System.UserProfile;
Step 5: Create btnLockScreenByURL_Click_1 event handler to download the image and set it as lock screen background.
Before setting the lock screen background, the image needs to be downloaded in local folder or any other folder of your choice. StorageFolder class will be used to set the local folder using Windows.Storage.ApplicationData.Current.LocalFolder. CreateFileAsync method of StorageFolder object will be used to create the file asynchronously.
Once file is created CreateDownload method of BackgroundDownloader class will be used to download the file. CreateDownload method returns DownloadOperation object. StartAsync method of DownloadOperation object will start the download.
Once the file is downloaded, SetImageFileAsync method of static class LockScreen will be used to set the lock screen wallpaper.
private async void btnLockScreenByURL_Click_1(object sender, RoutedEventArgs e) { string imgURL = http://www.dotnetspeaks.com/Lockscreenurl.jpg; StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder; StorageFile file = await storageFolder.CreateFileAsync("myImage", CreationCollisionOption.GenerateUniqueName); BackgroundDownloader downloader = new BackgroundDownloader(); DownloadOperation d = downloader.CreateDownload(new Uri(imgURL), file); await d.StartAsync(); await LockScreen.SetImageFileAsync(file); }
Step 6: Add a image of size 1366 X 768 image in the project. I named the image file as LockScreen.jpg.
Step 7: Create btnLockScreenByImage_Click_1 method which will set lock screen background image using the image in the application.
In this case GetFileFromApplicationUriAsync method of StorageFile will be used to get the StorageFile from the app.
private async void btnLockScreenByImage_Click_1(object sender, RoutedEventArgs e) { string imgURL = "ms-appx:///LockScreen.jpg"; StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder; StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(imgURL)); await LockScreen.SetImageFileAsync(file); }
This ends the article of setting lock screen background for Windows Store app using the image in the app or downloading from web.
|