2013-05-30 32 views
4

我用下面的類和的DependencyProperty允許樣式設置一個按鈕的圖像在設計時的錯誤:按鈕樣式的作品在運行但Visual Studio顯示

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。 如果您在那裏更改名稱,錯誤也會更改, 因此它與使用按鈕的Window/UserControl中選擇的名稱無關。

什麼可能導致這種情況,有沒有辦法解決它,所以它在設計時也可以工作?

+0

您是否嘗試將名稱空間聲明添加到XAML文檔根元素?也許Visual Studio只在那裏查找聲明? –

+0

嘗試將裝配位添加到名稱空間聲明中... xmlns:vcontrols =「clr-namespace:Vialis.Led.LedControl5.Controls; assembly = ???」 – MaLio

+0

嗨,裝配和應用程序是一樣的(起初它是一個外部裝配,但我想把控件移動到同一個裝配中可能會解決這個問題......它並沒有顯式添加'; assembly = Vialis.Led .LedControl5'也沒有幫助,這隻會讓事情變得更糟 – TimothyP

回答

2

更新2:

這個問題是隻能用VS2012設計師(在VS2010正常工作),它已經是固定在Visual Studio 2012 Update 3補丁。

+0

對不起,這麼晚回到你身邊。無論如何,這肯定是VS2012中的一個錯誤,因爲我得到了完全相同的例外。 – TimothyP

+1

@TimothyP,我使用VS2012 Update 2進行了檢查 - 問題已到位。根據[少數](https://connect.microsoft.com/VisualStudio/feedback/details/779344/vs2012-xaml-designer-fails-to-resolve-binding-to-attached-property-in-a-style) [other](http://connect.microsoft.com/VisualStudio/feedback/details/783339/visual-studio-2012-update-2-xamlparseexception-in-wpf-designer-binding-attached-property)類似的抱怨你是正確 - 這是VS2012問題,我相信。好消息 - 它已在[Update 3 RC2](http://www.microsoft.com/en-us/download/confirmation.aspx?id=39067)中得到修復! :) – Sevenate

+1

@TimothyP,我可以確認這個問題與設計時間最終在[** Update 3 RC2 **]中得到解決(http://www.microsoft.com/en-us/download/details.aspx?id = 39067) - 幾分鐘前在我的機器上測試過。 – Sevenate

相關問題