【问题标题】:Weird XAML errors after upgrading WPF app to .NET 5将 WPF 应用程序升级到 .NET 5 后出现奇怪的 XAML 错误
【发布时间】:2021-01-17 06:05:05
【问题描述】:

我有一个在 .NET Framework WPF 应用程序中创建的 XAML 视图,我正在尝试将应用程序升级到 .NET 5:

<Window
        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:AutoPBW="clr-namespace:AutoPBW;assembly=AutoPBW"
        xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        xmlns:shell="clr-namespace:System.Windows.Shell;assembly=PresentationFramework"
        mc:Ignorable="d"
        x:Name="window"
        x:Class="AutoPBW.WPF.MainWindow"
        Title="AutoPBW" Height="397" Width="653" Loaded="window_Loaded" Closing="window_Closing" Icon="AutoPBW.ico">
    <Window.Resources>
        <!-- this CollectionViewSource gives a null reference exception on the d:DesignSource attribute -->
        <CollectionViewSource x:Key="playerGameViewSource" d:DesignSource="{d:DesignInstance {x:Type AutoPBW:PlayerGame}, CreateList=True}"/>
        <Style x:Key="ListItemStyle" TargetType="{x:Type ListBoxItem}">
            <Style.Resources>
            </Style.Resources>
            <Setter Property="Template">
                <Setter.Value>
                    <!-- this ControlTempate gives an error saying that the Item.MouseOver.Background resource could not be found -->
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="True"/>
                                </MultiTrigger.Conditions>
                                <!-- here is where the Item.MouseOver.Background resource is actually referenced -->
                                <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.MouseOver.Background}"/>
                                <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.MouseOver.Border}"/>
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
       <!-- this is where the actual UI is defined, no errors here -->
    </Grid>
</Window>

我删除了一些不相关的部分,但 cmets 表示我遇到编译错误的位置。

我看到的错误是:

XDG0062 对象未设置为对象的实例。

XDG0062 找不​​到名为“Item.MouseOver.Background”的资源。资源名称区分大小写。

如何解决这些错误?

谢谢! ????

【问题讨论】:

    标签: wpf xaml .net-5


    【解决方案1】:

    哦,我想通了。我的项目文件中的 TargetFramework 设置不正确。应该是net5-windows,但我设置为net5.0-windows

    【讨论】:

      猜你喜欢
      • 2021-05-14
      • 2015-05-14
      • 1970-01-01
      • 1970-01-01
      • 2016-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多