Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新图像控件中的图像_C#_Wpf_Image - Fatal编程技术网

C# 更新图像控件中的图像

C# 更新图像控件中的图像,c#,wpf,image,C#,Wpf,Image,我正在尝试更新图像控件中的图像,该图像控件绑定到实现INotifyPropertyChanged的类。我已经尝试了与刷新位图缓存有关的大多数方法,以便图像可以刷新,但似乎没有一种方法适合我的情况。图像控件在xaml文件中定义为: 在类后面的代码中是: private ImageSource imagechart = null; public ImageSource Chart { get { return imagech

我正在尝试更新图像控件中的图像,该图像控件绑定到实现
INotifyPropertyChanged
的类。我已经尝试了与刷新位图缓存有关的大多数方法,以便图像可以刷新,但似乎没有一种方法适合我的情况。图像控件在xaml文件中定义为:
在类后面的代码中是:

 private ImageSource imagechart = null;

    public ImageSource Chart
    {
        get
        {
            return imagechart;
        }
        set
        {
            if (value != imagechart)
            {
                imagechart = value;
                NotifyPropertyChanged("Chart");

            }

        }
    }
事件发生后,我现在使用以下代码设置图像:

c.Chart = image;

当我现在运行我的应用程序时,这将显示图像,但在应用程序运行期间,我更新图像,但调用此
c.Chart=image显示初始图像。我开始明白WPF缓存图像,但所有声称解决这个问题的方法对我来说都是行不通的。对我不起作用的解决方案之一是

尝试将
图像
属性的返回类型更改为
Uri
。源属性上的TypeConverter应完成其余工作。如果这不起作用,请验证资源是否已实际更改

您可以使用assembly.GetManifestResourceStreams从程序集中读取资源并解析字节。然后手动将它们与File.writealBytes一起保存到输出目录中,查看是否具有预期的映像


据我所知,应用程序资源(嵌入到程序集中)不能在运行时更改(?)。您引用的是程序集ressource,而不是带有包uri的输出ressource。

谢谢大家的输入,因为通过它们,我终于找到了解决方法。因此,我的xaml仍然绑定为
,但在后面的代码中,我更改了类属性图表以返回位图,如下所示:

  private BitmapImage image = null;

    public BitmapImage Chart
    {
        get
        {
            return image;
        }
        set
        {
            if (value != image)
            {
                image = value;
                NotifyPropertyChanged("Chart");

            }

        }
    }
该类注意您实现的
INotifyPropertyChanged
。在设置图像时,我现在使用以下代码:

BitmapImage img = new BitmapImage();
img.BeginInit();
img.CacheOption = BitmapCacheOption.OnLoad;
img.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
//in the following code path is a string where i have defined the path to file
img.UriSource = new Uri(string.Format("file://{0}",path));
img.EndInit();
c.Chart = img;

这对我来说效果很好,并在更新时刷新图像。

尝试使绑定双向:image Source=“{binding Chart,Mode=TwoWay}”也不起作用。.我希望图像控件刷新图像而不必关闭应用程序,因为当我重新运行应用程序时,图像将显示为updated,只是询问。。。。应用程序上的所有其他绑定是否按预期工作?如何创建imagesource。。你能分享代码吗?@GarryPass所有其他绑定都按预期工作感谢你给了我一个主意并更改了属性以返回
BitmapImage
。查看我刚才发布的答案以了解更多详细信息。