Why do I get a XamlParseException when I inherit a Custom UserControl in another project?

做~自己de王妃 提交于 2019-12-24 22:46:27

问题


In one project I have an Editor Class:

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

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>

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


回答1:


Try:

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



来源:https://stackoverflow.com/questions/1493572/why-do-i-get-a-xamlparseexception-when-i-inherit-a-custom-usercontrol-in-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!