I reworked this question to show in more detail what I mean by reusing xaml definitions.
<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>
with
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 { }
}
Both use my local namespace declarations. I would like them to instead include a reference to a different xaml and get the namespaces from there
I did not find any way of doing this - here is some concept code that illustrates what I imagine this could look like. Obviously this would not compile.
<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>
As stated by Freeman, XAML inheritance is not possible. Anyway you can consider using XmlnsDefinitionAttribute for reducing and cleaning the namespace definitions.
You can find an interesting article here on CodeProject.
Pratically if the namespaces you want to include in your XAML are in a refereced assembly, you can easily map them in a single URI. Just add in the referenced assembly the XmlnsDefinition attribute in this way:
[assembly: XmlnsDefinition("urn:johannes-ui-controls", "XamlDemo.Foo")]
[assembly: XmlnsDefinition("urn:johannes-ui-controls", "XamlDemo.Bar")]
and so on.
Then in your XAML you can use them in this way:
<UserControl x:Class="..."
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:uiControls="urn:johannes-ui-controls">
<StackPanel>
<uiControls:ExtTextBox />
<uiControls:ExtButton />
</StackPanel>
</UserControl>
The limit of this solution is that you cannot use the XmlnsDefinition attribute with the assembly which contains your XAML.
Probably this is not exaclty what you meant, but maybe it can help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With