繁体   English   中英

如何从 XAML 内部访问嵌套命名空间?

[英]How to access nested namespace from inside XAML?

我有一个带有 2 个项目的 WPF 应用程序,一个用于 ViewModels (MyApp.Core),另一个用于 Views (MyApp)。

在 Views 和 ViewModels 中,我有不同的嵌套命名空间(例如:MyApp.Core.ViewModels.Example1)。

我想在 App.xaml 中注册我的 Views 和 ViewModels 并使用 DataTemplates 将 ViewModels 绑定到 Views。

我目前拥有的是这样的:

<Application
   x:Class="MyApp"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:example1v="clr-namespace:MyApp.Views.Example1"
   xmlns:example1vm="clr-namespace:MyApp.Core.ViewModels.Example1;assembly=MyApp.Core"
   xmlns:example2v="clr-namespace:MyApp.Views.Example1"
   xmlns:example2vm="clr-namespace:MyApp.Core.ViewModels.Example1;assembly=MyApp.Core"
   StartupUri="Views/MainWindow.xaml">

   <Application.Resources>
       <DataTemplate DataType="{x:Type example1vm:Example1ViewModel}">
           <example1v:Example1Window />
       </DataTemplate>
       <DataTemplate DataType="{x:Type example2vm:Example2ViewModel}">
           <example2v:Example2Window />
       </DataTemplate>
   </Application.Resources>
</Application>

正如你所看到的,我为每个嵌套的命名空间都有一个命名空间,随着应用程序的增长,它会变得更大。

我的问题是:有没有办法只导入基本命名空间,然后在 XAMl 标签中指定更多?

我在想这样的事情:

<Application
   x:Class="MyApp"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:v="clr-namespace:MyApp.Views"
   xmlns:vm="clr-namespace:MyApp.Core.ViewModels;assembly=MyApp.Core"
   StartupUri="Views/MainWindow.xaml">

   <Application.Resources>
       <DataTemplate DataType="{x:Type vm:Example1.Example1ViewModel}">
           <v:Example1.Example1Window />
       </DataTemplate>
       <DataTemplate DataType="{x:Type vm:Example2.Example2ViewModel}">
           <v:Example2.Example2Window />
       </DataTemplate>
   </Application.Resources>
</Application>

x:Type 标记扩展

<object property="{x:Type prefix:typeNameValue}".../>

prefix可选。 映射非默认 XAML 命名空间的前缀。 通常不需要指定前缀。 见备注。

typeNameValue必需。 可解析为当前默认 XAML 命名空间的类型名称; 如果提供了前缀,则为指定的映射前缀。

快速测试显示typeNameValue中的复合名称实际上只是工作 - 因为它显然可以解析为命名空间前缀 - 尽管 XAML 设计器可能会抱怨不支持嵌套类型。

namespace DataTypeNamespaceTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new Test.ViewModel();
        }
    }
}

namespace DataTypeNamespaceTest.Test
{
    public class ViewModel
    {
        public string Text { get; } = "Text in Test.ViewModel";
    }
}

XAML:

<Window x:Class="DataTypeNamespaceTest.MainWindow"
        xmlns:local="clr-namespace:DataTypeNamespaceTest"
        ...>
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:Test.ViewModel}">
            <TextBlock Padding="10" Background="Aqua" Text="{Binding Text}"/>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <ContentControl Content="{Binding}"/>
    </Grid>
</Window>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM