【发布时间】: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