WPF MVVM C#图像更新 [英] WPF MVVM C# Image updating

查看:211
本文介绍了WPF MVVM C#图像更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以请任何人在Web上向我展示一些示例,或者说明WPF MVVM C#中的图像更新.

Can please any one show me some example on web or explain Image updating in WPF MVVM C#.

我的XAML代码:

<Image Source="{Binding PicturePath}" VerticalAlignment="Center" Margin="0,0,10,0"/>

C#代码

private string _picturepath;
public string PicturePath
{
    get { return _picturepath; }
    set
    {
        _picturepath = value;
         NotifyPropertyChanged("PicturePath");
    }
}

PicturePath = IconPath + @"\Logo.png";

当图片更改时,我更新了PicturePath,但是程序中的图像保持不变.我还需要什么?

And when picture change, I update PicturePath, but Image in program stay the same. What I need more?

推荐答案

这是我在项目中最终得到的结果(我不得不说,花了相当长的时间测试似乎几乎可以正常工作的不同事物).您会看到注释的代码也无法正常工作100%不记得为什么)

Here's what I ended up with in my project (and I've got to say it took quite a while testing different things that seemed to almost work). You can see the commented code which was also not working 100% don't remember why)

所以我所有来自以"ms-appx:"开头的应用程序资源中的图像,我都使用了设置源而不加载流的方式,因为这些图像永远都不会改变(默认图像等)

So all my Images from app assets that are starting with "ms-appx:" I use with setting the source without loading to stream, because these never change (default images etc)

由用户创建或更改的其他图像,我必须重新加载并设置具有读取文件结果的源(否则,当它们更改时,有时它们不会更新)

The other images that are created or changed by the user I had to reload and set the source with the result of the file read (otherwise when they were changed sometimes they were not updating)

因此,基本上,我几乎在所有可以更改图像(不更改其名称)的地方都使用此转换器.

So basically I use this converter on almost all of the places that I use images that can change (without changing their name).

定义您的转换器:

<converters:ImageConverter x:Key="ImageConverter" /> 

然后像这样使用

<Image Source="{Binding PictureFilename, Converter={StaticResource ImageConverter}}"
       HorizontalAlignment="Center"
       VerticalAlignment="Center"/>

(另一种解决方法是用不同的名称命名图像,然后在更新源路径时可以正常工作.)

(Another workaround is to name your images differently and then when you update the source path it works fine.)

public class ImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        try
        {
            var CapturedImage = new BitmapImage();
            CapturedImage.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
            if (((string)value).StartsWith("ms-appx:"))
            {
                CapturedImage.UriSource = new Uri((string)value, UriKind.RelativeOrAbsolute);
                return CapturedImage;

            }
            var file = (StorageFile.GetFileFromPathAsync(new Uri((string)value, UriKind.RelativeOrAbsolute).LocalPath).AsTask().Result);
            using (IRandomAccessStream fileStream = file.OpenAsync(FileAccessMode.Read).AsTask().Result)
            {
                CapturedImage.SetSource(fileStream);
                return CapturedImage;

            }
        }
        catch (Exception e)
        {
            Logger.Error("Exception in the image converter!", e);
            return new BitmapImage();
        }

        //BitmapImage img = null;
        //if (value is string)
        //{
        //    img = new BitmapImage();
        //    img.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
        //    img.UriSource = new Uri((string)value, UriKind.RelativeOrAbsolute);
        //}

        //if (value is Uri)
        //{
        //    img = new BitmapImage();
        //    img.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
        //    img = new BitmapImage((Uri)value);
        //}

        //return img;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

这篇关于WPF MVVM C#图像更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆