如何在 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" };
基本上,如果有人愿意的话,我正在寻找一个工作示例。
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(3)
人生戏 2024-11-09 05:28:43
您无法将 ApplicationBarIconButton
的 Text 属性指定给 XAML 中的资源,这一点您已经解决了。要创建行为并将其附加到代码中,您可以使用类似于以下的代码(根据我现在正在开发的应用程序进行修改):
((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);
注意: 在上面的示例中 this 指的是视图的代码隐藏,并且不在查看模型。
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
根据马特的回答,这是我的解决方案:
将其添加到 xaml 页面的顶部:
并将其添加到底部的网格内:
参考文献:
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
Based on Matt's answer this is my solution:
Add this at the top of the xaml page:
and this inside the Grid at the bottom:
References:
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