无法使用 Prism 导航
Can't navigate using Prism
我无法使 Prism 中的导航正常工作。当我单击按钮转到相应视图时,没有任何反应。
这是男主视角 (Shell)XAML:
<Window x:Class="MVVMPractice2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
xmlns:Views="clr-namespace:MVVMPractice2.Views"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Margin="108,130,331.4,152.8" Content="View A" Command="{Binding NavigateCommand}" CommandParameter="ViewA"/>
<Button Margin="254,130,185.4,152.8" Content="View B" Command="{Binding NavigateCommand}" CommandParameter="ViewB"/>
<ContentControl prism:RegionManager.RegionName="ContentRegion"/> <!--PRISM POWER-->
</Grid>
</Window>
及其视图模型:
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);
}
}
和引导程序:
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>();
}
}
非常感谢您的帮助。
首先你应该将 ICommand
暴露给按钮的命令 属性 ,而不是委托命令 ICommand
的具体实现。
您可以通过实现摆脱视图模型定位器的约定
ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType)
在应用程序 class 中启动覆盖方法。
更多信息请搜索 Brian Lagunas viewmodellocator 博客
我通过仅在 UserControls 上使用 prism:ViewModelLocator.AutoWireViewModel="True"
而不是 window 使我的工作正常。我假设您使用的是 prism 6。
所以我所做的是,我首先创建了 MainWindow
来容纳我所有的 UserControls
,然后我创建了一个 MainUserControl
来容纳所有其他 UserControl。这一切都是我在 blog post (http://brianlagunas.com/getting-started-prisms-new-viewmodellocator/) 之后取得的。请记住创建您的 MVVM 文件夹(View 和 ViewModel)文件夹,并将其各自的内容作为博客亮点。
希望对您有所帮助。
我无法使 Prism 中的导航正常工作。当我单击按钮转到相应视图时,没有任何反应。
这是男主视角 (Shell)XAML:
<Window x:Class="MVVMPractice2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True"
xmlns:Views="clr-namespace:MVVMPractice2.Views"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Margin="108,130,331.4,152.8" Content="View A" Command="{Binding NavigateCommand}" CommandParameter="ViewA"/>
<Button Margin="254,130,185.4,152.8" Content="View B" Command="{Binding NavigateCommand}" CommandParameter="ViewB"/>
<ContentControl prism:RegionManager.RegionName="ContentRegion"/> <!--PRISM POWER-->
</Grid>
</Window>
及其视图模型:
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);
}
}
和引导程序:
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>();
}
}
非常感谢您的帮助。
首先你应该将 ICommand
暴露给按钮的命令 属性 ,而不是委托命令 ICommand
的具体实现。
您可以通过实现摆脱视图模型定位器的约定
ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType)
在应用程序 class 中启动覆盖方法。
更多信息请搜索 Brian Lagunas viewmodellocator 博客
我通过仅在 UserControls 上使用 prism:ViewModelLocator.AutoWireViewModel="True"
而不是 window 使我的工作正常。我假设您使用的是 prism 6。
所以我所做的是,我首先创建了 MainWindow
来容纳我所有的 UserControls
,然后我创建了一个 MainUserControl
来容纳所有其他 UserControl。这一切都是我在 blog post (http://brianlagunas.com/getting-started-prisms-new-viewmodellocator/) 之后取得的。请记住创建您的 MVVM 文件夹(View 和 ViewModel)文件夹,并将其各自的内容作为博客亮点。
希望对您有所帮助。