我将在WinRT应用程序中使用Caliburn.Micro。
这是我的主要VM:
public class MainViewModel : Conductor<Screen>
{
protected override void OnActivate()
{
if (ActiveItem == null)
{
ActivateItem(
ViewModelLocator.LocateForViewType(typeof(NewsFeedView)) as Screen);
}
base.OnActivate();
}
}
我在这里使用 Conductor 是因为我想要在 ContentControl 中加载不同的控件,但现在我只有这段代码。以下是我的主视图中的内容控件:
<ContentControl x:Name="ActiveItem" Grid.Column="1" Grid.Row="1" />
当我运行应用程序时,一切都正常,
MainViewModel.Activate
被调用,并将ActiveItem
设置为NewsFeedViewModel
,ContentControl
加载NewsFeedView
。问题是:当我在
NewsFeedView
控件中使用NavigationService.NavigateToViewModel
方法导航到另一个视图,然后在该视图中使用NavigationService.GoBack
,返回到MainView
时,在调用MainViewModel.Activate
时,ActiveItem
不是null
,但ContentControl.Content
是null
。 我尝试使用ContentControl
的View.Model
附加属性,但没有成功,如何重新绑定?编辑:最后,我在Caliburn中设置记录器以查看发生了什么,并找到了一个错误-当在导航回来后加载MainView时,会出现以下事件:
Attaching ***.Views.MainView to ***.ViewModels.MainViewModel.
ViewModel bound on ActiveItem.
Using cached view for ***.ViewModels.NewsFeedViewModel.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Exception: Unspecified error
at Windows.UI.Xaml.Controls.ContentControl.put_Content(Object value)
... some winRT stack
at Caliburn.Micro.View.SetContentPropertyCore(...
虽然信息不是很详细,但我使用了InteliTrace来获取更多信息,并得到了这个消息:“元素已经是另一个元素的子元素”。我想NewsFeedView在某个地方被存储了起来,当将其放入ContentControl中时就会抛出此异常。 如何解决这个问题?