我已经使用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));
}
}
PatientPage:
<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>
答案 0 :(得分:1)
不能构造XAML ViewModelLocator类型。为了在XAML中进行构造,类型不能为抽象,接口,嵌套,泛型或结构,并且必须具有公共默认构造函数
原因是ViewModelLocator
构造方法在您的方案中是私有的。因此,您无法在xaml中实例化它。您可以将其更改为公开。即使您不能使用它。因为xaml中的ViewModelLocator实例是由构造函数而不是单例方法创建的。换句话说<vm:ViewModelLocator xmlns:vm="using:PatientApp.UWP.ViewModels" x:Key="Locator" />
不等于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; }
}