Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 实例化在其他XAML文件中定义的对象_Wpf_Xaml - Fatal编程技术网

Wpf 实例化在其他XAML文件中定义的对象

Wpf 实例化在其他XAML文件中定义的对象,wpf,xaml,Wpf,Xaml,我有一个类似以下内容的XAML文件: <!-- File1.xaml --> <m:SomeName xmlns:m="clr-namespace:SomeNamespace"> ... </m:SomeName> 有什么想法吗?如果您想对该对象使用XAML,那么该对象需要扩展DependencyObject。最简单的方法是创建用户控件: <UserControl x:Class="SomeNamespace.SomeName"

我有一个类似以下内容的XAML文件:

<!-- File1.xaml -->
<m:SomeName xmlns:m="clr-namespace:SomeNamespace">
    ...
</m:SomeName>

有什么想法吗?

如果您想对该对象使用
XAML
,那么该对象需要扩展
DependencyObject
。最简单的方法是创建
用户控件

<UserControl
    x:Class="SomeNamespace.SomeName"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <!-- Other XAML content -->

</UserControl>
然后,您可以在应用程序的其他部分实例化控件:

<m:SomeOtherName xmlns:m="clr-namespace:SomeNamespace">
   <m:SomeOtherName.Property>
       <someNamespace:SomeName />
   </m:SomeOtherName.Property>
</m:SomeOtherName>

您应该在
App.Xaml
文件中将此共享对象创建为
资源
。那么这两个文件中都会出现相同的对象

namespace SomeNamespace
{
    public sealed partial class SomeName : UserControl
    {
        public SomeName()
        {
            this.InitializeComponent();
        }
    }
}
<m:SomeOtherName xmlns:m="clr-namespace:SomeNamespace">
   <m:SomeOtherName.Property>
       <someNamespace:SomeName />
   </m:SomeOtherName.Property>
</m:SomeOtherName>