2012-02-12 141 views
1

我有我所有的用户控件派生自一个强类型的视图类。它看起来或多或少是这样的:WPF用户控件的InitializeComponent异常

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位是新的......并且是我的问题的根源。添加此向View<TContext>基类并没有引起任何问题本身......但一旦我将该值设置为true在我的派生画面之一:

<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对这一观点引发以下异常:

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 =从标记真,它再次工作正常。没有内部异常或其他异常细节。我该如何调试/解决这个问题?

回答

2

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

制造一个抽象的非通用基础类(称为视图,which View<T> now inherits from),并宣布我DependencyProperties有代替。问题解决了。

我想我已经习惯了微软的品质是多么的可怜......所以其实我已经开始在这样的错误认识趋势。