2013-07-26 62 views
1

我想動畫網格行高度變化,但如何做到這一點與動態「星級」高度值?動畫網格行高度變化WP

我有以下電網:

<Grid x:Name="baseGrid"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="3*"/> 
    </Grid.RowDefinitions> 
    <Grid x:Name="grid0" Grid.Row="0" Tap="grid0_Tap"> 
     // Content 
    </Grid> 
    <Grid x:Name="grid1" Grid.Row="1" Tap="grid1_Tap"> 
     // Content 
    </Grid> 
    <Grid x:Name="grid2" Grid.Row="2"> 
     // Content 
    </Grid> 
</Grid> 

在後面的代碼我的邏輯是,如果用戶點擊grid0,GRID2獲取隱藏。同樣,如果用戶點擊grid1,grid2會返回它的大小。

private void grid0_Tap(object sender, RoutedEventArgs e) 
{ 
    this.LayoutRoot.RowDefinitions[2].Height = new GridLength(0); 
} 

private void grid1_Tap(object sender, RoutedEventArgs e) 
{ 
    this.LayoutRoot.RowDefinitions[2].Height = new GridLength(3, GridUnitType.Star); 
} 

這現在運作良好,但如何動畫呢?我認爲Storyboard需要從/到價值,因爲使用動態尺寸的Im我不能使用Storyboard?

+0

的問題不清楚。 「grid3被隱藏」,但沒有在您的XAML中定義「grid3」。 –

+0

對不起,混合了網格名稱。固定... – devha

回答

1

(編輯來解決Windows Phone 8動畫故事板問題)

任何XAML庫中都不存在動畫類GridLength

但是你可以關閉,或者你可以轉向第三方控制,它可以做你想做的事情。

而且,對於這種使用情況下,後臺代碼是相當多需要,而不是定義一個XAML資源,因爲你發現的元素的ActualHeight在運行時:

private double storedHeight = 0.0; 
    private void grid0_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
    { 
     //ListBox1.Visibility = Visibility.Visible; 
     if (storedHeight == 0.0) return; 
     var d = new DoubleAnimation(); 
     d.From = 0.0; 
     d.To = storedHeight; 
     d.Duration = TimeSpan.FromMilliseconds(200); 
     storedHeight = 0.0; 
     var s = new Storyboard(); 
     Storyboard.SetTarget(d, grid2); 
     Storyboard.SetTargetProperty(d, new PropertyPath("Height")); 
     s.Children.Add(d); 
     s.Begin(); 

    } 

    private void grid1_Tap(object sender, System.Windows.Input.GestureEventArgs e) 
    { 
     //ListBox1.Visibility = Visibility.Hidden; 
     if (storedHeight > 0.0) return; 
     storedHeight = ListBox1.ActualHeight; 
     var d = new DoubleAnimation(); 
     d.From = storedHeight; 
     d.To = 0.0; 
     d.Duration = TimeSpan.FromMilliseconds(200); 

     var s = new Storyboard(); 
     Storyboard.SetTarget(d, grid2); 
     Storyboard.SetTargetProperty(d, new PropertyPath("Height")); 
     s.Children.Add(d); 
     s.Begin(); 
    } 
+0

這裏的問題是我想要使用動態縮放來獲取應用程序在所有手機分辨率下工作。基本上在第一個元素(grid0)有地圖控制器,第二個(grid1)包含文本框,最後一個(grid2)有longListSelector。爲了得到上面的例子workin我需要爲邊框元素指定確切的高度尺寸。但如何處理這與不同的屏幕分辨率? – devha

+0

感謝羅布,但我認爲Windows Phone不支持BeginAnimation?至少當我嘗試使用longListSelector或map時,我得到錯誤...不包含'BeginAnimation'的定義。 – devha

+0

你是對的;在WP8 SDK中缺少這一點。但是你的帖子上有一個WPF標籤,所以我認爲這將是一個令人滿意的答案。希望這一個會爲你工作。查看修改。 –

0

這似乎不是一個明智的事情要做,而像這樣的網格動畫會看起來可怕,因爲它不是在合成器,但是:

private void grid0_Tap(object sender, RoutedEventArgs e) 
{ 
    this.LayoutRoot.RowDefinitions[2].Height = new GridLength(0); 
} 

private void grid1_Tap(object sender, RoutedEventArgs e) 
{ 
    double fromHeight = this.LayoutRoot.RowDefinitions[2].ActualHeight; 

    this.LayoutRoot.RowDefinitions[2].Height = new GridLength(3, GridUnitType.Star); 
    this.LayoutRoot.Measure(new Size(this.Width, this.Height)); 

    double toHeight = this.LayoutRoot.RowDefinitions[2].DesiredSize.Height; 

    MyStoryboard.From = fromHeight; 
    MyStoryboard.To = toHeight; 
    MyStoryboard.Begin(); 
} 
+0

任何其他想法如何處理類似的功能?我也可以使用除網格外的其他控件。 – devha