2013-02-17 50 views
1

我在Windows Phone 8項目中的DataContext有問題。當我運行項目和MainPage()完成時 - 我看到第一個GIF,但是當我去Button_Click_1時 - 第一個GIF仍然可見。我不知道DataContext是如何工作的。有什麼辦法再次設置DataContext並顯示第二個GIF?Windows Phone 8上的ImageTools,更改ImageSource和DataContext

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" /> 

回答

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)); 
     } 
    } 
} 
+0

我能添加更多。它工作正常! :)謝謝 – boski 2013-02-21 08:07:02

+0

響亮而清晰!你做了我的晚上!謝謝! – 2014-09-25 21:37:24

相關問題