簡體   English   中英

類庫視圖未綁定到 WPF Prism 應用程序的 ViewModel

[英]Class Library View not binding to ViewModel for WPF Prism Application

在過去的幾天里,我一直在嘗試學習和設置一個使用區域的演示 WPF Prism 應用程序。 我的解決方案中有 3 個項目,我遇到了一個問題,即“PrismSample.UX”類庫項目中包含的 View 和 ViewModel 不會綁定。 以下是我認為與我的問題相關的每個項目的代碼,任何幫助我指出正確方向的幫助將不勝感激。

PrismSample(WPF 應用程序“.NET 框架”)

殼牌.xaml

<Window x:Class="PrismSample.Shell"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:prism="http://prismlibrary.com/"
        xmlns:common="clr-namespace:PrismSample.Common;assembly=PrismSample.Common"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <ContentControl prism:RegionManager.RegionName="{x:Static common:RegionNames.ContentRegion}" />
    </Grid>
</Window>

應用程序.xaml.cs

using System.Windows;
using Prism.Ioc;
using Prism.Unity;
using Prism.Modularity;
using PrismSample.UX;

namespace PrismSample
{
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<Shell>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
         
        }

        protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
        {
            moduleCatalog.AddModule<UXModule>();
        }
    }
}

PrismSample.Common(類庫)

區域名稱.cs

namespace PrismSample.Common
{
    public static class RegionNames
    {
        public const string ContentRegion = "ContentRegion";
    }
}

PrismSample.UX(類庫)

UXModule.cs

using PrismSample.Common;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
using PrismSample.UX.Views;

namespace PrismSample.UX
{
    public class UXModule : IModule
    {
        public void OnInitialized(IContainerProvider containerProvider)
        {
            var regionManager = containerProvider.Resolve<IRegionManager>();
            regionManager.RequestNavigate(RegionNames.ContentRegion, nameof(UsersView));
        }

        public void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<UsersView>();
        }
    }
}

視圖/Users.xaml

<UserControl x:Class="PrismSample.UX.Views.UsersView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:prism="http://prismlibrary.com/"
             prism:ViewModelLocator.AutoWireViewModel="True"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <TextBlock 
            Text="{Binding PageName}" 
            FontSize="24"
            HorizontalAlignment="Center"/>
    </Grid>
</UserControl>

ViewModels/UsersViewModel.cs

using Prism.Regions;

namespace PrismSample.ViewModels.UX
{
    public class UsersViewModel : ViewModelBase
    {
        public string PageName { get; set; } = "Users";

        public UsersViewModel(IRegionManager regionManager) :
                              base(regionManager)
        {
        }
    }
}

ViewModels/ViewModelBase.cs

using Prism.Mvvm;
using Prism.Regions;

namespace PrismSample.ViewModels.UX
{
    public class ViewModelBase : BindableBase, INavigationAware
    {
        protected IRegionManager RegionManager { get; }

        public ViewModelBase(IRegionManager regionManager)
        {
            RegionManager = regionManager;
        }

        public virtual bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public virtual void OnNavigatedFrom(NavigationContext navigationContext)
        {

        }

        public virtual void OnNavigatedTo(NavigationContext navigationContext)
        {
            
        }
    }
}

您是否嘗試過刪除“ViewModelBase”並將其綁定到實際的 ViewModel?

  public class UsersViewModel : BindableBase, INavigationAware

namespace PrismSample.ViewModels.UX

這是標准約定的另一種方式:

namespace PrismSample.UX.Views

namespace PrismSample.UX.ViewModels

或者您將兩者一起注冊而不依賴於自動發現:

containerRegistry.RegisterForNavigation<UsersView, UsersViewModel>();

或者您更改約定(使用ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver )...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM