我正在尝试将项目添加到具有行为的应用程序栏。
在 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/