代码之家  ›  专栏  ›  技术社区  ›  bporter

WPF设计器错误声明在应用StaticResource时,它“无法为自定义UserControl创建类型为…”的实例

  •  2
  • bporter  · 技术社区  · 14 年前

    我有个有趣的问题。我创建了一个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" />
    

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

    下面是模板的标记。就像我说的,不过,我用这个到处都没有问题。事实上,我甚至可以在其他用户控件中使用它。我只是不明白为什么它不适用于这个特定的代码。

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

    也就是说,如果有人对问题的起因有任何想法,我们仍然会感激任何帮助。

    2 回复  |  直到 14 年前
        1
  •  2
  •   bporter    14 年前

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

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

        2
  •  0
  •   John Gardner    14 年前

    我在某些东西(比如模板中的转换器)抛出其他异常或依赖于应用程序。当前为了什么。(应用电流是VS还是Blend in design time,运行时的应用程序)