Windows Phone App data
本文关键字:data App Phone Windows | 更新日期: 2023-09-27 17:55:26
我想做的是我想在我的应用程序中为我的用户维护个人资料图片。用户可以从本地图片库中进行图片分析。 因此,每当应用程序加载时,它都应该检查本地应用程序数据是否存在图片,如果存在,则应将图像设置为该图片。 我尝试这样做,但它不起作用。
// Saving
BitmapImage bitmapImage = image.Source as BitmapImage;
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
string path = bitmapImage.UriSource.ToString();
localSettings.Values["ProfilePic"] = path;
// In constructor retrieving like this.
string sourceVal = localSettings.Values["ProfilePic"] as string;
if (sourceVal != null) {
Image img = new Image();
BitmapImage bi = new BitmapImage();
Uri uri = new Uri(sourceVal);
bi.UriSource = uri;
img.Source = bi;
}
它抛出异常,任何人都可以使用 C# 给他们适当的代码吗?
System.NullReferenceException
发生了。
在这里和那里修改了你的代码,它对我来说工作正常。看一看
XAML
<Image Name="img" Height="100" Width="100"/>
代码隐藏:
// define on top
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
//in constructor or in OnNavigatedTo()
string sourceVal = localSettings.Values["ProfilePic"] as string;
if (sourceVal != null)
{
// Image img1 = new Image();
BitmapImage bi = new BitmapImage();
Uri uri = new Uri(sourceVal, UriKind.Relative);
bi.UriSource = uri;
img.Source = bi;
}
else
{
// hardcoding image source for testing. You can set image here from gallery
img.Source = new BitmapImage(new Uri("/Assets/AlignmentGrid.png", UriKind.Relative));
}
// Save image (in OnNavigatedFrom())
BitmapImage bitmapImage = img.Source as BitmapImage;
string path = bitmapImage.UriSource.ToString();
localSettings.Values["ProfilePic"] = path;
试试这个,如果有任何问题,请告诉我。