【发布时间】:2016-12-21 14:02:36
【问题描述】:
回答 好的,所以添加 E-Bat 给出的建议代码没有任何影响,直到我开始一个新项目并逐字复制所有代码。我只能假设http://prismlibrary.com/ 上的 ViewModelLocator 中必须有一些背景代码,这些代码没有更新以考虑无参数构造函数。希望这可以帮助其他有同样问题的人
原始问题 我已经使用 prism 建立了一个 MVVM 项目。我有一个 MainWindow.xaml 和 5 个视图;我正在使用的 ButtonsView、HeaderView、ProcessInputView、ProcessLogView 和 ProcessSelectionView,每个 View 都有一个关联的 ViewModel。
MainWindow.xaml
<Window x:Class="TransactionAutomationTool.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TransactionAutomationTool"
xmlns:views="clr-namespace:TransactionAutomationTool.Views"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="800">
<Grid>
<views:HeaderView x:Name="HeaderViewControl" Margin="20,21,0,0" />
<views:ProcessSelectionView x:Name="ProcessSelectionViewControl" Margin="20,119,0,0" />
<views:ProcessInputView x:Name="ProcessInputViewControl" Margin="20,280,0,0" />
<views:ProcessLogView x:Name="ProcessLogView" Margin="298,105,0,0" />
<views:ButtonsView x:Name="ButtonViewControl" Margin="0,513,0,0" />
</Grid>
主窗口视图模型
public class MainWindowViewModel: BindableBase
{
public IEventAggregator _events;
private UserPrincipal userPrincipal;
public MainWindowViewModel(IEventAggregator events)
{
_events = events;
userPrincipal = UserPrincipal.Current;
_events.GetEvent<HeaderLoaded>().Subscribe(HeaderHasBeenLoaded);
}
private void HeaderHasBeenLoaded()
{
_events.GetEvent<UserNameUpdate>().Publish(string.Format("{0} {1}", userPrincipal.GivenName, userPrincipal.Surname));
}
}
当我尝试在设计模式下查看 MainWindow 时,出现以下错误 Screenshot of MainWindow In design Mode
找不到此对象的无参数构造函数 - 这突出显示了 HeaderView 和 ButtonsView
HeaderViewModel 和 ButtonsViewModel 都将 IEventAggregator 作为其构造函数中的参数,而其余的 ViewModel 则没有。我假设这是错误的来源。
页眉视图模型
public class HeaderViewModel: BindableBase
{
private string userName;
private string runTime;
public string UserName
{
get { return userName; }
set { SetProperty(ref userName, value); }
}
public string RunTime
{
get { return runTime; }
set { SetProperty(ref runTime, value); }
}
public HeaderViewModel(IEventAggregator events)
{
events.GetEvent<RunTimeUpdate>().Subscribe(RunTimeUpdated);
events.GetEvent<UserNameUpdate>().Subscribe(UserNameUpdated);
events.GetEvent<HeaderLoaded>().Publish();
}
private void RunTimeUpdated(string newRunTime)
{
RunTime = newRunTime;
}
private void UserNameUpdated(string userName)
{
UserName = userName;
}
}
如果我需要订阅这些事件并因此需要将 IEventAggregator 传递到我的 ViewModels 中,我该如何解决这个错误?
我是否需要通过重写 ConfigureContainer 方法在 Bootstrap 中注册它?如果是这样,我不完全确定该怎么做。
引导
class Bootstraper: UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void InitializeShell()
{
Application.Current.MainWindow.Show();
}
}
应用程序成功构建并成功运行,但只是在我尝试在设计器中查看 MainWindow 时收到此消息。
任何帮助将不胜感激。 编辑 我所有的视图构造函数都只有 initalizeComponent 方法并且不带参数
【问题讨论】:
-
在处理 mvvm 项目时,我经常与 xaml 设计器发生这种崩溃。只要您可以毫无问题地运行应用程序并且您的测试通过,就没有问题。我相信问题出在 xaml 设计师身上。我仍然对有关此问题的任何答案感兴趣
-
我觉得它运行正常,但我团队的其他成员可能对此不满意。只是想看看是否有解决方案。正如@E-Bat 所述,我之前已经看过 DesignerProperties.GetIsInDesignMode 代码,但它没有影响