2015-10-04 21 views
0

我目前工作在Windows通用的應用程序工作,我定義了一個名爲UserControlIconButtonVisualStateManager不是通用的應用

IconButton.xaml

<VisualStateManager.VisualStateGroups> 
    <VisualStateGroup x:Name="Common"> 
     <VisualState x:Name="MouseOver"> 
      <Storyboard> 
       <ColorAnimation To="Red" Storyboard.TargetName="path" Storyboard.TargetProperty="(Fill).(SolidColorBrush.Color)" /> 
      </Storyboard> 
     </VisualState> 
    </VisualStateGroup> 
</VisualStateManager.VisualStateGroups> 

<Grid> 
    <Path x:Name="path" Data="{x:Bind IconPathData, Mode=OneWay}" Stretch="UniformToFill" Fill="White" /> 
</Grid> 

IconButton。 xaml.cs

public sealed partial class IconButton : UserControl 
{ 
    public static readonly DependencyProperty IconPathDataProperty = DependencyProperty.Register("IconPathData", typeof(string), typeof(IconButton), new PropertyMetadata("")); 

    public string IconPathData 
    { 
     get { return (string)GetValue(IconPathDataProperty); } 
     set { SetValue(IconPathDataProperty, value); } 
    } 

    public IconButton() 
    { 
     InitializeComponent(); 
    } 

    private void UserControl_PointerEntered(object sender, Windows.UI.Xaml.Input.PointerRoutedEventArgs e) 
    { 
     VisualStateManager.GoToState(this, MouseOver.Name, true); 
    } 
} 

根據MSDNGoToState方法預計VisualState與給定的名稱傳遞給該方法。在我的情況下,VisualStatex:Name="MouseOver",所以GoToState應該能夠找到這個。
不幸的是GoToState總是返回false,只有當它找不到具有給定名稱的VisualState時纔會發生。
我真的不知道該怎麼做才能解決這個問題。文檔非常簡單,Web上的幾個例子與我一樣,但它們能夠正常工作。
你能告訴我我做錯了什麼,以及如何解決它?

+0

嘗試'PointerOver',而不是'MouseOver' –

+0

'MouseOver'是'VisualState'的只是名字,應該是無關的'GoToState'只要我提供正確的名稱(請參閱MSDN鏈接)。如如果我用'PointerOver'替換'MouseOver',預計沒有什麼變化。 – TorbenJ

回答

3

既然你不顯示的IconButton.xaml的全部內容,我認爲它看起來有點像:

<UserControl 
    x:Class="App1.IconButton" 
    ... 
    PointerEntered="UserControl_PointerEntered"> 

    <VisualStateManager.VisualStateGroups> 
     ... 
    </VisualStateManager.VisualStateGroups> 

    <Grid> 
     <Path ... /> 
    </Grid> 
</UserControl> 

如果是這樣,移動VisualStateManager的是的UserControl一個DIREKT孩子Grid(這一翻譯裏面:

<UserControl 
    x:Class="App1.IconButton" 
    ... 
    PointerEntered="UserControl_PointerEntered"> 

    <Grid> 
     <VisualStateManager.VisualStateGroups> 
      <VisualStateGroup x:Name="Common"> 
       <VisualState x:Name="MouseOver"> 
        <Storyboard> 
         <ColorAnimation To="Red" Storyboard.TargetName="path" Storyboard.TargetProperty="(Fill).(SolidColorBrush.Color)" /> 
        </Storyboard> 
       </VisualState> 
      </VisualStateGroup> 
     </VisualStateManager.VisualStateGroups> 

     <Path x:Name="path" ... /> 
    </Grid> 
</UserControl>