【问题标题】:Cannot create an instance of "[user control]" error in designer无法在设计器中创建“[用户控件]”错误的实例
【发布时间】:2013-09-04 08:09:28
【问题描述】:

我创建了以下用户控件。当我将它添加到 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>

提前感谢您的帮助!

【问题讨论】:

  • 为我工作。你有没有在用户控制后面的代码中写过任何东西?

标签: c# wpf xaml user-controls compiler-errors


【解决方案1】:

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

【讨论】:

  • 我在一些使用 ImageBrushes 的按钮上遇到了类似的问题。将画笔资源从 UserControl.Resources 移到 Application.Resources 中,如您的示例所示,这是我缺少的关键部分。
  • 听起来您仍应将其他回复标记为已接受答案?
【解决方案2】:

首先,需要写你实际的URI 而不是pack://siteoforigin:,,,/arrow2.png,并确保该文件作为资源存在于项目中,如下所示(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>

【讨论】:

  • 非常感谢您的指点!我做了这些更改,但不幸的是它仍然给设计者错误!
  • @Rob McCabe:奇怪的是,对我来说它有效。相同类型的错误?如果注释掉Image控件,错误会消失吗?
  • 是的,我现在可以重新添加标签而不会出错。它只是图像导致问题。这是我所拥有的:
  • @Rob McCabe:我认为整个事情都在Image 中。该项目找不到它并引发异常。你可以在我的回答中看到URI(第一个链接),或者直接写文件名,像这样:&lt;Setter Property="Source" Value="Arrow1.png" /&gt;
猜你喜欢
  • 2012-10-21
  • 2020-04-21
  • 2010-10-26
  • 2014-01-11
  • 1970-01-01
  • 1970-01-01
  • 2013-01-30
  • 1970-01-01
  • 2021-04-12
相关资源
最近更新 更多