如何在我的项目中重用xaml名称空间定义

本文关键字:xaml 空间 定义 我的 项目 | 更新日期: 2023-09-27 18:05:45

为了更详细地说明重用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>

如何在我的项目中重用xaml名称空间定义

正如Freeman所说,XAML继承是不可能的。无论如何,您可以考虑使用XmlnsDefinitionAttribute来减少和清理名称空间定义。

你可以在这里找到一篇有趣的文章。

实际上,如果您希望在XAML 中包含的名称空间位于引用的程序集中,则可以轻松地将它们映射到单个URI中。只需在引用的程序集中添加XmlnsDefinition属性,如下所示:
[assembly: XmlnsDefinition("urn:johannes-ui-controls", "XamlDemo.Foo")]
[assembly: XmlnsDefinition("urn:johannes-ui-controls", "XamlDemo.Bar")]

等等

在你的XAML中你可以这样使用它们:

<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>

此解决方案的限制是不能对包含XAML的程序集使用XmlnsDefinition属性。也许这不是你想要的,但也许它可以帮助你。

Base Control:

namespace WpfApplication9
{
    public class BaseControl : UserControl
    {
        public BaseControl()
        {
        }
    public override void EndInit()
    {
        base.EndInit();
        ExtTextBlock block = new ExtTextBlock { Width = 100 , Height = 20 , Text = "Test Block" };
        ExtButton button = new ExtButton { Width = 100, Height = 20 , Content = "ClickMe"};
        ExtLabel label = new ExtLabel { Width = 100, Height = 30 ,Content = "Test Label"};
        ExtTextBox txtBox = new ExtTextBox { Width = 100, Height = 20 ,Text= "Hi There"};
        Grid g = (Grid)BaseControl.FindChild(this, "gridMain");
        g.Children.Add(button);
        g.Children.Add(block);
        g.Children.Add(label);
        g.Children.Add(txtBox);
        Grid.SetRow(block, 0);
        Grid.SetRow(button, 1);
        Grid.SetRow(label, 2);
        Grid.SetRow(txtBox, 3);
        button.Click += button_Click;
    }
    void button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Hi There");
    }
    public static DependencyObject FindChild(DependencyObject parent, string name)
    {
        // confirm parent and name are valid.
        if (parent == null || string.IsNullOrEmpty(name)) return null;
        if (parent is FrameworkElement && (parent as FrameworkElement).Name == name) return parent;
        DependencyObject result = null;
        if (parent is FrameworkElement) (parent as FrameworkElement).ApplyTemplate();
        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            result = FindChild(child, name);
            if (result != null) break;
        }
        return result;
    }
    /// <summary>
    /// Looks for a child control within a parent by type
    /// </summary>
    public static T FindChild<T>(DependencyObject parent)
        where T : DependencyObject
    {
        // confirm parent is valid.
        if (parent == null) return null;
        if (parent is T) return parent as T;
        DependencyObject foundChild = null;
        if (parent is FrameworkElement) (parent as FrameworkElement).ApplyTemplate();
        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i);
            foundChild = FindChild<T>(child);
            if (foundChild != null) break;
        }
        return foundChild as T;
    }
}
}
namespace WpfApplication9.Foo
{
    public class ExtTextBlock : TextBlock { }
    public class ExtTextBox : TextBox { }
}
namespace WpfApplication9.Bar
{
    public class ExtButton : Button { }
    public class ExtLabel : Label { }
}

1的控制。xaml

<base:BaseControl x:Class="WpfApplication9.Control1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:base="clr-namespace:WpfApplication9"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid x:Name="gridMain">
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>
</Grid>

你可以像创建Control1一样创建Control2类。如我所说,xaml继承是不可能的