UWP MVVM:ViewModel绑定

时间:2016-04-03 15:52:23

标签: c# mvvm win-universal-app uwp-xaml

我正试图从UWP的角度进入MVVM。似乎所有东西都像WPF一样,但我无法运行良好的ViewModel / View Mapping。 根据Microsodt-Specialist:https://channel9.msdn.com/Events/Build/2015/3-635,在20分钟左右开始,ResourceDictionaries有一些新的特殊语法。我已经完成了所有步骤,按预期加载了Dictionary,但直接映射不起作用:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:v="using:ARGUSnet.RaspberryPiFramework.Designer.Views"
xmlns:vm="using:ARGUSnet.RaspberryPiFramework.Designer.ViewModels"
xmlns:local="using:ARGUSnet.RaspberryPiFramework.Designer.Dictionaries"
    x:Class="ARGUSnet.RaspberryPiFramework.Designer.Dictionaries.ViewModelMappingDict">

<DataTemplate x:Key="dtTest" x:DataType="vm:TestViewModel">
    <v:TestView />
</DataTemplate>

</ResourceDictionary>

我认为这里的问题在于,我必须定义一个密钥。虽然我发现了一些关于此的解释,但我不明白为什么现在必须这样做。 无论如何,如果我直接在ViewContainer中执行此操作,则映射有效:

<Page.Resources>
    <DataTemplate x:DataType="vm:TestViewModel">
        <v:TestView />
    </DataTemplate>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ContentControl 
        Content="{Binding CurrentContent}" />
</Grid>

问题是,找到的所有示例都是直接工作,没有显式的Mapping-Dictionary,或者使用像mvvmlight这样的Freamworks。 由于这更多是出于学习目的,我宁愿在没有将Binding-Mechanics黑框化为框架的情况下完成这项工作。

有可能,这只是ResourceDictionary中的一个Bug吗?我也发现了这个Thread How to associate view with viewmodel or multiple DataTemplates for ViewModel?,但根据我的理解,那里有关于运行时映射的问题,而我可以直接绑定。

0 个答案:

没有答案