【问题标题】:Silverlight custom control inheritance issueSilverlight 自定义控件继承问题
【发布时间】:2011-12-05 16:00:35
【问题描述】:

所以,我在 SL4 中有以下控制...

XAML:

<Canvas x:Class="LineAnnotation"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Canvas.Left="0" Canvas.Top="0" Loaded="Canvas_Loaded"  >
</Canvas>

代码隐藏:

public partial class LineAnnotation : Canvas
    {


        public LineAnnotation()
        {
            InitializeComponent();

        }


        void Canvas_Loaded(object sender, RoutedEventArgs e)
        {
            this.Height = ((Canvas)this.Parent).Height;
            this.Width = ((Canvas)this.Parent).Width;
        }
    }

这个类工作正常,我可以实例化并使用它。但是,我有另一个控件可以继承这个控件:

public class ArrowAnnotation : LineAnnotation
{

    public ArrowAnnotation() : base() 
    {
        // Some other init stuff
    }

} 

当我尝试在我的代码中实例化其中之一时,我得到以下异常:

System.Windows.Markup.XamlParseException: Failed to assign to property 'System.Windows.FrameworkElement.Loaded'. [Line: 5 Position: 47]
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at LineAnnotation.InitializeComponent()
   at LineAnnotation..ctor()

不只是 Loaded 事件,它可以是任何事件处理程序,我得到相同的异常。此代码恰好与 WPF 项目共享,并且工作正常。知道这里有什么问题吗?

编辑:我意识到 ArrowAnnotation 不必是部分类,因为没有 xaml。改变这一点没有任何区别。

【问题讨论】:

    标签: silverlight inheritance user-controls


    【解决方案1】:

    看来你不能这样做。您可以在代码隐藏中连接处理程序,也可以在子类中实现处理程序。

    http://forums.silverlight.net/t/148606.aspx/1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-16
      • 2011-06-25
      • 1970-01-01
      • 2012-12-17
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      相关资源
      最近更新 更多