C# 在图像组件中显示光盘中的图像
我有一个应用程序,可以循环浏览从游戏中截取的屏幕截图。我的当前解决方案适用于第一个图像,但在循环图像后,它不会刷新。My PropertyChanged事件已配置并正常运行。只有图像不工作 我的显示代码是:C# 在图像组件中显示光盘中的图像,c#,wpf,xaml,C#,Wpf,Xaml,我有一个应用程序,可以循环浏览从游戏中截取的屏幕截图。我的当前解决方案适用于第一个图像,但在循环图像后,它不会刷新。My PropertyChanged事件已配置并正常运行。只有图像不工作 我的显示代码是: <Image DataContext="{StaticResource IMG}" Stretch="UniformToFill"> <Image.Source> <BitmapImage UriSource="{Binding Image}"
<Image DataContext="{StaticResource IMG}" Stretch="UniformToFill">
<Image.Source>
<BitmapImage UriSource="{Binding Image}"/>
</Image.Source>
</Image>
然后我的代码将检索绝对Uri,并通过绑定进行设置
我如何才能让它工作?我不相信
BitmapImage
类上的UriSource
会触发图像的重新加载/重新渲染<代码>图像。源代码
有效,但是:
void Main()
{
var firstUri= new Uri("http://www.gravatar.com/avatar/2e8b6a4ea2ee8aedc49e5e4299661543?s=128&d=identicon&r=PG");
var secondUri= new Uri("http://www.gravatar.com/avatar/23333db13ce939b8a70fb36dbfd8f934?s=32&d=identicon&r=PG");
var wnd = new Window();
var pnl = new StackPanel();
wnd.Content = pnl;
var img = new Image()
{
Source = new BitmapImage(firstUri),
Stretch = Stretch.UniformToFill,
};
wnd.Show();
pnl.Children.Add(img);
// Optional; just need something to change the img src
Observable
.Timer(TimeSpan.FromSeconds(2))
// The next line is roughly equivalent to invoking on
// the Dispatcher (i.e., marshalling back to the UI thread)
.ObserveOn(new DispatcherScheduler(wnd.Dispatcher))
.Subscribe(_ =>
{
img.Source = new BitmapImage(secondUri);
});
}
将代码更改为返回BitmapImage而不是Uri可以解决此问题 这意味着XAML应更改为:
<Image DataContext="{StaticResource IMG}" Stretch="UniformToFill" Source="{Binding Image}" />
是的,谢谢,我把我得到的作为另一个答案发布了出来。如果我创建了一个新的位图图像并在我的代码中返回它,它就会工作。谢谢你给我指明了正确的方向!