【问题标题】:WPF designer error states that it "Could not create an instance of type..." for my custom UserControl when a StaticResource is appliedWPF 设计器错误指出,当应用 StaticResource 时,它​​“无法为我的自定义 UserControl 创建类型的实例...”
【发布时间】:2011-04-11 19:00:43
【问题描述】:

我有一个有趣的问题。我创建了一个 WPF UserControl,其中包含一个使用模板的按钮:

<Button x:Name="btnStart" Template="{StaticResource RoundedGlassButton}" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="4" Height="32" Background="DarkGreen" Foreground="White" Width="72" Content="Start" />

当我将 UserControl 放在父控件或窗体上时,设计器会抛出错误,“无法创建类型为 'MyUserControl' 的实例。” 但是,虽然设计器无法工作,该应用程序仍将编译,我在运行时得到所需的结果。我已将问题缩小到应用于我的按钮的模板。当我删除模板时,设计师工作:

<Button x:Name="btnStart" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="4" Height="32" Background="DarkGreen" Foreground="White" Width="72" Content="Start" />

我在所有地方都使用这个特定的模板 - 所以我知道它有效。它甚至适用于其他用户控件。我只是不明白为什么它在这个特定的 UserControl 中不起作用。

有人有什么想法吗?如果不能在视觉设计器中看到结果,就很难构建 UI。

下面是模板的标记。但是,就像我说的那样,我到处都在使用它,没有任何问题。事实上,我什至在其他 UserControls 中使用它也很好。我只是对为什么它不适用于这个特定的代码感到困惑。

    <ControlTemplate x:Key="RoundedGlassButton" TargetType="{x:Type Button}">
    <ControlTemplate.Resources>
        <Storyboard x:Key="Timeline1">
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="glow" Storyboard.TargetProperty="(UIElement.Opacity)">
                <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="Timeline2">
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="glow" Storyboard.TargetProperty="(UIElement.Opacity)">
                <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </ControlTemplate.Resources>
    <Border BorderBrush="#FF888888" BorderThickness="1,1,1,1" CornerRadius="4">
        <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="#FF000000" BorderThickness="1,1,1,1" CornerRadius="4">                
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="0.507*"/>
                    <RowDefinition Height="0.493*"/>
                </Grid.RowDefinitions>
                <Border Opacity="0" BorderThickness="2" BorderBrush="White" HorizontalAlignment="Stretch" x:Name="glow" Width="Auto" Grid.RowSpan="2" CornerRadius="4">
                    <Border.BitmapEffect>
                        <BlurBitmapEffect Radius="2" />
                    </Border.BitmapEffect>                       
                </Border>
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" Grid.RowSpan="2" SnapsToDevicePixels="True" />
                <Border HorizontalAlignment="Stretch" Margin="0,0,0,0" x:Name="shine" Width="Auto" CornerRadius="4,4,8,8">
                    <Border.Background>
                        <LinearGradientBrush EndPoint="0.494,0.889" StartPoint="0.494,0.028">
                            <GradientStop Color="#AAFFFFFF" Offset="0"/>
                            <GradientStop Color="#33FFFFFF" Offset="1"/>
                        </LinearGradientBrush>
                    </Border.Background>
                </Border>
            </Grid>
        </Border>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsPressed" Value="True">
            <Setter Property="Opacity" TargetName="shine" Value="0.4"/>
            <Setter Property="Background" TargetName="border" Value="#CC000000"/>
            <Setter Property="Visibility" TargetName="glow" Value="Hidden"/>
        </Trigger>
        <Trigger Property="IsMouseOver" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard Storyboard="{StaticResource Timeline1}"/>
            </Trigger.EnterActions>
            <Trigger.ExitActions>
                <BeginStoryboard x:Name="Timeline2_BeginStoryboard" Storyboard="{StaticResource Timeline2}"/>
            </Trigger.ExitActions>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

由于项目截止日期,我将标记直接移动到父控件中以暂时解决问题。也就是说,如果有人对可能导致问题的原因有任何想法 - 任何帮助仍将不胜感激。

【问题讨论】:

  • 很奇怪!您能否提供更多详细信息,例如该模板有什么不寻常之处?我假设它使用触发器等......也许其中一个依赖于设计时不可用的东西?
  • 因为这里没什么可做的。如果仍然有问题,您可以发布您的模板 XAML 吗?

标签: c# wpf visual-studio


【解决方案1】:

好吧,我很尴尬。事实证明,我忘记在我的 UserControl 标记顶部添加对包含模板的资源的引用。我的 UserControl 文件中的前几行标记应该如下所示:

    <UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/MyTemplates/MyTemplate.xaml" />
        </ResourceDictionary.MergedDictionaries>...

不过,奇怪的是,只有设计者抱怨,而应用程序仍然编译并正常工作。

【讨论】:

    【解决方案2】:

    还有其他嵌套异常吗?

    当某些东西(例如模板中的转换器)抛出其他异常或依赖 Application.Current 时,我已经看到了这种情况。 (app.current 在设计时是 VS 或 Blend,你的应用在运行时)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      • 2013-09-04
      • 2021-01-01
      相关资源
      最近更新 更多