簡體   English   中英

如何在項目中重用xaml命名空間定義

[英]How can I reuse xaml namespace definitions in my project

我重寫了這個問題,以更詳細地說明重用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>

基礎控制:

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無法實現。

正如Freeman所說,XAML繼承是不可能的。 無論如何,您可以考慮使用XmlnsDefinitionAttribute來減少和清除命名空間定義。

你可以發現一個有趣的文章在這里 CodeProject上。

實際上,如果要包含在XAML 的名稱空間位於引用的程序集中 ,則可以輕松地將它們映射到單個URI中。 只需以這種方式在引用的程序XmlnsDefinition添加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>

此解決方案的限制是您不能將XmlnsDefinition屬性與包含XAML的程序集一起使用。 可能這不是你的意思,但也許它可以幫助你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM