wpf 图像源绑定到本地存储中的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15160868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:48:31  来源:igfitidea点击:

Image Source binding to file in local Storage

c#wpfimagexamlwindows-phone-8

提问by max06

In Windows Metro Apps (C#), I'm using a ValueConverter to pass an Image-Uri like this:

在 Windows Metro 应用程序 (C#) 中,我使用 ValueConverter 来传递这样的 Image-Uri:

public class ProfileImage : IValueConverter {

    public Object Convert(Object value, Type targetType, Object parameter, String language) {

        if (value == null) {
            return "Common/images_profile/user.png";
        }

        return "ms-appdata:///local/" + (String)value;

    }

    public Object ConvertBack(Object value, Type targetType, Object parameter, String language) {
        return value;
    }

}

XAML:

XAML:

<Image x:Name="profileImage" Height="80" Width="80" Source="{Binding Path, Converter={StaticResource ProfileImage}}"/>

The images are being downloaded async into localFolder.

图像被异步下载到 localFolder 中。

I wanted to use this on Windows Phone 8 - but it doesn't show up any image.

我想在 Windows Phone 8 上使用它 - 但它没有显示任何图像。

var localFolder = ApplicationData.Current.LocalFolder;
StorageFile myFile = await localFolder.CreateFileAsync(
    UID + ".jpg",
    CreationCollisionOption.FailIfExists);

using (var s = await myFile.OpenStreamForWriteAsync()) {
    s.Write(imageBytes, 0, imageBytes.Length);
}

is used for writing the Images to the LocalStorage.

用于将图像写入 LocalStorage。

If there's no content in value, the Image in Common/images_profile/user.pngis being displayed properly. This one is in the package, not in the local Folder.

如果 中没有内容value,则图像中Common/images_profile/user.png的显示正确。这是在包中,而不是在本地文件夹中。

I need to know which format I have to use as return parameter to get the images displayed.

我需要知道必须使用哪种格式作为返回参数才能显示图像。

采纳答案by chrs.zrkl

I think the URL scheme ms-appdata:///does not work everywhere.

我认为 URL 方案ms-appdata:///并不适用于任何地方。

I'm using this conveter for binding images from isolated storage:

我正在使用这个转换器来绑定来自独立存储的图像:

public class PathToImageConverter : IValueConverter
{
    private static IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication();

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string path = value as string;

        if (string.IsNullOrEmpty(path))
            return null;

        if ((path.Length > 9) && (path.ToLower().Substring(0, 9).Equals("isostore:")))
        {
            using (var sourceFile = isoStorage.OpenFile(path.Substring(9), FileMode.Open, FileAccess.Read))
            {
                BitmapImage image = new BitmapImage();
                image.SetSource(sourceFile);

                return image;
            }
        }
        else
        {
            BitmapImage image = new BitmapImage(new Uri(path));

            return image;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

For binding, you must use the isostore:prefix in your url.

对于绑定,您必须在 url 中使用isostore:前缀。

回答by Hermit Dave

must be missing something here.. why don't you use StorageFile.Path instead of returning "return "ms-appdata:///local/" + (String)value;"

一定在这里遗漏了一些东西..为什么不使用 StorageFile.Path 而不是返回“返回“ms-appdata:///local/”+(字符串)值;“

Realised that its #WP8 so another thing. You can still use isolated storage and Silverlight Uri

意识到它的#WP8 是另一回事。您仍然可以使用独立存储和 Silverlight Uri