按钮样式在运行时工作,但Visual Studio在设计时显示错误

时间:2013-05-30 06:35:17

标签: c# wpf xaml visual-studio-2012

我正在使用以下Class和DependencyProperty来允许样式为Button设置图像:

public static class ImageButton
{
    public static readonly DependencyProperty ImageProperty = 
                  DependencyProperty.RegisterAttached("Image", typeof(ImageSource), typeof(ImageButton),
                                                                                                    new FrameworkPropertyMetadata((ImageSource)null));
    public static ImageSource GetImage(DependencyObject obj)
    {
        return (ImageSource)obj.GetValue(ImageProperty);
    }

    public static void SetImage(DependencyObject obj, ImageSource value)
    {
        obj.SetValue(ImageProperty, value);
    }
}

我已经定义了以下样式(在ImageButton.xaml中):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:vcontrols="clr-namespace:Vialis.Led.LedControl5.Controls">

    <ControlTemplate x:Key="ImageButtonTemplate" TargetType="Button">
        <Image  Source="{Binding Path=(vcontrols:ImageButton.Image),
                                 RelativeSource={RelativeSource FindAncestor,
                                 AncestorType={x:Type Button}}}"
                Width="{TemplateBinding Width}" 
                Height="{TemplateBinding Height}" 
                Stretch="Fill"
                RenderTransformOrigin="0.5, 0.5">
            <Image.Resources>
                <Storyboard x:Key="ShrinkStoryboard">
                    <DoubleAnimation Storyboard.TargetName="ImageScale" 
                                                Storyboard.TargetProperty="(ScaleTransform.ScaleX)"
                                                To="0.8"
                                                Duration="0:0:0.15"
                                                AutoReverse="False"/>
                    <DoubleAnimation Storyboard.TargetName="ImageScale" 
                                                Storyboard.TargetProperty="(ScaleTransform.ScaleY)"
                                                To="0.8"
                                                Duration="0:0:0.15"
                                                AutoReverse="False"/>
                </Storyboard>
                <Storyboard x:Key="GrowStoryboard">
                    <DoubleAnimation Storyboard.TargetName="ImageScale" 
                                                Storyboard.TargetProperty="(ScaleTransform.ScaleX)"
                                                To="1.0"
                                                Duration="0:0:0.15"
                                                AutoReverse="False"/>
                    <DoubleAnimation Storyboard.TargetName="ImageScale" 
                                                Storyboard.TargetProperty="(ScaleTransform.ScaleY)"
                                                To="1.0"
                                                Duration="0:0:0.15"
                                                AutoReverse="False"/>
                </Storyboard>
            </Image.Resources>
            <Image.RenderTransform>
                <ScaleTransform x:Name="ImageScale" ScaleX="1" ScaleY="1" CenterX="1" CenterY="1"/>
            </Image.RenderTransform>

            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Pressed" Storyboard="{StaticResource ShrinkStoryboard}"/>
                    <VisualState x:Name="MouseOver" Storyboard="{StaticResource GrowStoryboard}"/>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
        </Image>
    </ControlTemplate>

    <Style x:Key="ImageButtonStyle" TargetType="Button">
        <Setter Property="Opacity" Value="0.5"/>
        <Setter Property="Template" Value="{StaticResource ImageButtonTemplate}"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Opacity" Value="1"/>
            </Trigger>
        </Style.Triggers>
    </Style>

</ResourceDictionary>

最后为了使用它,我有类似的东西:

<Button Width="32"
        Height="32"
        Style="{StaticResource ImageButtonStyle}"
        vcontrols:ImageButton.Image="/Images/someimage.png"/>

当我编译并执行应用程序时,一切正常。 该按钮获取图像并使用样式中定义的动画。

然而,在设计时,Visual Studio似乎无法将其可视化 XAML编辑器在整个按钮定义下显示波浪线。

错误信息显示:

  

前缀'vcontrols'不会映射到命名空间。

它指的是在Style中使用vcontrols。 如果你在那里更改名称,错误也会改变, 所以它与使用Button的Window / UserControl中选择的名称无关。

可能导致这种情况的原因是否有办法解决这个问题,以便它在设计时也能正常工作?

1 个答案:

答案 0 :(得分:2)

更新2:

此问题仅适用于VS2012设计器(在VS2010中工作正常)并且已在Visual Studio 2012 Update 3补丁中修复。