Xaml 在WinPhone 8.1、Prism和Unity中解析ViewModels

Xaml 在WinPhone 8.1、Prism和Unity中解析ViewModels,xaml,mvvm,windows-phone-8.1,unity-container,prism,Xaml,Mvvm,Windows Phone 8.1,Unity Container,Prism,我正在开发一个Windows Phone 8.1应用程序,其中Prism用于Windows应用商店应用程序,Unity作为IoC容器。到目前为止,我已经掌握了基本知识,基于约定的视图和ViewModel解析到目前为止仍然有效 对于导航,我希望使用抽屉布局(带有弹出式菜单)并在主页中显示所选型号(而不是导航到页面) 因此,我有一个MainViewModel(使用自定义名称约定,它被正确实例化并分配给MainPage)。我有一个DrawerViewModel。抽屉位于主页内,应显示其他视图模型 My

我正在开发一个Windows Phone 8.1应用程序,其中Prism用于Windows应用商店应用程序,Unity作为IoC容器。到目前为止,我已经掌握了基本知识,基于约定的视图和ViewModel解析到目前为止仍然有效

对于导航,我希望使用抽屉布局(带有弹出式菜单)并在
主页
中显示所选型号(而不是导航到页面)

因此,我有一个
MainViewModel
(使用自定义名称约定,它被正确实例化并分配给
MainPage
)。我有一个
DrawerViewModel
。抽屉位于
主页
内,应显示其他视图模型

My MainPage.xaml:

<Page
    x:Class="StackExchangeReader.Views.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:StackExchangeReader"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:m="using:StackExchangeReader.Models"
    xmlns:drawerLayout="using:DrawerLayout"
    xmlns:prism="using:Microsoft.Practices.Prism.Mvvm"
    xmlns:designViewModels="using:StackExchangeReader.DesignViewModels"
    mc:Ignorable="d"
    prism:ViewModelLocator.AutoWireViewModel="True"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    d:DataContext="{d:DesignInstance designViewModels:DrawerDesignViewModel, IsDesignTimeCreatable=True}">

    <Grid x:Name="RootLayout">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <!-- Title Bar -->
        <Grid x:Name="TitleBar" Background="#00ADEF" Grid.Row ="0" Height="60">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Image Margin="5"  x:Name="DrawerIcon"  Grid.Column="0" Source="/Assets/drawer_icon.png" HorizontalAlignment="Left" Tapped="DrawerIcon_Tapped" />
            <TextBlock Grid.Column="1" Text="{Binding Title}" Foreground="White" VerticalAlignment="Center" FontSize="18"/>
        </Grid>
        <!-- Drawer Layout -->
        <drawerLayout:DrawerLayout Grid.Row="1"  x:Name="DrawerLayout">
            <Grid x:Name="MainFragment" Background="White">
                <!-- Main Content goes here -->
            </Grid>

            <Grid x:Name="ListFragment" Background="#F4F4F4">
                <ListView DataContext="{Binding Sites}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Name}" Margin="10" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="18" Foreground="Black" />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

            </Grid>
        </drawerLayout:DrawerLayout>
    </Grid>
</Page>
DroperViewModel.cs

public class MainViewModel : ViewModel
{
    private INavigationService navigation;
    private IEventAggregator events;
    public MainViewModel(INavigationService navigation, IEventAggregator events)
    {
        this.navigation = navigation;
        this.events = events;
    }

    public string Title { get; set; } = "Example";
    private ViewModel selectedViewModel;

    public ViewModel SelectedViewModel
    {
        get { return selectedViewModel; }
        set { SetProperty(ref selectedViewModel, value); }
    }

}
public class DrawerViewModel : ViewModel
{
    private ISiteService siteService;
    private IEventAggregator events;

    public DrawerViewModel(ISiteService siteService, IEventAggregator events)
    {
        this.siteService = siteService;
        this.events = events;

        sites = new ObservableCollection<Site>();
    }

    private ObservableCollection<Site> sites;
    public ObservableCollection<Site> Sites
    {
        get { return sites; }
    }
}
公共类抽屉视图模型:视图模型
{
私人ISiteService站点服务;
私人私家侦探事件;
公共付款人IEWModel(ISiteService站点服务、IEventAggregator事件)
{
this.siteService=siteService;
这个事件=事件;
站点=新的可观测集合();
}
私人可观测采集点;
公众可观察的收集地点
{
获取{返回站点;}
}
}