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

4

我正在使用下面的类和DependencyProperty,允许样式为按钮设置图像:

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”没有映射到命名空间。”
它是指在样式中使用vcontrols。如果您更改那里的名称,错误也会改变,因此与使用Button的Window/UserControl中选择的名称无关。
可能导致这种情况的原因是什么?有没有办法修复它,使其在设计时也能正常工作?

尝试将程序集位添加到命名空间声明中... xmlns:vcontrols="clr-namespace:Vialis.Led.LedControl5.Controls;assembly=???" - MaLio
当我阅读你的问题时,脑海中浮现的第一件事与MaLio的评论相同。你所说的“这只会让情况变得更糟”是什么意思?汇编实际上是整个“Vialis.Led.LedControl5”吗? - Dave Williams
目前所有的内容都在同一程序集中。至于“更糟”的部分,只要我做了其他错误,就开始出现其他问题了。所以基本上ImageButton类和Style与我正在使用它们的WPF窗口在同一程序集中。 - TimothyP
1
@TimothyP,我刚刚在新的WPF应用程序项目中测试了你的代码片段,一切看起来都很好,并且在VS2010中按预期工作(就我所知)——图像按钮在设计模式下也很好看。你需要我发布完整的测试项目代码吗?(它将包含95%的你的代码和5%的自动生成的项目模板代码) - Sevenate
@Sevenate 如果您能发布那个,那就太好了。然后我会将其加载到2012中,看看它是否仍然有效。 - TimothyP
显示剩余3条评论
1个回答

2

更新2:

这个问题只出现在VS2012设计器中(在VS2010中它工作正常),并且已经在Visual Studio 2012 Update 3补丁中修复。


很抱歉这么晚才回复你。无论如何,这一定是VS2012中的一个bug,因为我得到了完全相同的异常。 - TimothyP
1
@TimothyP,我已经使用VS2012 Update 2进行了检查 - 问题仍然存在。根据一些其他类似的投诉,你是正确的 - 我相信这是VS2012的问题。好消息是 - 它已经在Update 3 RC2中得到了修复! :) - Sevenate
1
@TimothyP,我可以确认,在Update 3 RC2中,设计时间的问题终于得到修复 - 我刚刚在我的机器上进行了测试。 - Sevenate
顺便提一下,你只需要下载 VS2012.3 RC.exe(1.3 MB)网络安装程序。 - Sevenate
正在安装中,稍后会告知 :) - TimothyP
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接