这我得到:型“System.NullReferenceException”的第一次机会异常出现在PresentationFramework.dll进样视图模型与数据抛出异常
当我使用的参数为我LessonPlannerViewModel类的构造函数。
我使用datatemplateselector类在每周/每日视图之间切换。
public class ApplicationNavigationTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item is LessonPlannerViewModel)
{
var vm = item as LessonPlannerViewModel;
Window window = Application.Current.MainWindow;
if (vm.IsDailyView)
return window.FindResource("dailyViewTemplate") as DataTemplate;
else
return window.FindResource("weeklyViewTemplate") as DataTemplate;
}
return base.SelectTemplate(item, container);
}
}
public LessonPlannerViewModel(DateTime asOfDate)
{
_asOfDate = asOfDate;
if(_isDailyView)
LoadDailyData();
if(_isWeeklyView)
LoadWeeklyData();
...
是不允许的?没有参数,我不会例外...
我该怎么做?
编辑:我现在改变的参数为整数,并得到更好的消息,P
XamlParseException => “没有在类型中找到匹配的构造‘TBM.ViewModel.LessonPlannerViewModel’。您可以使用Arguments或FactoryMethod指令来构造这种类型。'行号“13”和行位置“10”。
确定这是目前被理解,
<UserControl.Resources>
<ViewModel:LessonPlannerViewModel x:Key="LessonPlannerViewModelID" />
</UserControl.Resources>
没有参数。
那么现在该怎么做?
我已经使用laurent's框架。但是我看到我的加载策略需要改变,比如在创建类对象时运行Init方法来加载数据。 – msfanboy 2010-04-28 17:15:36
我也创造了这个经验,它最好能够清理构造函数或者只是注册消息。您可以使用具有抽象方法的抽象基类来进行初始化,激活,重置,刷新和viewmodel操作。 – CodeWeasel 2010-04-29 06:38:30
啊谁在乎......呃akzeptier das als Antwort XD – msfanboy 2010-05-02 09:26:31