Can't navigate using Prism

2019-09-09 11:57发布

问题:

I can't get the navigation in Prism to work. When I click on the buttons to go to respective views, nothing happens.

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>

and its 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);
    }
}

and Bootstrapper:

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>();
    }
}

I would appreciate some help.

回答1:

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.

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.

Hope this helps.



回答2:

First of all you should expose ICommand to button's command property , not delegate command which is concrete implementation of ICommand.

You can get rid of conventions of view model locator by implementing ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) in application class startup overriden method.

For more info please search Brian Lagunas viewmodellocator blog



标签: c# mvvm Prism