Today, bloggers in the development of a Windows Phone app, want to implement the app background replacement, the idea is simple. Use the Applicationdatacontainer container to store my picture path, read the path each time I load the app to determine what background I use, and then change the value of the container when changing the background. The relevant code is as follows:
Applicationdatacontainer localsetting =ApplicationData.Current.LocalSettings;
Localsetting. values["Background"] = "ms-appx:///assets/images/background2.jpg";Button btn= Sender asButton; if(string. Equals (btn. Tag,"1") {localsetting. values["Background"] ="ms-appx:///assets/images/background1.jpg"; } Else if(string. Equals (btn. Tag,"2") {localsetting. values["Background"] ="ms-appx:///assets/images/background2.jpg"; } Else{localsetting. values["Background"] ="ms-appx:///assets/images/background3.jpg"; }
When loading the application, brush the background of the grid with the image corresponding to the container path, where the Contentpanel is a grid:
if(!localsetting. values["Background"]. Equals (NULL)) { stringStrimageuri = localsetting. values["Background"]. ToString (); if(!string. IsNullOrEmpty (Strimageuri)) {ImageBrush ImageBrush=NewImageBrush (); Imagebrush.imagesource=NewBitmapImage (NewUri (Strimageuri, Urikind.relativeorabsolute)); Contentpanel.background=ImageBrush; } }
It is important to note that my pictures are placed in the image folder below the assets folder in the project, and the values in my container initially are similar to this:
Localsetting. values["Background" "assets/images/background2.jpg";
Then you will get an error: The given System.Uri cannot is converted into a Windows.Foundation.Uri, now the URI definition seems to have been modified, specific reference: https:// Msdn.microsoft.com/zh-cn/library/windows/apps/windows.foundation.uri (v=vs.85). aspx
My solution is to add a ms-appx:///in front of the path, note that this is an absolute path, UriKind should be set to Urikind.relativeorabsolute or Urikind.absolute
UAP Development Error The given System.Uri cannot be converted into a Windows.Foundation.Uri (Windows phone background replacement)