C# 在应用程序启动时自动保存和检索背景图像
在我的WindowsPhone7应用程序中,我创建了一个按钮,可以根据计数的数量改变回退。默认情况下,我有一个背景图像。我需要的是,如果用户更改了背景(使用上面的按钮),我希望该图像应该通过独立存储保存或记住。然后,当应用程序再次启动时,该特定图像(用户上次选择的)应该是我的应用程序的背景图像。现在的问题是,我无法检索图像(但我能够保存),并且不知道如何在应用程序启动时自动将该图像作为背景图像(不想使用任何按钮保存或检索,必须是自动的)。有人能帮我吗?非常感谢你的辛勤工作C# 在应用程序启动时自动保存和检索背景图像,c#,windows-phone-7,C#,Windows Phone 7,在我的WindowsPhone7应用程序中,我创建了一个按钮,可以根据计数的数量改变回退。默认情况下,我有一个背景图像。我需要的是,如果用户更改了背景(使用上面的按钮),我希望该图像应该通过独立存储保存或记住。然后,当应用程序再次启动时,该特定图像(用户上次选择的)应该是我的应用程序的背景图像。现在的问题是,我无法检索图像(但我能够保存),并且不知道如何在应用程序启动时自动将该图像作为背景图像(不想使用任何按钮保存或检索,必须是自动的)。有人能帮我吗?非常感谢你的辛勤工作 private voi
private void button1_Click(object sender, RoutedEventArgs e)
{
string imguri = "";
click_count = click_count % 4;
switch (click_count)
{
case 0: imguri = "Images/image4.png"; break;
case 1: imguri = "Images/image3.jpg"; break;
case 2: imguri = "Images/image2.png"; break;
case 3: imguri = "Images/image1.jpg"; break;
}
click_count++;
BitmapImage bitmapImage = new BitmapImage(new Uri(imguri, UriKind.Relative));
ImageBrush imageBrush = new ImageBrush();
imageBrush.ImageSource = bitmapImage;
var app = Application.Current as App;
this.LayoutRoot.Background = imageBrush;
app.appbrush = imageBrush;
app.backchanged = true;
string filename = "Images/app.jpg";
StreamResourceInfo sr = Application.GetResourceStream(new Uri(filename, UriKind.Relative));
bitmapImage.SetSource(sr.Stream);
WriteableBitmap wb = new WriteableBitmap(bitmapImage);
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
myIsolatedStorage.CreateDirectory("Images");
if (myIsolatedStorage.FileExists(filename))
{
myIsolatedStorage.DeleteFile(filename);
}
IsolatedStorageFileStream filestream = myIsolatedStorage.CreateFile(filename);
Extensions.SaveJpeg(wb, filestream, wb.PixelWidth, wb.PixelHeight, 0, 100);
filestream.Close();
}
}
请尝试以下代码加载图像并将其指定给背景:
IsolatedStorageFile Store = IsolatedStorageFile.GetUserStoreForApplication();
if (Store.FileExists("Images/app.jpg"))
{
try
{
using (IsolatedStorageFileStream Stream = new IsolatedStorageFileStream(Path, FileMode.Open, FileAccess.Read, Store))
{
if (Stream.Length > 0)
{
BitmapImage Image = new BitmapImage();
Image.SetSource(Stream);
this.LayoutRoot.Background = Image;
}
}
}
catch (Exception)
{
}
}
当应用程序启动时,在哪里放置代码以自动加载该图像?