【问题标题】:How to do binding for a global ApplicationBar in Windows Phone with MvvmCross如何使用 MvvmCross 绑定 Windows Phone 中的全局 ApplicationBar
【发布时间】:2014-08-23 22:10:44
【问题描述】:

我正在尝试创建一个 ApplicationBar,它将对我的 Windows Phone 应用程序是全局的。也就是说,我想为 ApplicationBar 创建和设置一次绑定,但让它显示在多个不同的页面(视图)上。

目前我的方法是在 App.xaml 文件中创建 ApplicationBar,以便我可以将其用作我的视图中的静态资源。我的 App.xaml 文件目前如下所示:

<Application 
    x:Class="MyApplication.WindowsPhone.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"       
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:valueConverters="clr-namespace:MyApplication.WindowsPhone.ValueConverters"
    xmlns:viewModels="clr-namespace:MyApplication.Core.ViewModels;assembly=MyApplication.Core">

    <!--Application Resources-->
    <Application.Resources>
        <shell:ApplicationBar x:Key="GlobalAppBar" IsVisible="True" IsMenuEnabled="True" Mode="Minimized">
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Text="Action 1" Click="MenuItem1_Click"/>
                <shell:ApplicationBarMenuItem Text="Action 2" Click="MenuItem2_Click"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
        <valueConverters:NativeVisibilityConverter x:Name="VisibilityConverter" />
        <viewModels:MenuViewModel x:Key="MenuViewModel" />
    </Application.Resources>

    <Application.ApplicationLifetimeObjects>
        <!--Required object that handles lifetime events for the application-->
        <shell:PhoneApplicationService 
            Launching="Application_Launching" Closing="Application_Closing" 
            Activated="Application_Activated" Deactivated="Application_Deactivated"/>
    </Application.ApplicationLifetimeObjects>

</Application>

这很好,我可以在我的视图中重用 ApplicationBar,例如:

<views:BaseView
    x:Class="MyApplication.WindowsPhone.Views.FirstView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:views="clr-namespace:MyApplication.WindowsPhone.Views"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    mc:Ignorable="d"
    shell:SystemTray.IsVisible="True"
    ApplicationBar="{StaticResource GlobalAppBar}">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel Grid.Row="0" Margin="12,17,0,28">
            <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock Text="{Binding PageTitle}" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

        </Grid>
    </Grid>

</views:BaseView>

但是,现在我想将每个 ApplicationBarMenuItem 的 Text 和 Click 属性绑定到 MenuViewModel 中的属性和命令,但我不确定如何才能做到这一点。是否可以从 App.xaml 文件中实现与 ViewModel 的绑定,或者我需要采取其他一些路径吗?

非常感谢任何意见。

【问题讨论】:

    标签: c# xaml windows-phone-8 mvvm mvvmcross


    【解决方案1】:

    Windows Phone AppBar 是幼稚的而不是 Silverlight 控件 - 所以它们不是可直接绑定的。

    但是,它们存在多个包装器 - 搜索“windowsphone bindable application bar”,您会找到几个包装器 - 例如。 http://cimbalino.org 包括一个通过 Behaviorhttp://bindableapplicationb.codeplex.com 的包装器,只是针对这个问题。

    当您使用包装器时,您应该可以轻松地将其 DataContext 设置为您的 MenuViewModel

    【讨论】:

    • 感谢您抽出宝贵时间回复!所以据我所知,要实现我所追求的并不是一件容易的事,这是一个全球应用程序吧。据我所知,如果我选择一个包装器,我将无法设置应用程序栏并在不同的页面上重复使用它,例如通过设置每个页面的 ApplicationBar 属性。
    • 我认为你可以 - 但我可能是错的。我不认为这与 MvvmCross 相关 - 我认为这是一个一般的 WinPhone 问题 - 希望一些 WP 专家能够提供帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    相关资源
    最近更新 更多