c# – WPF,MVVM和PRISM – 为此对象定义的无参数构造函数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – WPF,MVVM和PRISM – 为此对象定义的无参数构造函数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3787字,纯文字阅读大概需要6分钟。
内容图文
![c# – WPF,MVVM和PRISM – 为此对象定义的无参数构造函数](/upload/InfoBanner/zyjiaocheng/806/d51cdd2e295f4e70a1fc94efb6adac24.jpg)
回答
好的,所以添加E-Bat给出的建议代码没有任何影响,直到我开始一个新项目并逐字复制所有代码.我只能假设在http://prismlibrary.com/上的ViewModelLocator中必须有一些后台代码,它们没有更新以考虑无参数构造函数.希望这可以帮助其他人解决同样的问题
原始问题
我使用棱镜设置了一个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>
MainWindowViewModel
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的其余部分则不然.我假设这是错误的来源.
HeaderViewModel
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传递给我的ViewModel,我怎么能解决这个错误呢?
我是否需要通过覆盖ConfigureContainer方法在Bootstrap中注册?如果是这样,我不完全确定如何做到这一点.
引导
class Bootstraper: UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void InitializeShell()
{
Application.Current.MainWindow.Show();
}
}
应用程序构建成功并成功运行,但只是在尝试查看设计器中的MainWindow时,我收到此消息.
任何帮助将不胜感激.
编辑
我的所有视图构造函数都只有initalizeComponent方法,并且不带参数
解决方法:
您的视图正在尝试执行仅在运行时有意义的逻辑,因此您需要确保您不处于设计模式:
public HeaderView()
{
if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
var svc = ServiceLocator.Current;
var eventAggregator = svc.GetInstance<IEventAggregator>();
this.DataContext = new HeaderViewModel(eventAggregator);
}
InitializeComponent();
}
编辑:
对于设计时视图模型的支持看看here
基本上,您需要为ViewModel提供无参数构造函数以支持设计模式.
内容总结
以上是互联网集市为您收集整理的c# – WPF,MVVM和PRISM – 为此对象定义的无参数构造函数全部内容,希望文章能够帮你解决c# – WPF,MVVM和PRISM – 为此对象定义的无参数构造函数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。