【问题标题】:Can't navigate using Prism无法使用 Prism 导航
【发布时间】:2016-03-07 14:52:02
【问题描述】:

我无法让 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>

及其 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);
    }
}

和引导程序:

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

我将不胜感激。

【问题讨论】:

  • 我没有使用 Prism 的视图模型定位器,但它是否因为无法为您的视图找到视图模型而苦苦挣扎?
  • 我有 ViewA 和 ViewB 的 ViewModel。如果你愿意,我可以在这里加入他们
  • 你试过RequestNavigate("ContentRegion", "ViewA")吗?顺便说一句,使用RegisterTypeForNavigation&lt;ViewType&gt;()注册导航视图更容易...
  • 我的猜测是 ViewModel 定位器无法解析您的 ViewModel。可能有几个原因: 命名空间与预期的模式不匹配(它们应该是 MVVMPractice2.ViewsMVVMPractice2.ViewModels,如果它们不同,你必须自己配置它或使用默认约定。如果 ViewModels 在不同的程序集中,你也需要进行手动配置。最后一个似乎不适用于您的是 View name = ViewModel 约定(在 Prism 6 中对此约定进行了更改,但前提是 View 被命名为 SomethingView跨度>
  • MainWindowMainWindowViewModel 在哪些命名空间中?应该分别是ViewsViewModels

标签: c# mvvm prism


【解决方案1】:

我只在用户控件而不是窗口上使用prism:ViewModelLocator.AutoWireViewModel="True" 让我的工作。我假设您使用的是 prism 6。

所以我所做的是,我首先创建了一个MainWindow,它将容纳我所有的UserControls,然后我创建了一个MainUserControl,它将容纳所有其他用户控件。这一切都是我在blog post (http://brianlagunas.com/getting-started-prisms-new-viewmodellocator/) 之后实现的。请记住创建您的 MVVM 文件夹(View 和 ViewModel)文件夹,并将它们各自的内容作为博客亮点。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    首先,您应该将ICommand 暴露给按钮的命令属性,而不是委托命令,这是ICommand 的具体实现。

    您可以通过实现摆脱视图模型定位器的约定 ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver((viewType) 在应用程序类启动覆盖方法中。

    有关更多信息,请搜索 Brian Lagunas viewmodellocator 博客

    【讨论】:

    • 谁投了反对票,请给我一个我错的理由
    • 也许 Brian Lagunas 可以评论一下
    • 好的,如果我删除链接并将其替换为实际答案,您将删除否决票?
    • 因为我是新人所以不知道不能发链接
    • 为什么即使在我删除链接后它仍然被否决真的不理解
    猜你喜欢
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    相关资源
    最近更新 更多