无法创建“”的实例;[用户控制]”;设计器中出现错误

本文关键字:错误 用户 创建 实例 控制 | 更新日期: 2023-09-27 18:29:50

我已经创建了以下用户控件。当我将它添加到xaml窗口时,我得到错误"无法创建"ucAppItem"的实例。我从工具栏将用户控件拖到窗口上。

用户控件的XAML如下:

<UserControl x:Class="Demos.ucAppItem"
             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" 
             mc:Ignorable="d" Width="852" Height="215">
    <Grid>
        <Label Name="lblTitle"  Content="Title" HorizontalAlignment="Left" Margin="233,10,0,0" VerticalAlignment="Top" FontSize="22" FontFamily="Arial"/>

        <Image Width="40" Height="40" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,80,0">
            <Image.Style>
                <Style TargetType="{x:Type Image}">
                    <Setter Property="Source" Value="pack://siteoforigin:,,,/arrow2.png"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Source" Value="pack://siteoforigin:,,,/arrow1.png"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>
        <Label x:Name="lblRun" Content="Run" HorizontalAlignment="Right" Margin="0,88,35,0" VerticalAlignment="Top" Foreground="#FF2EAADC" FontSize="20">
            <Label.Style>
                <Style TargetType="{x:Type Label}">
                    <Setter Property="Foreground" Value="#FF2EAADC"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="#006d9e"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Label.Style>
        </Label>
    </Grid>
</UserControl>

窗口的XAML如下:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Demos" x:Class="Demos.Window1"
        Title="Window1" Height="487" Width="854">
    <Grid>
        <local:ucAppItem/>
    </Grid>
</Window>

提前感谢您的帮助!

无法创建“”的实例;[用户控制]”;设计器中出现错误

@Anatooil Nikolaev-感谢您的帮助!你在标签上的指针解决了我的问题,你对图像的看法是正确的。我会把你的回答标记为答案。问题出在消息来源身上。

我的标签现在被定义为:

   <Label x:Name="lblRun" Content="Run" HorizontalAlignment="Right" Margin="0,88,35,0" VerticalAlignment="Top" FontSize="20">
        <Label.Style>
            <Style TargetType="{x:Type Label}">
                <Setter Property="Foreground" Value="#FF2EAADC"/>
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Foreground" Value="#006d9e"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Label.Style>
    </Label>

我的图像现在被定义为:

       <Image>
            <Image.Style>
                <Style TargetType="{x:Type Image}">
                    <Setter Property="Source" Value="{StaticResource arrow2}"/>
                    <Setter Property="Height" Value="40"/>
                    <Setter Property="Width" Value="40"/>
                    <Setter Property="VerticalAlignment" Value="Center"/>
                    <Setter Property="Margin" Value="0,0,80,0"/>
                    <Setter Property="HorizontalAlignment" Value="Right"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Source" Value="{StaticResource arrow1}"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>

我有资源(在App.xaml文件中设置),设置如下:

<Application x:Class="demos.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <BitmapImage x:Key="arrow1" UriSource="arrow1.png" />
        <BitmapImage x:Key="arrow2" UriSource="arrow2.png" />
    </Application.Resources>
</Application>

首先,不需要pack://siteoforigin:,,,/arrow2.png,而是需要编写实际的URI,并确保该文件作为资源存在于项目中,如下所示(MSDN):

pack://application:,,,/arrow1.png

其次,标签lblRun的触发器样式将不起作用,因为您在本地设置了此Foreground值,在WPF中有一个值优先级列表(MSDN),即本地值的优先级高于触发器样式:

<Label x:Name="lblRun" Foreground="#FF2EAADC" FontSize="20" ... />

尝试删除Foreground本地值并使用Style setter:

<Label x:Name="lblRun" Content="Run" HorizontalAlignment="Right" Margin="0,88,35,0" VerticalAlignment="Top" FontSize="20">
    <Label.Style>
        <Style TargetType="{x:Type Label}">
            <Setter Property="Foreground" Value="#FF2EAADC"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="#006d9e"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Label.Style>
</Label>