我在Windows Phone 8项目中遇到DataContext问题。当我运行项目并且MainPage()完成时 - 我看到第一个GIF,但是当我去Button_Click_1时 - 第一个GIF仍然可见。我不知道DataContext是如何工作的。有没有办法再次设置DataContext并显示第二个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" />
答案 0 :(得分:3)
您需要实施INotifyPropertyChanged
,以便Xaml
知道ImageSource
属性的更改。
示例:
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));
}
}
}