【问题标题】:Animate input box in UWPUWP 中的动画输入框
【发布时间】:2016-07-04 12:56:29
【问题描述】:

我想像上面那样在 MVVM 模板 10 中为输入框设置动画

我有一个列表视图 并且需要像图片一样的搜索栏

【问题讨论】:

  • 你已经尝试了什么?提供一些代码
  • 不清楚你在问什么。我们不是魔法精灵的......你不只是擦堆栈溢出灯,问一个两句话的问题然后得到答案。你需要付出一些努力。你试过什么?你的代码在哪里?你研究了什么?您的输出中是否有任何错误?等等……
  • 这一切都在Animations overview进行了很好的详细解释。别再无助了。

标签: xaml uwp template10


【解决方案1】:

我已附上您的问题的解决方案。 UWP c# 中 AutoSuggestBox 的 GotFocus 和 Lost Focus 触发了两个故事板

这是我取得的成就:

XAML:

<Page.Resources>
    <Storyboard x:Name="OnCancel">
        <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="button">
            <EasingDoubleKeyFrame KeyTime="0:0:0.1" Value="70">
                <EasingDoubleKeyFrame.EasingFunction>
                    <CircleEase EasingMode="EaseIn"/>
                </EasingDoubleKeyFrame.EasingFunction>
            </EasingDoubleKeyFrame>
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="HeaderGrid">
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="51"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="HeaderGrid">
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="1"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)" Storyboard.TargetName="HeaderGrid">
            <EasingDoubleKeyFrame KeyTime="0" Value="-36.058"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Name="OnTextBoxFocus">
        <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="button">
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.8" Value="70">
                <EasingDoubleKeyFrame.EasingFunction>
                    <CircleEase EasingMode="EaseOut"/>
                </EasingDoubleKeyFrame.EasingFunction>
            </EasingDoubleKeyFrame>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimationUsingKeyFrames EnableDependentAnimation="True" Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="HeaderGrid">
            <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
        <DoubleAnimation Duration="0:0:0.4" To="0" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="HeaderGrid" d:IsOptimized="True"/>
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.TranslateY)" Storyboard.TargetName="HeaderGrid">
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
            <EasingDoubleKeyFrame KeyTime="0:0:0.4" Value="-36.058"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid x:Name="HeaderGrid" Margin="0,0,-1,-0.117" RenderTransformOrigin="0.5,0.5" Height="51">
        <Grid.RenderTransform>
            <CompositeTransform/>
        </Grid.RenderTransform>
        <TextBlock x:Name="textBlock" TextWrapping="Wrap" Text="TextBlock" Margin="5,0,1,3" FontSize="36" SelectionHighlightColor="{x:Null}" Foreground="DodgerBlue"/>
        <TextBox Width="1" Height="1" IsReadOnly="True"/>
        <Path Data="M0,48 L360,48" Height="1" Margin="0,0,0,0.117" Stretch="Fill" Stroke="DodgerBlue" UseLayoutRounding="False" VerticalAlignment="Bottom" d:LayoutOverrides="LeftPosition, RightPosition"/>
    </Grid>
    <StackPanel Grid.Row="1">
        <Grid Height="32" Margin="12,8,12,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>

            <AutoSuggestBox x:Name="searchText" PlaceholderText="Search" QueryIcon="Find" TextMemberPath="name" LostFocus="searchText_LostFocus" GotFocus="searchText_GotFocus"/>
            <Button x:Name="button" Content="Cancel" VerticalAlignment="Stretch" d:LayoutOverrides="Height" Grid.Column="4" Margin="5,0,0,0" Width="70" Click="button_Click"/>
        </Grid>
        <ListView x:Name="listView" Background="#FFECECEC" Margin="0,8,0,0">
            <ListViewItem Content="List View Item 1" BorderThickness="0,0,0,1" BorderBrush="#FFB9B9B9"/>
            <ListViewItem Content="List View Item 2" BorderThickness="0,0,0,1" BorderBrush="#FFB9B9B9"/>
            <ListViewItem Content="List View Item 3" BorderThickness="0,0,0,1" BorderBrush="#FFB9B9B9"/>
            <ListViewItem Content="List View Item 4" BorderThickness="0,0,0,1" BorderBrush="#FFB9B9B9"/>
            <ListViewItem Content="List View Item 5" BorderThickness="0,0,0,1" BorderBrush="#FFB9B9B9"/>
            <ListViewItem Content="List View Item 6" BorderThickness="0,0,0,1" BorderBrush="#FFB9B9B9"/>
            <ListViewItem Content="List View Item 7" BorderThickness="0,0,0,1" BorderBrush="#FFB9B9B9"/>
            <ListViewItem Content="List View Item 8" BorderThickness="0,0,0,1" BorderBrush="#FFB9B9B9"/>
            <ListViewItem Content="List View Item 9" BorderThickness="0,0,0,1" BorderBrush="#FFB9B9B9"/>
            <ListViewItem Content="List View Item 10" BorderThickness="0,0,0,1" BorderBrush="#FFB9B9B9"/>
        </ListView>
    </StackPanel>

</Grid>

XAML.CS

后面的代码中
 private void searchText_LostFocus(object sender, RoutedEventArgs e)
    {
        OnCancel.Begin();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        OnCancel.Begin();
    }


    private void searchText_GotFocus(object sender, RoutedEventArgs e)
    {
        OnTextBoxFocus.Begin();
    }

另外不要忘记在初始化时设置按钮宽度 = 0。

希望这会有所帮助。

【讨论】:

  • @SamTholiya 很高兴我能帮上忙
【解决方案2】:

您可以使用数据绑定将标题和Cancel 按钮的Visibility 属性绑定到ViewModel 中定义的属性,正如@Raunaq Patel 所说,动画由GotFocusLostFocus 事件触发。

所以你可以例如这样的代码:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <TextBlock x:Name="pageHeader" Text="Main Page" Grid.Row="0" Visibility="{Binding IsVisible}" FontSize="30" />
    <Grid Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid Grid.Row="0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <AutoSuggestBox HorizontalAlignment="Stretch" GotFocus="{x:Bind ViewModel.Search_GotFocus}"
                            LostFocus="{x:Bind ViewModel.Search_LostFocus}" VerticalAlignment="Stretch"
                            Width="{Binding BoxWidth}" />

            <TextBlock Text="Cancel" Foreground="BlueViolet" Tapped="{x:Bind ViewModel.Cancel_Tapped}" Width="100"
                       VerticalAlignment="Stretch" HorizontalAlignment="Center" FontSize="20"
                       Visibility="{Binding CancelIsVisible}" Grid.Column="1" />
        </Grid>
        <ListView Grid.Row="1" IsEnabled="{Binding ListViewEnable}">
            <ListViewItem>Item 1</ListViewItem>
            <ListViewItem>Item 2</ListViewItem>
            <ListViewItem>Item 3</ListViewItem>
            <ListViewItem>Item 4</ListViewItem>
            <ListViewItem>Item 5</ListViewItem>
        </ListView>
    </Grid>
</Grid>

由于您使用的是模板 10,因此后面的代码例如在 MainPageViewModel 中,如下所示:

public class MainPageViewModel : ViewModelBase
{
    private Visibility _IsVisible;

    public Visibility IsVisible
    {
        get { return _IsVisible; }
        set
        {
            if (value != _IsVisible)
            {
                _IsVisible = value;
                RaisePropertyChanged();
            }
        }
    }

    private Visibility _CancelIsVisible;

    public Visibility CancelIsVisible
    {
        get { return _CancelIsVisible; }
        set
        {
            if (value != _CancelIsVisible)
            {
                _CancelIsVisible = value;
                RaisePropertyChanged();
            }
        }
    }

    private bool _ListViewEnable;

    public bool ListViewEnable
    {
        get { return _ListViewEnable; }
        set
        {
            if (value != _ListViewEnable)
            {
                _ListViewEnable = value;
                RaisePropertyChanged();
            }
        }
    }

    private double _BoxWidth;

    public double BoxWidth
    {
        get { return _BoxWidth; }
        set
        {
            if (value != _BoxWidth)
            {
                _BoxWidth = value;
                RaisePropertyChanged();
            }
        }
    }

    public MainPageViewModel()
    {
        _IsVisible = Visibility.Visible;
        _CancelIsVisible = Visibility.Collapsed;
        _ListViewEnable = true;
        _BoxWidth = Window.Current.Bounds.Width;
    }

    public void Search_GotFocus(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        IsVisible = Visibility.Collapsed;
        CancelIsVisible = Visibility.Visible;
        ListViewEnable = false;
        BoxWidth = _BoxWidth - 100;
    }

    public void Search_LostFocus(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        IsVisible = Visibility.Visible;
        CancelIsVisible = Visibility.Collapsed;
        ListViewEnable = true;
        BoxWidth = Window.Current.Bounds.Width;
    }

    public void Cancel_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
    {
        IsVisible = Visibility.Visible;
        CancelIsVisible = Visibility.Collapsed;
        ListViewEnable = true;
        BoxWidth = Window.Current.Bounds.Width;
    }
}

这里你可以看到ListView中的数据是假的,你当然可以使用DataTemplate并将集合绑定到ListViewItemSource。这是我的示例的渲染图像:

【讨论】:

    【解决方案3】:

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 2018-05-08
    • 1970-01-01
    相关资源
    最近更新 更多