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

发布于 2024-11-02 05:28:43 字数 1862 浏览 4 评论 0 原文

我正在尝试将项目添加到具有行为的应用程序栏。

在 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" };

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

谢谢

I am attempting to add items to an application bar with behavoirs.

In xaml they look like:

<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>

For multi language support I need to add something like:

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

to each button. It would appear that this cannot be done in xaml, hence the use of code.

The button is added in this code:

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

ApplicationBar.Buttons.Add(appBarSaveButton);

I just can't figure how to add the behavior. This is my start point:

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

Basically I am looking for a working sample if anyone can oblige.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

友欢 2024-11-09 05:28:43

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

将其添加到 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

Based on Matt's answer this is my solution:

Add this at the top of the xaml page:

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

and this inside the Grid at the bottom:

    <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>

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

人生戏 2024-11-09 05:28:43

您无法将 ApplicationBarIconButtonText 属性指定给 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 指的是视图的代码隐藏,并且不在查看模型。

You cannot specify the Text property of an ApplicationBarIconButton to a resource in XAML, which you've already worked out. To create a behavior and attached it in code you use code similar to the following (modified from an app I'm working on right now):

((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);

The principal is the same for your scenario: create the behavior, and then use Interaction.GetBehaviors(this).Add(yourBehavior);

NOTE: In the above examples this refers to the code-behind for the view and is not in the view model.

我一直都在从未离去 2024-11-09 05:28:43

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

不确定是否添加命令,但这使用相同的技术应该是可能的。

You can definitely make the ApplicationBar bindable by using the wrapper from http://blog.humann.info/post/2010/08/27/How-to-have-binding-on-the-ApplicationBar.aspx

Not sure about adding commands but this shoudl be possible with the same technique.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文