首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF UserControl InitializeComponent异常

WPF UserControl InitializeComponent异常
EN

Stack Overflow用户
提问于 2012-02-12 09:50:10
回答 1查看 2.4K关注 0票数 2

我有一个强类型的视图类,我的所有UserControls都派生自这个类。它看起来或多或少像这样:

代码语言:javascript
复制
public class View<TContext> : UserControl 
{

    /// <summary>
        /// Gets or sets a value indicating whether to auto create the data context type.
        /// </summary>
    public static DependencyProperty AutoCreateDataContextProperty = DependencyProperty.Register("AutoCreateDataContext", typeof(bool), typeof(View<TContext>), new PropertyMetadata(false));
    /// <summary>
    /// Gets or sets a value indicating whether to auto create the data context type.
    /// </summary>
    /// <value>
    ///     <c>true</c> if [auto resolve data context]; otherwise, <c>false</c>.
    /// </value>
    public bool AutoCreateDataContext
    {
        get { return (bool)GetValue(AutoCreateDataContextProperty); }
        set { SetValue(AutoCreateDataContextProperty, value); }
    }

    /// <summary>
    /// Gets or sets the view model.
    /// </summary>
    /// <value>
    /// The view model.
    /// </value>
    public new TContext DataContext
    {
        get
        {
            if (AutoCreateDataContext && !DesignerProperties.GetIsInDesignMode(new ContentControl()))
            {
                base.DataContext = ServiceProvider.Current.GetService<TContext>();
            }
            return (TContext)base.DataContext;
        }
        set { base.DataContext = value; }
    }
}

关于AutoCreateDataContext的部分是new...and是我问题的来源。在我的一个派生视图中将该值设置为true后,将其添加到View<TContext>基类中并没有引起任何问题: itself...but:

代码语言:javascript
复制
<s:View x:TypeArguments="local:PersonSearchViewModel"
    x:Class="PersonSearchView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ...
             Height="600" Width="800" Background="White" AutoCreateDataContext="True">

此视图的InitializeComponent抛出以下异常:

代码语言:javascript
复制
System.NullReferenceException occurred
  Message=Object reference not set to an instance of an object.
  Source=PresentationFramework
  StackTrace:
       at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector)
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
       at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
       at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       at ....

一旦我从标记中删除了AutoCreateDataContext=True,它又可以正常工作了。没有内部异常或更多异常详细信息。如何调试/解决此问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-02-12 10:21:35

我做了一些猜测,然后进行了反汇编,发现它是WPF处理泛型DependencyObjects (如我的View<T>)上声明的DependencyProperties的错误。

创建了一个抽象的非泛型基类(称为View,which View<T> now inherits from),并在那里声明了我的DependencyProperties。问题解决了。

我想我已经习惯了微软质量有多差的is...so,实际上我已经开始意识到这类bug的趋势。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9245734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档