我正在使用带有Prism的WPF处理我办公室的申请,我遇到了障碍。我发现由Brian Lagunas(Prism的开发人员之一)主持的网络研讨会正在做我需要在这个应用程序中做的几件事情,所以我基本上跟着并更改名称空间等以适合我的应用程序。
解决方案将编译,但当我尝试使用Prism:RegionManager.RegionName的ContentControl进行导航时,它会引发异常。
解决方案有2个项目。第一个项目调用第二个项目的MainWindow。我的代码如下。
项目1 - MainWindow
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 Bootstrapper
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不会打开,然后抛出异常。我已经回过头了代码,并在网络研讨会的例子中检查了它,我无法弄清楚为什么抛出异常..帮助将不胜感激。
答案 0 :(得分:3)
看起来您的应用程序设置不正确。您的Bootstrapper应该是负责显示您的MainWindow的类。您的App.xaml中不应该有任何StartupUri。您的项目设置方式显然有问题,但您的帖子中没有足够的信息可以给我一个明确的答案。另外,有2个MainWindows令我困惑:)
下载并安装Prism Template Pack:https://visualstudiogallery.msdn.microsoft.com/e7b6bde2-ba59-43dd-9d14-58409940ffa0
然后为WPF创建一个新的Prism Unity App。它会为您正确地存储您的应用程序。然后开始添加您的Views和ViewModels