C# Windows Phone 8上的ImageTools,更改ImageSource和DataContext

C# Windows Phone 8上的ImageTools,更改ImageSource和DataContext,c#,windows-phone-8,datacontext,C#,Windows Phone 8,Datacontext,我在WindowsPhone8项目中遇到DataContext问题。当我运行project并且MainPage()完成时-我看到第一个GIF,但当我点击按钮时-第一个GIF仍然可见。我不知道DataContext是如何工作的。有没有办法再次设置DataContext并显示第二个GIF namespace PhoneApp1 { public partial class MainPage : PhoneApplicationPage { public Uri Im

我在WindowsPhone8项目中遇到DataContext问题。当我运行project并且MainPage()完成时-我看到第一个GIF,但当我点击按钮时-第一个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;
        }


    }
}
名称空间PhoneApp1
{
公共部分类主页:PhoneApplicationPage
{
公共Uri ImageSource{get;set;}
//建造师
公共主页()
{
初始化组件();
ImageTools.IO.Decoders.AddDecoder();
ImageSource=新Uri(“http://c.wrzuta.pl/wi7505/bcd026ca001736004fc76975/szczur-piwo-gif-gif“,乌里金。绝对);
this.DataContext=this;
}
私有无效按钮\u单击\u 1(对象发送者,路由目标)
{
ImageSource=新Uri(“http://0-media-cdn.foolz.us/ffuuka/board/wsg/image/1338/94/1338947099997.gif“,乌里金。绝对);
this.DataContext=this;
}
}
}
XAML


您需要实现
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));
        }
    }
}
公共部分类主页面:PhoneApplicationPage,INotifyPropertyChanged
{
私有Uri imageSource;
公共Uri图像源
{
获取{return\u imageSource;}
设置{u imageSource=value;NotifyPropertyChanged(“imageSource”);}
}
//建造师
公共主页()
{
初始化组件();
ImageTools.IO.Decoders.AddDecoder();
this.DataContext=this;
ImageSource=新Uri(“http://c.wrzuta.pl/wi7505/bcd026ca001736004fc76975/szczur-piwo-gif-gif“,乌里金。绝对);
}
私有无效按钮\u单击\u 1(对象发送者,路由目标)
{
ImageSource=新Uri(“http://0-media-cdn.foolz.us/ffuuka/board/wsg/image/1338/94/1338947099997.gif“,乌里金。绝对);
}
公共事件属性更改事件处理程序属性更改;
/// 
///通知属性已更改。
/// 
///财产。
私有void NotifyPropertyChanged(字符串属性)
{
if(PropertyChanged!=null)
{
PropertyChanged(此,新PropertyChangedEventArgs(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));
        }
    }
}