C# Can';不要使用棱镜导航
我无法让Prism中的导航正常工作。当我点击按钮进入各自的视图时,什么都没有发生 这是Man View(Shell)XAML:C# Can';不要使用棱镜导航,c#,mvvm,prism,C#,Mvvm,Prism,我无法让Prism中的导航正常工作。当我点击按钮进入各自的视图时,什么都没有发生 这是Man View(Shell)XAML: 及其ViewModel: public class MainWindowViewModel : BindableBase { private readonly IRegionManager regionManager; //PRISM POWER public DelegateCommand<string> NavigateComman
及其ViewModel:
public class MainWindowViewModel : BindableBase
{
private readonly IRegionManager regionManager; //PRISM POWER
public DelegateCommand<string> NavigateCommand { get; set; }
public MainWindowViewModel(IRegionManager regionManager)
{
this.regionManager = regionManager;
NavigateCommand = new DelegateCommand<string>(Navigate);
}
private void Navigate(string uri)
{
regionManager.RequestNavigate("ContentRegion", uri);
}
}
公共类MainWindowViewModel:BindableBase
{
专用只读IRegionManager regionManager;//PRISM POWER
公共DelegateCommand导航命令{get;set;}
公共主窗口视图模型(IRegionManager regionManager)
{
this.regionManager=regionManager;
NavigateCommand=新的DelegateCommand(导航);
}
私有void导航(字符串uri)
{
RequestNavigate(“ContentRegion”,uri);
}
}
和引导程序:
public class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void InitializeShell()
{
Application.Current.MainWindow.Show();
}
protected override void ConfigureContainer()
{
base.ConfigureContainer();
Container.RegisterType(typeof(object), typeof(ViewA), "ViewA");
Container.RegisterType(typeof(object), typeof(ViewB), "ViewB");
Container.RegisterType<ICustomer, Customer>();
}
}
公共类引导程序:UnityBootstrapper
{
受保护的覆盖依赖对象CreateShell()
{
返回Container.Resolve();
}
受保护的覆盖无效初始值设置Shell()
{
Application.Current.MainWindow.Show();
}
受保护的覆盖无效配置容器()
{
base.ConfigureContainer();
RegisterType(typeof(object)、typeof(ViewA)、“ViewA”);
RegisterType(typeof(object),typeof(ViewB),“ViewB”);
Container.RegisterType();
}
}
我将非常感谢您的帮助。首先,您应该将
ICommand
公开给button的command属性,而不是委托命令,它是ICommand
的具体实现
通过实现,可以摆脱视图模型定位器的约定
ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType)
在应用程序类启动重写方法中
有关更多信息,请搜索Brian Lagunas viewmodellocator博客我是通过仅对用户控件而非窗口使用
prism:viewmodellocator.AutoWireViewModel=“True”
来实现的。我假设您使用的是prism 6
所以我做的是,我首先创建了main窗口
,它将容纳我所有的UserControls
,然后我创建了一个MainUserControl
,它将容纳所有其他的UserControls。所有这些我都是在这之后实现的()。记住创建你的MVVM文件夹(视图和视图模型)包含博客突出显示的各自内容的文件夹
希望这能有所帮助。我没有使用视图模型定位器is Prism,但它是否因为找不到视图模型而陷入困境?我有ViewA和ViewB的VIEWMODEL。如果您想尝试
RequestNavigate(“ContentRegion”,“ViewA”),我可以将它们包含在这里
?顺便说一句,使用RegisterTypeForNavigation()
注册视图以进行导航更容易。我猜您的ViewModel无法由ViewModel定位器解析。可能有几个原因:名称空间与预期模式不匹配(它们应该是MVVMPractice2.Views
和MVVMPractice2.ViewModels
,如果它们不同,您必须自己配置或使用默认约定。如果ViewModels位于不同的程序集中,您还需要手动配置。最后一个似乎不适用于您的是视图名称=ViewModel约定(Prism 6对此约定进行了更改,但前提是视图名为SomethingView
哪些名称空间是MainWindow
和MainWindowViewModel
中的?应该分别是Views
和ViewModels
。谁投了否决票,请给我一个我错的原因,也许是Brian La。)gunas可以对itOkay发表评论,如果我删除链接并用实际答案替换它,你会删除否决票吗?因为我是新的,我不知道链接不能被发布为什么即使在我删除链接后它也会被否决真的不理解
public class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
{
return Container.Resolve<MainWindow>();
}
protected override void InitializeShell()
{
Application.Current.MainWindow.Show();
}
protected override void ConfigureContainer()
{
base.ConfigureContainer();
Container.RegisterType(typeof(object), typeof(ViewA), "ViewA");
Container.RegisterType(typeof(object), typeof(ViewB), "ViewB");
Container.RegisterType<ICustomer, Customer>();
}
}