如何在C#中向ApplicationBarIconButton添加行为?

时间:2011-04-15 12:19:47

标签: windows-phone-7 behavior application-bar

我正在尝试使用行为向项目栏添加项目。

在xaml中,它们看起来像:

<phone:PhoneApplicationPage.ApplicationBar>
  <shell:ApplicationBar IsVisible="True"
                          IsMenuEnabled="True">
      <shell:ApplicationBarIconButton x:Name="Save" 
                                      IconUri="/resources/icons/appbar.check.rest.png"
                                      Text="Save"  />
      <shell:ApplicationBarIconButton x:Name="Cancel"
                                      IconUri="/resources/icons/appbar.cancel.rest.png"
                                      Text="Cancel"  />
  </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

<i:Interaction.Behaviors>
  <Behaviors:ApplicationBarIconButtonCommand TextKey="Save" 
                                             CommandBinding="{Binding SaveEventSetupCommand}" />
  <Behaviors:ApplicationBarIconButtonCommand TextKey="Cancel" 
                                             CommandBinding="{Binding CancelEventSetupCommand}" />
</i:Interaction.Behaviors>

对于多语言支持,我需要添加如下内容:

Text="{Binding Path=Localizedresources.lblCourse, Source={StaticResource LocalizedStrings}}"

到每个按钮。似乎这不能在xaml中完成,因此使用代码。

此代码中添加了按钮:

ApplicationBarIconButton appBarSaveButton = new ApplicationBarIconButton(
            new Uri("/resources/icons/appbar.check.rest.png", UriKind.Relative)) 
            { Text = "Test" };

ApplicationBar.Buttons.Add(appBarSaveButton);

我无法想象如何添加行为。这是我的起点:

WP7Contrib.View.Controls.Behaviors.ApplicationBarIconButtonCommand 
            ibc = new WP7Contrib.View.Controls.Behaviors.ApplicationBarIconButtonCommand 
{ TextKey = "Test" };

基本上我正在寻找一个工作样本,如果有人可以的话。

由于

3 个答案:

答案 0 :(得分:1)

根据马特的回答,这是我的解决方案:

在xaml页面的顶部添加:

xmlns:Preview="clr-namespace:Phone7.Fx.Preview;assembly=Phone7.Fx.Preview"

并且在底部的Grid内部:

    <Preview:BindableApplicationBar x:Name="AppBar" BarOpacity="1.0" IsVisible="{Binding IsBarVisible}" >
        <Preview:BindableApplicationBarIconButton 
            Command="{Binding SaveCommand}"
            Text="{Binding Path=Localizedresources.lblSave, Source={StaticResource LocalizedStrings}}" 
            IconUri="/resources/icons/appbar.check.rest.png" />
        <Preview:BindableApplicationBarIconButton 
            Command="{Binding CancelCommand}"
            Text="{Binding Path=Localizedresources.lblCancel, Source={StaticResource LocalizedStrings}}" 
            IconUri="/resources/icons/appbar.cancel.rest.png" />
    </Preview:BindableApplicationBar>

参考文献:
http://blog.humann.info/post/2010/08/27/How-to-have-binding-on-the-ApplicationBar.aspx

http://www.codeproject.com/KB/windows-phone-7/CommandToAppBarWP7.aspx?display=Mobile

答案 1 :(得分:0)

您无法为XAML中的资源指定ApplicationBarIconButton文本属性,您已经解决了这个问题。要创建行为并将其附加到代码中,您可以使用类似于以下内容的代码(从我正在处理的应用程序中修改):

((ApplicationBarIconButton)this.ApplicationBar.Buttons[0].Text = Strings.NewContact;
var newBehavior = new ApplicationBarButtonNavigation
{
    ButtonText = Strings.NewContact,
    NavigateTo = new Uri("/views/ContactView.xaml", UriKind.RelativeOrAbsolute),
};
Interaction.GetBehaviors(this).Add(newBehavior);

您的方案的主体是相同的:创建行为,然后使用Interaction.GetBehaviors(this).Add(yourBehavior);

注意:在上面的示例中,指的是视图的代码隐藏,不在视图模型中。

答案 2 :(得分:0)

您可以使用http://blog.humann.info/post/2010/08/27/How-to-have-binding-on-the-ApplicationBar.aspx

中的包装器使ApplicationBar可绑定

不确定添加命令,但是可以采用相同的技术。

相关问题