Windows Phone 应用数据
Windows Phone App data
我想做的是在我的应用程序中为我的用户维护个人资料照片。
用户可以从本地图片库中获取图片。因此,无论何时加载应用程序,它都应检查本地应用程序数据中是否存在图片,如果存在,则应为其设置图像。我试过这样做,但没有用。
// 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;
试试这个,如果有任何问题请告诉我。
我想做的是在我的应用程序中为我的用户维护个人资料照片。 用户可以从本地图片库中获取图片。因此,无论何时加载应用程序,它都应检查本地应用程序数据中是否存在图片,如果存在,则应为其设置图像。我试过这样做,但没有用。
// 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;
试试这个,如果有任何问题请告诉我。