wpf 无法在设计器中创建“[用户控件]”错误的实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18552502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:33:14  来源:igfitidea点击:

Cannot create an instance of "[user control]" error in designer

c#wpfxamluser-controlscompiler-errors

提问by Rob McCabe

I have created the following user control. When I add it to a xaml window, I get the error "Cannot create an instance of "ucAppItem". I dragged the user control onto the window from the toolbar.

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

XAML of user control is as follows:

用户控件的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 of window is as follows:

窗口的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>

Thanks for your help in advance!

提前感谢您的帮助!

采纳答案by Rob McCabe

@Anatoily Nikolaev - thanks for your help! Your pointer on the label fixed the issue I had with it and you were right about the image. I'll mark your response as the answer. It was the source that was the issue.

@Anatoily Nikolaev - 感谢您的帮助!您在标签上的指针解决了我遇到的问题,您对图像的看法是正确的。我会将您的回复标记为答案。这是问题的来源。

My label is now defined as:

我的标签现在定义为:

   <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>

My image is now defined as:

我的形象现在被定义为:

       <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>

And I have resources (set in the App.xaml file), set as this:

我有资源(在 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>

回答by Anatoliy Nikolaev

First, instead of pack://siteoforigin:,,,/arrow2.pngneed to write your actual URIand to make sure that the file exists in the project as a resource, like this (MSDN):

首先,pack://siteoforigin:,,,/arrow2.png不需要编写您的实际URI文件并确保该文件作为资源存在于项目中,像这样 ( MSDN):

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

Secondly, trigger style for the label lblRun, will not work because you set this Foregroundvalue locally, in WPF have a list of value precedence (MSDN), that the local value of a higher priority than the trigger style:

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

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

Try to remove it Foregroundlocal value and use Stylesetter:

尝试删除它的Foreground本地值并使用Stylesetter:

<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>