繁体   English   中英

如何使用MvvmCross绑定Windows Phone中的全局ApplicationBar

[英]How to do binding for a global ApplicationBar in Windows Phone with MvvmCross

我正在尝试创建一个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的绑定,还是我需要采取其他方法?

非常感谢任何输入。

Windows Phone AppBar只是天真而不是Silverlight控件-因此它们不是可绑定的。

但是,有几个包装器可供使用-搜索“ Windowsphone可绑定应用程序栏”,您将找到几个包装器-例如, http : //cimbalino.org通过Behavior包含一个包装器,或者http://bindableapplicationb.codeplex.com是包装器。只是为了这个问题。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM