Mvvm WTS无法在XAML中声明ViewModelLocator

Mvvm WTS无法在XAML中声明ViewModelLocator,mvvm,uwp,Mvvm,Uwp,我已经用Windows Template Studio创建了我的MVVM Light应用程序,现在我正在尝试在XAML中使用数据绑定。但对于生成的代码来说,这似乎是不可能的,因为它没有在XAML中声明Viewmodel。我更改了代码以执行此操作,但后来遇到了以下错误: 无法构造XAML ViewModelLocator类型。为了在XAML中构造,类型不能是抽象、接口、嵌套、泛型或结构,并且必须具有公共默认构造函数 我已经尝试将构造函数公开,但这并不能解决问题。有没有办法让数据绑定正常工作 以下是

我已经用Windows Template Studio创建了我的MVVM Light应用程序,现在我正在尝试在XAML中使用数据绑定。但对于生成的代码来说,这似乎是不可能的,因为它没有在XAML中声明Viewmodel。我更改了代码以执行此操作,但后来遇到了以下错误:

无法构造XAML ViewModelLocator类型。为了在XAML中构造,类型不能是抽象、接口、嵌套、泛型或结构,并且必须具有公共默认构造函数

我已经尝试将构造函数公开,但这并不能解决问题。有没有办法让数据绑定正常工作

以下是所有代码:

App.XAML

<Application
x:Class="PatientApp.UWP.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:PatientApp.UWP.ViewModels">

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <XamlControlsResources  xmlns="using:Microsoft.UI.Xaml.Controls"/>
            <ResourceDictionary Source="/Styles/_Colors.xaml"/>
            <ResourceDictionary Source="/Styles/_FontSizes.xaml"/>
            <ResourceDictionary Source="/Styles/_Thickness.xaml"/>
            <ResourceDictionary Source="/Styles/TextBlock.xaml"/>
            <ResourceDictionary Source="/Styles/Page.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <vm:ViewModelLocator xmlns:vm="using:PatientApp.UWP.ViewModels" x:Key="Locator" />
    </ResourceDictionary>
</Application.Resources>
ViewModelLocator:

    [Windows.UI.Xaml.Data.Bindable]
public class ViewModelLocator
{
    private static ViewModelLocator _current;

    public static ViewModelLocator Current => _current ?? (_current = new ViewModelLocator());

    private ViewModelLocator()
    {
        SimpleIoc.Default.Register(() => new NavigationServiceEx());
        SimpleIoc.Default.Register<ShellViewModel>();
        Register<PatientsViewModel, AllPatientsPage>();
        Register<DiseasesViewModel, AllDiseasesPage>();
    }

    public DiseasesViewModel DiseasesViewModel => SimpleIoc.Default.GetInstance<DiseasesViewModel>();

    public PatientsViewModel PatientsViewModel => SimpleIoc.Default.GetInstance<PatientsViewModel>();

    public ShellViewModel ShellViewModel => SimpleIoc.Default.GetInstance<ShellViewModel>();

    public NavigationServiceEx NavigationService => SimpleIoc.Default.GetInstance<NavigationServiceEx>();

    public void Register<VM, V>()
        where VM : class
    {
        SimpleIoc.Default.Register<VM>();

        NavigationService.Configure(typeof(VM).FullName, typeof(V));
    }
}
第页:

<Page
x:Class="PatientApp.UWP.Views.AllPatientsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Style="{StaticResource PageStyle}"
mc:Ignorable="d"
xmlns:datamodel="using:PatientApp.Model"
DataContext="{Binding PatientViewModelInstance, Source={StaticResource Locator}}">
<Grid
        Background="{ThemeResource SystemControlPageBackgroundChromeLowBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <CommandBar DefaultLabelPosition="Right"
                Grid.Row="0"
                OverflowButtonVisibility="Collapsed">

        <AppBarButton Icon="Sort" Label="Sort" />
        <AppBarButton Icon="Edit" Label="Edit" />
        <AppBarButton Icon="Add" Label="Add" />
        <AppBarButton Icon="Zoom" Label="Search" />
    </CommandBar>
    <GridView  x:Name="Patients"
               Grid.Row="1"
               ItemsSource="{Binding Patients}">
        <GridView.ItemsPanel>
            <ItemsPanelTemplate>
                <ItemsStackPanel Margin="14,0,0,0" Orientation="Vertical" />
            </ItemsPanelTemplate>
        </GridView.ItemsPanel>
        <GridView.ItemTemplate>
            <DataTemplate x:DataType="datamodel:Patient">
                <TextBlock Text="{x:Bind Name}"
                                   FontWeight="Medium"
                                   TextWrapping="NoWrap"
                                   HorizontalAlignment="Left" />
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>
</Grid>
无法构造XAML ViewModelLocator类型。为了在XAML中构造,类型不能是抽象、接口、嵌套、泛型或结构,并且必须具有公共默认构造函数

原因是ViewModelLocator构造方法在场景中是私有的。因此您无法在xaml中实例化它。你可以把它改成公共的。即使你不能使用它。因为xaml中的ViewModelLocator实例是由构造函数而不是单例方法创建的。换句话说,不等于ViewModelLocator.Current。不幸的是,ActivationService使用ViewModelLocator.Current来获取NavigationService。因此,应用程序在启动时抛出异常

public static NavigationServiceEx NavigationService => ViewModelLocator.Current.NavigationService;
使用MVVMLight很难接近。通常,页面DataContext是在代码隐藏中进行保护的。并与x:bind绑定到绑定数据源

private SettingsViewModel ViewModel
{
    get { return ViewModelLocator.Current.SettingsViewModel; }
}

的确使用{x:Bind ViewModel.Patients}似乎可以像我希望的那样工作。