简体   繁体   English

WPF MVVM C#​​ 图像更新

[英]WPF MVVM C# Image updating

Can please any one show me some example on web or explain Image updating in WPF MVVM C#.可以请任何人在网上向我展示一些示例或解释 WPF MVVM C#​​ 中的图像更新。

My XAML code:我的 XAML 代码:

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

C# code C# 代码

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

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

And when picture change, I update PicturePath , but Image in program stay the same.当图片更改时,我更新PicturePath ,但程序中的 Image 保持不变。 What I need more?我还需要什么?

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)您可以看到注释的代码也无法 100% 工作,不记得为什么)

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)所以我所有来自应用程序资产的图像都以“ms-appx:”开头,我使用设置源而不加载到流,因为这些永远不会改变(默认图像等)

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).所以基本上我在几乎所有我使用可以更改的图像的地方使用这个转换器(不更改它们的名称)。

define your converter:定义您的转换器:

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

And then use like this然后像这样使用

<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();
    }
}

So now I made stand alone procjet only for picture binding所以现在我制作了独立的 procjet 仅用于图片绑定

ImageConverter code:图像转换器代码:

public class ImageConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo language)
    {
        try
        {
            return new BitmapImage(new Uri((string)value));
        }
        catch
        {
            return new BitmapImage();
        }
    }

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

MainWindowViewModel:主窗口视图模型:

public class MainWindowViewModel : ViewModelBase
{
    public MainWindowViewModel()
    {
        Changetopicone = new RelayCommand(param => piconevoid());
        Changetopictwo = new RelayCommand(param => pictwovoid());
        LogoSourcePath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
    }

    private void piconevoid()
    {
        PicturePath = LogoSourcePath + @"\pic1.png";
    }

    private void pictwovoid()
    {
        PicturePath = LogoSourcePath + @"\pic2.png";
    }

    public string LogoSourcePath { get; set; }

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

    public ICommand Changetopicone
    {
        get;
        set;
    }

    public ICommand Changetopictwo
    {
        get;
        set;
    }
}

MainWindow:主窗口:

<StackPanel>
        <Button Command="{Binding Changetopicone}" Content="Changetopicone"/>
        <Button Command="{Binding Changetopictwo}" Content="Changetopictwo"/>
        <Image Source="{Binding PicturePath, Converter={StaticResource ImgCon}}" VerticalAlignment="Center" Width="200" Height="200"/>
    </StackPanel>

In my program I have some problem, so I made this standalone project to be sure that works.在我的程序中我遇到了一些问题,所以我制作了这个独立项目以确保它有效。

Thanks for all the help!感谢所有的帮助!

You need to implement the interface INotifyPropertyChanged, add this code, then call OnPropertyChanged() from the setter of PicturePath.您需要实现接口INotifyPropertyChanged,添加这段代码,然后从PicturePath 的setter 中调用OnPropertyChanged()。

void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM