问题描述
我无法在 Prism 中使用导航.当我点击按钮转到相应的视图时,什么也没有发生.
I can't get the navigation in Prism to work. When I click on the buttons to go to respective views, nothing happens.
这是 Man View (Shell) XAML:
This is the Man View (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>();
}
}
我希望得到一些帮助.
推荐答案
我只在 UserControls 上使用 prism:ViewModelLocator.AutoWireViewModel="True"
而不是在窗口上使用我的工作.我假设您使用的是棱镜 6.
I got mine to work by using the prism:ViewModelLocator.AutoWireViewModel="True"
on UserControls only and not the window. I am assuming that you are using prism 6.
所以我所做的是,我首先创建了 MainWindow
它将容纳我所有的 UserControls
,然后我创建了一个 MainUserControl
将容纳所有其他用户控件.我在这篇博客文章(http://brianlagunas.com/getting-started-prisms-new-viewmodellocator/).请记住创建您的 MVVM 文件夹(View 和 ViewModel)文件夹,其中包含各自的内容作为博客亮点.
So what I did is, I first created the MainWindow
that is going to house all my UserControls
, I then created a MainUserControl
that would house all the other UserControls. All this I achieved following this blog post (http://brianlagunas.com/getting-started-prisms-new-viewmodellocator/). Remember to create your MVVM folders (View and ViewModel) folders with their respective contents as the blog highlights.
希望这会有所帮助.
这篇关于无法使用棱镜导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!