豆瓣電台WP7用戶端 MVVM重構記錄之使用AppBarUtils使ApplicationBarIconButton支援綁定(包括IconUri)

來源:互聯網
上載者:User

因為ApplicationBar並不支援資料繫結,所以做MVVM的時候是個麻煩。經過今天的研究終於搞定的了。

我們需要的是一個第三方dll:

AppBarUtils 大牛Allen Lee的。:http://appbarutils.codeplex.com

不過目前所提供的功能來看只支援Text跟Command的綁定,於是我下載源碼下來修改了一下,使之支援了IconUri綁定。這就不貼代碼了,直接提供改過的dll。

使用:

添加xmlns:

 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:AppBarUtils="clr-namespace:AppBarUtils;assembly=AppBarUtils"

在XAML添加:

  <phone:PhoneApplicationPage.ApplicationBar>        <shell:ApplicationBar IsVisible="True" >            <shell:ApplicationBarIconButton   IconUri="/icon/f.png" Text="test" />        </shell:ApplicationBar>  </phone:PhoneApplicationPage.ApplicationBar>    <i:Interaction.Behaviors>        <AppBarUtils:AppBarItemCommand Id="test" Text="{Binding BtnName}" IconUri="{Binding ImgUri}" Command="{Binding ChangeAppBarBtnImg}" />    </i:Interaction.Behaviors>

VM:

public class MainPageViewModel : ViewModelBase   {       public MainPageViewModel()       {           ChangeAppBarBtnImg = new RelayCommand(ChangeImg);       }       private string _BtnName="OK";       public string BtnName       {           get { return _BtnName; }           set { this._BtnName = value;           this.RaisePropertyChanged("BtnName");           }       }       private Uri _ImgUri = new Uri("/icon/f.png", UriKind.Relative);       public Uri ImgUri       {           get { return _ImgUri; }           set           {               _ImgUri = value;               this.RaisePropertyChanged("ImgUri");           }       }       private RelayCommand _ChangeAppBarBtnImg;       public RelayCommand ChangeAppBarBtnImg       {           get { return _ChangeAppBarBtnImg; }           set { _ChangeAppBarBtnImg = value;           this.RaisePropertyChanged("ChangeAppBarBtnImg");           }       }       private void ChangeImg()       {           if (this.ImgUri.OriginalString == "/icon/f.png")           {               this.ImgUri = new Uri("/icon/s.png", UriKind.Relative);           }           else           {               this.ImgUri = new Uri("/icon/f.png", UriKind.Relative);           }       }   }

這樣就可以使AppliactionBar支援綁定了。

樣本下載:/Files/kklldog/WP7ApplicationBarBinding.rar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.