如何从 XAML 内部访问嵌套命名空间? [英] How to access nested namespace from inside XAML?
问题描述
我有一个包含 2 个项目的 WPF 应用程序,一个用于 ViewModels (MyApp.Core),另一个用于视图 (MyApp).
I have a WPF app with 2 projects, one for the ViewModels (MyApp.Core) and another for the Views (MyApp).
在视图和视图模型中,我有不同的嵌套命名空间(例如:MyApp.Core.ViewModels.Example1).
Inside the Views and ViewModels I have different nested namespaces (for example: MyApp.Core.ViewModels.Example1).
我想在 App.xaml 中注册我的视图和视图模型,并使用 DataTemplates 将视图模型绑定到视图.
I would like to register my Views and ViewModels in the App.xaml and use DataTemplates to bind ViewModels to 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>
如您所见,我为每个嵌套命名空间设置了一个命名空间,并且随着应用程序的增长,它会变得更大.
As you can see, I have one namespace for every nested namespace and this will get bigger as the app grows.
我的问题是:有没有办法只导入基本命名空间,然后在 XAMl 标签中指定更多?
我正在考虑这样的事情:
I'm thinking of something like this:
<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:类型标记扩展:
prefix
可选.映射非默认 XAML 命名空间的前缀.通常不需要指定前缀.见备注.
prefix
Optional. A prefix that maps a non-default XAML namespace. Specifying a prefix is frequently not necessary. See Remarks.
typeNameValue
必需.可解析为当前默认 XAML 命名空间的类型名称;或指定的映射前缀(如果提供了前缀).
typeNameValue
Required. A type name resolvable to the current default XAML namespace; or the specified mapped prefix if prefix is supplied.
快速测试表明 typeNameValue
中的复合名称实际上可以正常工作 - 因为它显然可以解析到命名空间前缀 - 尽管 XAML 设计器可能会抱怨嵌套类型不受支持.
A quick test reveals that a compound name in typeNameValue
actually just works - since it is obviously resolvable to the namespace prefix - although the XAML Designer may complain about a nested type not being supported.
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>
这篇关于如何从 XAML 内部访问嵌套命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!