WPF UserControl InitializeComponent异常 [英] WPF UserControl InitializeComponent Exception
问题描述
我有一个强烈类型化的View类,我所有的UserControl都派生自该类.看起来或多或少是这样的:
I have a strongly typed View class that all my UserControls derive from. It looks more or less like this:
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>
基类本身并不会引起任何问题...但是一旦我在派生的Views中将其值设置为true
The bit about AutoCreateDataContext is new...and is the source of my question. Adding this to the View<TContext>
base class hasn't caused any problems itself...but once I set the value to true in one of my derived Views:
<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引发以下异常:
InitializeComponent for this view throws the following exception:
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,它就会再次正常运行.没有内部异常或进一步的异常详细信息.我该如何调试/解决这个问题?
As soon as I remove the AutoCreateDataContext=True from the markup, it works fine again. There is no inner exception or further exception detail. How can I debug/resolve this?
推荐答案
我进行了一些猜测,然后进行了分解,发现这是WPF如何处理在通用DependencyObjects上声明的DependencyProperties的错误(例如,我的 View< T>
).
I did some guessing and then disassembling and found it to be a bug in how WPF handles DependencyProperties declared on generic DependencyObjects (like my View<T>
).
制作了一个抽象的非通用基类(称为View, View< T>现在从中继承
),并在那里声明了我的DependencyProperties.问题解决了.
Made an abstract non-generic base class (called View, which View<T> now inherits from
) and declared my DependencyProperties there instead. Problem solved.
我想我已经习惯了Microsoft的质量有多么差...所以我实际上已经开始认识到这种bug的趋势.
I guess I've gotten used to just how poor Microsoft quality is...so I've actually started recognizing trends in bugs like this.
这篇关于WPF UserControl InitializeComponent异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!