豆瓣电台WP7客户端 MVVM重构记录之使用AppBarUtils使ApplicationBarIconButton支持绑定(包括IconUri)...

因为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

转载于:https://www.cnblogs.com/kklldog/archive/2012/03/05/2380726.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值