2017-11-18 413 views
0

我正在嘗試編寫button樣式,它存在於其自己的資源字典中。它應該有一個基本動畫,其中當鼠標移過它時,按鈕背景從深灰色變爲淺灰色。WPF:在故事板動畫中使用預定義顏色,用於資源字典中的按鈕樣式

的問題是,它似乎不喜歡,我引用故事板具體內鍵預定顏色的事實。我不明白爲什麼這是因爲我習慣於引用現有資源。

將引發異常在運行時:

"InvalidOperationException: Cannot freeze this Storyboard timeline tree for use across threads" 

下面是該按鈕的未完成的風格:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:local="clr-namespace:Style.Components.Buttons"> 


<Style TargetType="{x:Type Button}" x:Key="LargeFlatListOptionButtonStyle"> 
    <Setter Property="Foreground" Value="{DynamicResource TextParagraphWhiteP1}" /> 
    <Setter Property="BorderThickness" Value="0" /> 
    <Setter Property="Background" Value="{DynamicResource BackgroundGreyLevel2}" /> 
    <Setter Property="MinHeight" Value="32" /> 
    <Setter Property="MinWidth" Value="50" /> 
    <Setter Property="Padding" Value="6,2" /> 
    <Setter Property="BorderBrush" Value="{DynamicResource ControlOutlineUnselected}" /> 
    <Setter Property="SnapsToDevicePixels" Value="true" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Button}"> 
       <Grid> 
        <Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> 
         <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /> 
        </Border> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <!-- ANIMATIONS --> 
     <EventTrigger RoutedEvent="Button.MouseEnter"> 
      <EventTrigger.Actions> 
       <BeginStoryboard> 
        <Storyboard> 
         <ColorAnimation To="{DynamicResource BackgroundGreyLevel2Color}" Storyboard.TargetProperty="(Border.BorderBrush).Color" Duration="0:0:0.1"/> 
        </Storyboard> 
       </BeginStoryboard> 
      </EventTrigger.Actions> 
     </EventTrigger> 
    </Style.Triggers> 
</Style> 

顏色資源在這裏定義:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
... 
<SolidColorBrush x:Key="BackgroundGreyLevel2" Color="#FF464646" /> 
<SolidColorBrush x:Key="TextParagraphWhiteP1" Color="White" /> 
<Color x:Key="BackgroundGreyLevel2Color" >#FF464646</Color> 
... 

</ResourceDictionary> 

當然,我只能寫出我希望淡入淡出的顏色,但我更願意使用我已定義的資源,就好像我們的樣式從灰色更新爲例如藍色;我不得不記得在這裏和所有其他內聯位置更改它。

回答

1

來自Microsoft Docs on Storyboards的報價。

您不能使用動態資源引用或數據綁定表達式來設置Storyboard或動畫屬性值。這是因爲Style內的所有內容都必須是線程安全的,並且時序系統必須凍結Storyboard對象以使它們成爲線程安全的。如果Storyboard或其子時間軸包含動態資源引用或數據綁定表達式,則Storyboard不能被凍結。

我可以建議一些解決方法(我見過做),這可能會或可能不會爲你工作:

  • 使用StaticResource標記擴展引用顏色資源(它會工作,如果你重新定義顏色資源,但不會允許您的庫的用戶稍後重新定義它)。然後,人們可以爲按鈕設置「基本風格」,併爲不同顏色的按鈕創建特定樣式(基於該「基本樣式」並使用StaticResource添加動畫)。
  • 更改ControlTemplate,以便您有兩個不同顏色的面(彼此之上)(例如,您可以在具有深灰色背景的邊框頂部具有淺灰色背景的邊框),然後爲頂層元素設置動畫Opacity。你基本上淡出了最重要的元素。那些元素可以使用DynamicResource來設置它們的屬性。
+0

djomlastic,謝謝你的幫助。我可以通過「重新定義顏色資源」來問你的第一個建議是什麼意思嗎? –

+0

@BenHayward好吧,如果您將BackgroundGreyLevel2Color更改爲某種其他顏色(如紅色),但您不更改該按鍵,則您的按鈕將變爲紅色。 StaticResource vs DynamicResource的問題依然存在,所以如果你的圖書館的用戶決定他希望BackgroundGreyLevel2Color是黃金(而不是灰色),那麼該按鈕將被動畫爲灰色。我相信第二種選擇可能會更好,我已經在控制庫中看到了。 – djomlastic

+0

@djomalstic我看到了 - 我試着簡單地將故事板改爲To =「{StaticResource BackgroundGreyLevel2Color}」,但這也行不通。我明白你的第二個建議是什麼意思 - 這可能確實是最好的解決方法。我會放棄它!非常感謝 –

相關問題