【发布时间】:2019-03-26 13:01:51
【问题描述】:
我已经使用 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>
【问题讨论】: