如何处理 View 资源上的 XML 解析异常?

How to handle an XML parse exception on View resource?

我的 LoginView 出现错误,类型为 XML 解析异常。所以我检查了内部错误详细信息,它告诉我找不到可见性转换器资源。

完整错误:

"{"Cannot find resource named 'BoolToNonVisibilityConverter'. Resource names are case sensitive."}"

我从这里了解到 xml 解析器找不到转换器 class。但是我检查了命名空间和引用,除此之外一切似乎都很好。

我在想可能找不到资源,因为在资源声明之前在 UserControl 上设置了可见性绑定。 This answer 确实提供了一些细节。

有谁知道为什么在 运行 时间找不到资源?

这是 LoginView 定义,包含 BoolToNonVisibilityConverter 资源引用:

<UserControl x:Class="MongoDBApp.Views.LoginView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:btv="clr-namespace:MongoDBApp.Converters"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:pass_helper="clr-namespace:MongoDBApp.Helpers"
             Visibility="{Binding LoggedIn,
                                  Converter={StaticResource BoolToNonVisibilityConverter}}"
             d:DesignHeight="300"
             d:DesignWidth="300"
             mc:Ignorable="d">

    <UserControl.Resources>
        <btv:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
        <btv:BoolToNonVisibilityConverter x:Key="BoolToNonVisibilityConverter" />
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="2*" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="4*" />
            <ColumnDefinition Width="2*" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="2*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>


        <GroupBox Name="groupBox3"
                  Grid.RowSpan="7"
                  Grid.ColumnSpan="5"
                  Width="250"
                  Height="250"
                  Header="Login">
            <Grid>
                <Button Name="LoginWithWidget"
                        Width="102"
                        Height="22"
                        Margin="68,126,71,70"
                        Command="{Binding LoginCommand}"
                        Content="Login" />

                <TextBox Name="UserTextBox"
                         Width="94"
                         Height="23"
                         Margin="119,24,0,171"
                         HorizontalAlignment="Left"
                         VerticalAlignment="Center"
                         Text="{Binding User}" />
                <Label Name="label1"
                       Height="28"
                       Margin="34,24,0,166"
                       HorizontalAlignment="Left"
                       VerticalAlignment="Center"
                       Content="Username" />
                <Label Name="label2"
                       Height="28"
                       Margin="34,64,0,126"
                       HorizontalAlignment="Left"
                       VerticalAlignment="Center"
                       Content="Password" />

            </Grid>
        </GroupBox>
    </Grid>
</UserControl>

UserControl 元素将在其子元素之前被解析,此时 btv:BoolToNonVisibilityConverter 是未知的(因此 运行 时间异常)。我知道你有两个选择:

 public SomeUserControl()
 {
     //Add your resource here before InitializeComponent parse the xaml.
     InitializeComponent();
 }
  • 将资源添加到 App.xaml 资源中,使其成为 应用程序范围的资源。
  • 将其添加到后面的 UserControl 代码中,但是 之前 xaml 已如上所示解析。

记住 xaml 定义的资源字典将覆盖在 InitializeComponents 之前定义的任何资源字典。