1

WindowsPhone8プロジェクトのDataContextに問題があります。プロジェクトを実行してMainPage()を実行すると、最初のGIFが表示されますが、Button_Click_1に移動すると、最初のGIFが表示されたままになります。DataContextがどのように機能するのかわかりません。DataContextを再度設定して2番目の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" />
4

1 に答える 1

3

プロパティへの変更を認識INotifyPropertyChangedできるように実装する必要があります。XamlImageSource

例:

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));
        }
    }
}
于 2013-02-17T23:01:22.240 に答える