我重新修订了这个问题,以更详细地展示我所说的重用xaml定义的含义。
<UserControl x:Class="XamlDemo.ControlA"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:XamlDemo"
xmlns:foo="clr-namespace:XamlDemo.Foo"
xmlns:bar="clr-namespace:XamlDemo.Bar"
mc:Ignorable="d">
<!--
xmlns:foo="clr-namespace:XamlDemo.Foo"
xmlns:bar="clr-namespace:XamlDemo.Bar"
foo and bar will tend to repeat in exactly this constellation all over the project.
If one of these namespaces changes all xaml files need to be edited.
I would like to include a different file as a component where i would only write foo and bar once
-->
<StackPanel>
<foo:ExtTextBlock></foo:ExtTextBlock>
<bar:ExtLabel></bar:ExtLabel>
</StackPanel>
</UserControl>
<UserControl x:Class="XamlDemo.ControlB"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:XamlDemo"
xmlns:foo="clr-namespace:XamlDemo.Foo"
xmlns:bar="clr-namespace:XamlDemo.Bar"
mc:Ignorable="d" >
<StackPanel>
<foo:ExtTextBox></foo:ExtTextBox>
<bar:ExtButton></bar:ExtButton>
</StackPanel>
</UserControl>
使用
using System.Windows.Controls;
namespace XamlDemo.Bar
{
public class ExtButton : Button { }
public class ExtLabel : Label { }
}
namespace XamlDemo.Foo
{
public class ExtTextBlock : TextBlock { }
public class ExtTextBox : TextBox { }
}
两个都使用了我的本地命名空间声明。我希望它们能够包含对不同xaml的引用,并从那里获取命名空间。
我没有找到任何方法来实现这一点 - 这里有一些概念代码,说明了我想象中的样子。显然,这不会编译。
<magic
xmlns:foo="clr-namespace:XamlDemo.Foo"
xmlns:bar="clr-namespace:XamlDemo.Bar">
</magic>
<UserControl x:Class="..."
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns="get it from magic">
</UserControl>