У меня строго типизированный класс View, из которого происходят все мои пользовательские элементы управления. Это выглядит примерно так:
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 = True из разметки, он снова работает нормально. Нет внутренних или дополнительных подробностей исключений. Как я могу отладить / решить это?