2012-02-12 5 views
1

모든 UserControls가 파생 된 강력한 형식의 View 클래스가 있습니다. AutoCreateDataContext에 대한 비트가 새로운WPF UserControl 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; } 
    } 
} 

... 그리고 내 질문의 근원이다 : 그것은 더 많거나 적은 같은 보인다. 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 = True를 제거하면 다시 정상적으로 작동합니다. 내부 예외 또는 추가 예외 사항은 없습니다. 이 디버깅/해결 방법은 무엇입니까?

답변

2

나는 추측을 한 다음 해체하여 WPF에서 제네릭 DependencyObjects (예 : View<T>)에 선언 된 DependencyProperties를 처리하는 방법에 버그가 있음을 발견했습니다.

뷰가 아닌 일반 기본 클래스 (View, which View<T> now inherits from)를 만들고 거기에 대신 내 DependencyProperties를 선언했습니다. 문제 해결됨.

나는 마이크로 소프트의 품질이 얼마나 나쁜지 익숙해 졌을 것입니다. 그래서 실제로 이런 버그의 경향을 인식하기 시작했습니다.