【问题标题】:Exception while setting DataContext设置 DataContext 时出现异常
【发布时间】:2014-04-22 16:29:38
【问题描述】:

我创建了一个自定义 UserControl,它公开了一些 DependencyProperties,例如 HeaderTitle 和 HeaderTitleForeground

public partial class PageHeaderControl : UserControl
{
    public string HeaderTitle 
    {
        get { return (string)GetValue(HeaderTitleProperty); }
        set { SetValue(HeaderTitleProperty, value); }
    }

    public static readonly DependencyProperty HeaderTitleProperty = DependencyProperty.Register("HeaderTitle", typeof(string), typeof(PageHeaderControl), new PropertyMetadata(""));

    public string HeaderTitleForeground
    {
        get { return (string)GetValue(HeaderTitleForegroundProperty); }
        set { SetValue(HeaderTitleForegroundProperty, value); }
    }

    public static readonly DependencyProperty HeaderTitleForegroundProperty = DependencyProperty.Register("HeaderTitleForeground", typeof(string), typeof(PageHeaderControl), new PropertyMetadata(""));

    public PageHeaderControl()
    {
        InitializeComponent();
        (this.Content as FrameworkElement).DataContext = this;
    }
}

但是当我调试我的应用程序时,它会抛出一个异常,如下所示:

  System.Exception occurred
   _HResult=-2146233088
   _message=Error HRESULT E_FAIL has been returned from a call to a COM component.
   HResult=-2146233088
   Message=Error HRESULT E_FAIL has been returned from a call to a COM component.
   Source=System.Windows
   StackTrace:
      at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
   InnerException: 

但是自定义控件是正确绘制的。那么,我该如何解决呢?这是一个关键问题吗?

【问题讨论】:

    标签: c# xaml user-controls windows-phone


    【解决方案1】:

    您的班级是 PageHeaderControl,但您的依赖道具已注册为 ElevationPageHeaderControl 作为其所有者..?

    【讨论】:

    • 那是一个错字。谢谢你的评论。我刚刚在下面回答了我的问题。
    【解决方案2】:

    我发现了我的错误。它是 HeaderTitleForeground 的类型,所以我只是从字符串更改为 SolidColorBrush 并将 SolidColorBrush(Colors.Black) 添加到 PropertyMetadata。这是 UserControl 的固定版本:

    public partial class PageHeaderControl : UserControl
    {
    public string HeaderTitle 
    {
        get { return (string)GetValue(HeaderTitleProperty); }
        set { SetValue(HeaderTitleProperty, value); }
    }
    
    public static readonly DependencyProperty HeaderTitleProperty = DependencyProperty.Register("HeaderTitle", typeof(string), typeof(PageHeaderControl), new PropertyMetadata(""));
    
    public SolidColorBrush HeaderTitleForeground
    {
        get { return (SolidColorBrush)GetValue(HeaderTitleForegroundProperty); }
        set { SetValue(HeaderTitleForegroundProperty, value); }
    }
    
    public static readonly DependencyProperty HeaderTitleForegroundProperty = DependencyProperty.Register("HeaderTitleForeground", typeof(SolidColorBrush), typeof(PageHeaderControl), new PropertyMetadata(new SolidColorBrush(Colors.Black)));
    
    public PageHeaderControl()
    {
        InitializeComponent();
        (this.Content as FrameworkElement).DataContext = this;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 2013-09-02
      • 2021-06-29
      • 2017-09-21
      • 2018-10-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多