简体   繁体   中英

Can't navigate using Prism

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM