本文介绍了当我在另一个项目中继承自定义UserControl时,为什么会收到XamlParseException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个项目中,我有一个编辑器类:

In one project I have an Editor Class:

namespace TestXamlInherit234
{
    public class CustomerEditor : BaseEditor
    {
        public CustomerEditor()
        {
            TheMessage.Text = "changed222";
        }
    }
}

继承自另一个项目中的WPF用户控件:

which inherits from a WPF User Control in another project:

using System.Windows.Controls;

namespace Core
{
    public partial class BaseEditor : UserControl
    {
        public TextBlock TheMessage
        {
            get
            {
                return TheMessage2;
            }
        }

        public BaseEditor()
        {
            InitializeComponent();

        }
    }
}


<UserControl x:Class="Core.BaseEditor"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid>
        <TextBlock x:Name="TheMessage2" Text="This is in the base editor"/>
    </Grid>
</UserControl>

当两个类都在同一个项目中时,此方法有效,但是当它们在两个不同项目中时,出现 XamlParseException 错误.

This works when both classes are in the same project but when they are in two different projects, I get a XamlParseException error.

推荐答案

尝试:

<Core:BaseEditor x:Class="TestXamlInherit234.CustomerEditor"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Core="yourcorenamespace"
    Height="300" Width="300">
    <Grid>
        <TextBlock x:Name="TheMessage2" Text="This is in the base editor"/>
    </Grid>
</Core:BaseEditor>

WPF对继承任何类型的UserControl的支持非常有限.当我这样做是为了解决缺少泛型支持的问题时,我不得不在代码中定义控件并从ContentControl派生.

WPF's support for inheriting any kind of UserControls is very limited. When I did this to work around the lack of generics support I had to define my control in code and derive from ContentControl.

这篇关于当我在另一个项目中继承自定义UserControl时,为什么会收到XamlParseException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 06:44