【问题标题】:Windows Phone App dataWindows Phone 应用程序数据
【发布时间】:2015-08-28 00:01:41
【问题描述】:

我想做的是在我的应用中为我的用户维护个人资料图片。 用户可以从本地图片库中获取图片。因此,每当应用程序加载时,它应该检查本地应用程序数据是否存在图片,如果存在,它应该将图像设置为它。我试过这样做,但它不起作用。

// 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 发生。

【问题讨论】:

标签: c# windows windows-phone-8


【解决方案1】:

在这里和那里修改了你的代码,它对我来说工作正常。看看

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;

试试这个,如果有任何问题,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    相关资源
    最近更新 更多