모든 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를 제거하면 다시 정상적으로 작동합니다. 내부 예외 또는 추가 예외 사항은 없습니다. 이 디버깅/해결 방법은 무엇입니까?