【问题标题】:How to fix my Prism region activation problem?如何解决我的 Prism 区域激活问题?
【发布时间】:2019-03-29 07:40:35
【问题描述】:

我在使用 MVVM 的地方设置了一个全新的 WPF 应用程序。 我在我的项目中使用 Prism 7.1.latest 作为 Nuget。 在我通过登录后,我将被转发到我的 MenuPage,用户可以在其中选择两个不同的选项。打开设置页面或上传页面。 Menue 是静态的,而我只想刷新 ContentView。

MenuPageViewModel.cs

public MenuPage2ViewModel(IContainerExtension container, IRegionManager regionManager)
        {
            _container = container;
            _regionManager = regionManager;

            _regionManager.Regions.Add("ContentRegion", new Region());

            this.CloseApplicationCommand = new DelegateCommand<Window>(this.CloseWindow);
            this.MinimizeApplicationCommand = new DelegateCommand<Window>(this.MinimizeWindow);
        }


        private DelegateCommand _loadSettingsPageCommand;

        public DelegateCommand LoadSettingsPageCommand => _loadSettingsPageCommand ?? (_loadSettingsPageCommand = new DelegateCommand(LoadSettingsPage, CanLoadSettingsPage));

        private void LoadSettingsPage()
        {
            IRegion region = _regionManager.Regions["ContentRegion"];

            var view = region.GetView(nameof(SettingsPage));
            if (view == null)
            {
                var t = _container.Resolve<SettingsPage>();
                region.Add(t, nameof(SettingsPage));
                view = region.GetView(nameof(SettingsPage));
            }

            region.Activate(view);
        }

        private bool CanLoadSettingsPage()
        {
            return true;
        }

MenuPage2.xaml 我的 ContentControl 应该加载的位置

<Grid Column="2" Row="1">
           <ContentControl regions:RegionManager.RegionName="ContentRegion" />
</Grid>

SettingsPage.xaml 我的蓝色背景视图

<UserControl x:Class="TachiFox3Reader.Win.Views.SettingsPage"
             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:mvvm="http://prismlibrary.com/"
             mvvm:ViewModelLocator.AutoWireViewModel="True"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">

    <Grid Background="Blue">

    </Grid>
</UserControl>

所以当我单击按钮激活设置视图时,什么也没有发生。 但现在在我定义内容容器的网格中应该至少有一个蓝色背景。

当我在 VS 中调试时,我可以看到 View 包含我的 SettingsPage,它被设置为活动但我的 gui 什么都不会显示。

【问题讨论】:

  • 为什么不能从 xaml 定义区域并使用RequestNavigate 加载设置页面?我认为您的代码可以大大简化。
  • 我没有在我的项目中使用RequestNavigate。这是我第一次自己做一个 WPF MVVM。但是谢谢你,我会检查一下。

标签: c# wpf prism


【解决方案1】:

删除线:

 _regionManager.Regions.Add("ContentRegion", new Region());

该区域已添加到 XAML 中。

我用来保存区域的 XAML 是:

<Window x:Class="WpfApp4.MainWindow"
        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:wpfApp4="clr-namespace:WpfApp4"
        xmlns:regions="http://prismlibrary.com/"
        mc:Ignorable="d"        
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Command="{Binding LoadSettingsPageCommand}"></Button>
        <ContentControl regions:RegionManager.RegionName="ContentRegion" />
    </Grid>    
</Window>

【讨论】:

  • 当我删除这一行时,我得到一个错误:System.Collections.Generic.KeyNotFoundException: 'The region manager does not contain the ContentRegion region.'
  • 这表示区域没有被处理,它在MenuPage2.xaml中声明。这段代码 还在吗?
  • 奇怪的是您仍然收到此错误,我已更新答案以显示我用来保存该区域的 XMAL
猜你喜欢
  • 1970-01-01
  • 2010-12-04
  • 1970-01-01
  • 2021-04-03
  • 1970-01-01
  • 2021-06-19
  • 2021-10-24
  • 1970-01-01
  • 2013-08-13
相关资源
最近更新 更多