【发布时间】:2019-02-04 01:35:23
【问题描述】:
在 ListView 中,添加和删除项目是通过漂亮的动画完成的。但是,当我移动一个项目(或对集合进行排序)时,没有漂亮的动画,它似乎只是重置。
有谁知道我如何为物品移动到新位置设置动画?我在 ios 应用程序中看到过这种行为,在 UWP 中肯定有可能吗?
你可以在这个演示中看到删除动画很好,重新排序不是。
简单代码示例:
Xaml
<Page
x:Class="ExampleApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ExampleApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Button Name="btnReorder" Content="Reorder" Click="btnReorder_Click" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="32" />
<Button Name="btnRemove" Content="Remove" Click="btnRemove_Click" HorizontalAlignment="Left" Margin="100,10,0,0" VerticalAlignment="Top" Height="32" />
<ListView Name="list" Margin="0,200,0,0">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:MyData">
<Rectangle Width="100" Height="20" Fill="{x:Bind Path=Brush}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Page>
代码
using System.Collections.ObjectModel;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
namespace ExampleApp
{
public sealed partial class MainPage : Page
{
ObservableCollection<MyData> myCollection = new ObservableCollection<MyData>();
public MainPage()
{
InitializeComponent();
myCollection.Add(new MyData() { Brush = new SolidColorBrush(Colors.Red) });
myCollection.Add(new MyData() { Brush = new SolidColorBrush(Colors.Blue) });
myCollection.Add(new MyData() { Brush = new SolidColorBrush(Colors.Orange) });
myCollection.Add(new MyData() { Brush = new SolidColorBrush(Colors.CornflowerBlue) });
myCollection.Add(new MyData() { Brush = new SolidColorBrush(Colors.Yellow) });
myCollection.Add(new MyData() { Brush = new SolidColorBrush(Colors.Green) });
list.ItemsSource = myCollection;
}
private void btnReorder_Click(object sender, RoutedEventArgs e)
{
// moving an item does not animate the move
myCollection.Move(2, 3);
}
private void btnRemove_Click(object sender, RoutedEventArgs e)
{
// removing does a nice animation
myCollection.RemoveAt(1);
}
}
public class MyData
{
public Brush Brush { get; set; }
}
}
谢谢!
【问题讨论】: