【问题标题】:How do I add a behavior to ApplicationBarIconButton in C#?如何在 C# 中向 ApplicationBarIconButton 添加行为?
【发布时间】:2011-11-10 17:42:37
【问题描述】:

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

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

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

谢谢

【问题讨论】:

  • 什么样的行为?我担心它无法完成,因为 ApplicationBar 不是 Silverlight 控件。

标签: windows-phone-7 behavior application-bar


【解决方案1】:

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

在 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

【讨论】:

    【解决方案2】:

    您不能将 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 指的是视图的代码隐藏,不在视图模型中。

    【讨论】:

      【解决方案3】:

      您绝对可以使用 http://blog.humann.info/post/2010/08/27/How-to-have-binding-on-the-ApplicationBar.aspx 中的包装器使 ApplicationBar 可绑定

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-15
        • 2015-03-23
        • 1970-01-01
        • 2011-04-14
        • 2017-03-09
        • 2013-01-21
        • 1970-01-01
        • 2015-01-12
        相关资源
        最近更新 更多