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

| 我正在尝试将行为添加到应用程序栏中。 在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\" };
基本上,如果有人愿意,我正在寻找一个有效的样本。 谢谢     
已邀请:
根据Matt的回答,这是我的解决方案: 将其添加到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     
您无法为XAML中的资源指定ѭ6的Text属性。要创建行为并将其附加到代码中,请使用类似于以下代码(从我现在正在使用的应用程序中修改):
((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);
注意:在上面的示例中,这是指视图的背后代码,而不是视图模型中的代码。     
您绝对可以使用http://blog.humann.info/post/2010/08/27/How-to-have-binding-on-the-ApplicationBar.aspx中的包装器使ApplicationBar具有可绑定性。 不确定是否要添加命令,但是可以使用相同的技术来实现。     

要回复问题请先登录注册