【发布时间】:2009-09-30 15:03:01
【问题描述】:
(这个问题是对my other question on this topic 的改进,有点复杂,但我真的很想了解为什么这不起作用,所以这里有一个更直接的例子)
我想做什么:在一个项目中创建一个简单的类(无 XAML),该类继承另一个项目中的 WPF UserControl(使用 XAML)。
这是我在主项目中的继承类:
EmployeeEditor.cs:
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using CoreApp;
namespace TestInheritUserControl234
{
class EmployeeEditor : BaseEditor
{
public EmployeeEditor()
{
TextBlock tb = new TextBlock();
tb.Text = "This was added in EmployeeEditor";
tb.Foreground = new SolidColorBrush(Colors.Blue);
EditorContent.Children.Add(tb);
}
}
}
这是我在第二个项目中的基类,请注意它有 x:Name="EditorContent":
BaseEditor.xaml:
<UserControl x:Class="CoreApp.BaseEditor"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel x:Name="EditorContent" Margin="5">
<TextBlock x:Name="TheMessage" Text="This was added in BaseEditor XAML."/>
</StackPanel>
</UserControl>
BaseEditor.xaml.cs:
using System.Windows.Controls;
namespace CoreApp
{
public partial class BaseEditor : UserControl
{
public BaseEditor()
{
InitializeComponent();
TextBlock tb = new TextBlock();
tb.Text = "This was added in BaseEditor code-behind.";
EditorContent.Children.Add(tb);
}
}
}
问题是当它运行时,我收到错误名称“EditorContent”在当前上下文中不存在。
但是,如果所有类都在一个项目中,它运行良好。
为什么您可以跨项目边界继承一个类,但不能继承其附加的 XAML?
【问题讨论】:
标签: c# wpf xaml inheritance user-controls