单击按钮上的 Xamarin.Forms 动画(Flash 背景) [英] Xamarin.Forms Animation on click Button (Flash background)

查看:32
本文介绍了单击按钮上的 Xamarin.Forms 动画(Flash 背景)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的表单上实现一个拨号盘.现在,在我的 XAML 中,我正在测试一个按钮:XAML

I want to implement a dialpad on my form. Now, in my XAML I am testing a button: XAML

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
         x:Class="MyXFDemo001.Views.NewItemPage"
         Title="New Item">
<ContentPage.ToolbarItems>
    <ToolbarItem Text="Save" Clicked="Save_Clicked" />
</ContentPage.ToolbarItems>
<ContentPage.Content>
    <StackLayout Spacing="20" Padding="15">
        <Button
            x:Name="buttonSelectContact"
            Clicked="buttonSelectContact_Clicked"
            Text="CLICK" BackgroundColor="#0080ff" />
    </StackLayout>
</ContentPage.Content>

在我后面的代码中,我有一个方法 buttonSelectContact_Clicked:

And in my code behind I have a method buttonSelectContact_Clicked:

private async void buttonSelectContact_Clicked(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    btn.BackgroundColor = Color.FromHex("#22ac38");
    await Task.Delay(500);
    btn.BackgroundColor = Color.FromHex("#0080ff");
}

它可以工作,但没有我想要的那么顺畅.你能建议我添加动画而不是这个背景颜色吗?

It works, but not quietly as smooth as I want. Can you suggest me to add animation instead of this BackgroundColor?

推荐答案

根据 https://forums.xamarin.com/discussion/58818/how-to-animate-change-of-background-color你的代码看起来像这样

According to https://forums.xamarin.com/discussion/58818/how-to-animate-change-of-background-color your code would look like this

XAML

<StackLayout Spacing="20" Padding="15">
    <Grid>
        <Button
        x:Name="buttonSelectContactSecond"
        Text="CLICK" BackgroundColor="#22ac38" />
        <Button
            x:Name="buttonSelectContact"
            Clicked="buttonSelectContact_Clicked"
            Text="CLICK" BackgroundColor="#0080ff" />
    </Grid>
</StackLayout>

在代码后面

private async void buttonSelectContact_Clicked(object sender, EventArgs e) 
{
    StartAnimation();
}

private async void StartAnimation()
{
    await Task.Delay(200);
    await buttonSelectContact.FadeTo(0, 250);
    await Task.Delay(200);
    await buttonSelectContact.FadeTo(1, 250);
}

这篇关于单击按钮上的 Xamarin.Forms 动画(Flash 背景)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆