Windows Phone 8上的ImageTools,更改ImageSource和DataContext [英] ImageTools on Windows Phone 8, changing ImageSource and DataContext
本文介绍了Windows Phone 8上的ImageTools,更改ImageSource和DataContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Windows Phone 8项目中使用DataContext遇到问题。当我运行项目并且MainPage()完成时-我看到第一个GIF,但是当我进入Button_Click_1时-第一个GIF仍然可见。我不知道DataContext如何工作。有没有办法再次设置DataContext并显示第二个GIF?
I have problem with DataContext in Windows Phone 8 project. When I run project and MainPage() is done - I see 1st GIF, but when I go Button_Click_1 - 1st GIF is still visible. I have no idea how DataContext work. Is there any way to set DataContext again and display 2nd GIF?
namespace PhoneApp1
{
public partial class MainPage : PhoneApplicationPage
{
public Uri ImageSource { get; set; }
// Constructor
public MainPage()
{
InitializeComponent();
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
ImageSource = new Uri("http://c.wrzuta.pl/wi7505/bcd026ca001736004fc76975/szczur-piwo-gif-gif", UriKind.Absolute);
this.DataContext = this;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
ImageSource = new Uri("http://0-media-cdn.foolz.us/ffuuka/board/wsg/image/1338/94/1338947099997.gif", UriKind.Absolute);
this.DataContext = this;
}
}
}
XAML
<imagetools:AnimatedImage x:Name="Image" Source="{Binding ImageSource, Converter={StaticResource ImageConverter}}" Margin="43,0,50,257" />
推荐答案
您将要实现 INotifyPropertyChanged
,因此 Xaml
知道对 ImageSource
属性的更改。
You will want to implement INotifyPropertyChanged
so the Xaml
knows of the changes to the ImageSource
property.
示例:
public partial class MainPage : PhoneApplicationPage, INotifyPropertyChanged
{
private Uri _imageSource;
public Uri ImageSource
{
get { return _imageSource; }
set { _imageSource = value; NotifyPropertyChanged("ImageSource"); }
}
// Constructor
public MainPage()
{
InitializeComponent();
ImageTools.IO.Decoders.AddDecoder<GifDecoder>();
this.DataContext = this;
ImageSource = new Uri("http://c.wrzuta.pl/wi7505/bcd026ca001736004fc76975/szczur-piwo-gif-gif", UriKind.Absolute);
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
ImageSource = new Uri("http://0-media-cdn.foolz.us/ffuuka/board/wsg/image/1338/94/1338947099997.gif", UriKind.Absolute);
}
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Notifies the property changed.
/// </summary>
/// <param name="property">The property.</param>
private void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
这篇关于Windows Phone 8上的ImageTools,更改ImageSource和DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文