wpf - 设置属性 'Prism.Regions.RegionManager.RegionName' 引发异常

标签 wpf xaml prism

我正在使用 WPF 和 Prism 为我的办公室开发一个应用程序,但遇到了障碍。我发现一个由 Brian Lagunas(Prism 的开发人员之一)主持的网络研讨会正在做一些我需要在这个应用程序中做的事情,所以我基本上遵循并更改了命名空间等以适应我的应用程序。

解决方案将编译,但当我尝试使用 ContentControl 和 Prism:RegionManager.RegionName 进行导航时,它会引发异常。

该解决方案有 2 个项目。第一个项目调用第二个项目的主窗口。我的代码如下。

项目 1 - 主窗口

namespace AdjusterToolV2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow 
    {
        public MainWindow()
        {
           InitializeComponent();
        }

        private void btnLetters_Click(object sender, RoutedEventArgs e)
        {
            URLetters.MainWindow frm1 = new URLetters.MainWindow();
            frm1.Show();
        }
    }
}

Prism Bootstrap

using Prism.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.Practices.Unity;
using URLetters.Views;

namespace URLetters
{
    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(PHLtrWithEvidenceView), "PHLtrWithEvidenceView");
        }
    }
}

项目 2 主页的 XAML(后面的代码为空)

<Controls:MetroWindow x:Class="URLetters.MainWindow"
                      xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"      
                      xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
                      xmlns:d= "http://schemas.microsoft.com/expression/blend/2008"
                      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    
                      xmlns:prism="http://prismlibrary.com/"
                      prism:ViewModelLocator.AutoWireViewModel="True"
                      xmlns:local="clr-namespace:URLetters"
                      mc:Ignorable="d"

                      Title="Unresolved Liability Letters" 
                      Height="500" 
                      Width="700" 
                      Icon="../Resources/GEICO.ico"
                      ResizeMode="NoResize"
                      WindowStartupLocation="CenterScreen">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="120"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <DockPanel Grid.Column="0" Background="#154995" HorizontalAlignment="Left" LastChildFill="False" Width="120">
            <StackPanel Grid.Column="0">
                <Button x:Name="btn48hrWithEvidence"
                        Command="{Binding NavigateCommand}"
                        CommandParameter="PHLtrWithEvidenceView"
                        x:FieldModifier="public"
                        Height="40"
                        Width="100"
                        Margin="10,10,0,0" 
                        ToolTip="Letter to the PH with a 48 hour contact time limit. Used when evidence has been provided by the claimant" >
                    <TextBlock FontSize="11" Text="48 Hour - PH With Evidence" TextWrapping="Wrap" TextAlignment="Center" />
                </Button>
                <Button x:Name="btn48hrNoEvidence"
                        Height="40"
                        Width="100"
                        Margin="10,10,0,0"
                        ToolTip="Letter to the PH with a 48 hour contact time limit. Used when there is no evidence provided by the claimant" >
                    <TextBlock FontSize="11" Text="48 Hour - PH With No Evidence" TextWrapping="Wrap" TextAlignment="Center" />
                </Button>
                <Button x:Name="btnNoCtcPH"
                        Height="40"
                        Width="100"
                        Margin="10,10,0,0"
                        ToolTip="Letter to the PH advising them that we have reached an AT FAULT liability decision based on the evidence provided." >
                    <TextBlock FontSize="11" Text="No Contact - PH Liability Decision" TextWrapping="Wrap" TextAlignment="Center" />
                </Button>
                <Button x:Name="btnNoCtcPHLiabDenial"
                        Height="40"
                        Width="100"
                        Margin="10,10,0,0"
                        ToolTip="Letter to the PH advising them that we have denied liability because we have no evidence to support the PH involvement" >
                    <TextBlock FontSize="11" Text="No Contact - PH Liability Denial" TextWrapping="Wrap" TextAlignment="Center" />
                </Button>
                <Button x:Name="btnNoCtcCLMTLiabDenial"
                        Height="40"
                        Width="100"
                        Margin="10,10,0,0"
                        ToolTip="Letter to the CLAIMANT advising that we have denied liability because we have no evidence to support the PH involvement" >
                    <TextBlock FontSize="11" Text="No Contact - CLMT Liability Denial" TextWrapping="Wrap" TextAlignment="Center" />
                </Button>
            </StackPanel>
        </DockPanel>
        <DockPanel x:Name="ContentRegionName" Grid.Column="1">
        <ContentControl prism:RegionManager.RegionName="ContentRegion"/>
        </DockPanel>

    </Grid>
</Controls:MetroWindow>

项目 2 主窗口的 ViewModel

using Prism.Mvvm;
using Prism.Regions;
using Prism.Commands;


namespace URLetters.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        private readonly IRegionManager _regionManager;

        public DelegateCommand<string> NavigateCommand { get; set; }

        public MainWindowViewModel(IRegionManager regionManager)
        {
            _regionManager = regionManager;

            NavigateCommand = new DelegateCommand<string>(Navigate);
        }

        private void Navigate(string uri)
        {
            _regionManager.RequestNavigate("ContentRegion", uri);
        }

    }
}

它在 XAML 文件中的这一行抛出异常:

<ContentControl prism:RegionManager.RegionName="ContentRegion"/>

异常内容为:“抛出异常:PresentationFramework.dll 中的‘System.Windows.Markup.XamlParseException’

其他信息:“设置属性‘Prism.Regions.RegionManager.RegionName’引发异常。”行号“69”和行位置“14”。”

当我单击 btnLetters 按钮时会发生这种情况。 URLetters MainWindow 未打开,然后引发异常。我已经回顾了代码,并根据网络研讨会中的示例进行了检查,但我无法弄清楚为什么会引发异常。非常感谢您的帮助。

最佳答案

您的应用程序设置似乎不正确。您的 Bootstrapper 应该是负责显示主窗口的类。您的 App.xaml 中不应包含任何 StartupUri。您的项目设置方式显然有问题,但您的帖子中没有足够的信息让我给出明确的答案。另外,拥有 2 个 MainWindows 让我很困惑:)

下载并安装 Prism 模板包:https://visualstudiogallery.msdn.microsoft.com/e7b6bde2-ba59-43dd-9d14-58409940ffa0

然后为 WPF 创建一个新的 Prism Unity 应用程序。它会正确地为您 stub 您的申请。然后开始添加您的 View 和 View 模型

关于wpf - 设置属性 'Prism.Regions.RegionManager.RegionName' 引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39089567/

相关文章:

c# - 在 WPF 应用程序中使用 WinRT 库。

c# - 我将如何使用带有媒体播放器的包 uri 资源?

c# - UWP - 缩放图像(捏缩放和双击),翻转 View

xamarin.forms - XF : Equivalent to Page OnAppearing event when app is resumed

navigation - 如何为 Xamarin.Forms( View )导航堆栈重置 Prism?

wpf - 是否可以自定义 WPF View 创建机制以使用我自己的 ViewFactory?

c# - 如何访问或修改动态创建的按钮

c# - 将可见性属性绑定(bind)到变量

silverlight - 在 XAML 中按下 "ENTER"键时调用命令

wpf - 从 MVVM 绑定(bind)到 View 在 Prism 中不起作用