windows-phone-7 - 如何在 C# 中向 ApplicationBarIconButton 添加行为?

标签 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" };

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

谢谢

最佳答案

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

在 xaml 页面的顶部添加:

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

在底部的网格中:

    <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

关于windows-phone-7 - 如何在 C# 中向 ApplicationBarIconButton 添加行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5676623/

相关文章:

c# - 收到调用任务启动器完成的通知

winforms - 在 windows Phone 中实现拖动和交换项目

javascript - IE : first-of-type 的 CSS 行为

javascript - 如何在 dojo 小部件上使用 dojo/behavior?

c# - 添加新的 ApplicationBarMenuItem 图标时无法分配给属性

c# - 如何在按下时更改应用程序栏按钮的颜色

c# - 我可以使用 NavigationContext.QueryString 设置参数吗? (Windows 手机)

wcf - Windows Phone 7 应用程序 + WCF + SSL + 用户名认证

我可以依赖 C 中的 %(模)运算符来获取负数吗?

windows-phone-7 - 更改浅色主题中的应用程序栏图标颜色 - Windows Phone 7