そのため、別のアクションが発生する前に、ボタンにアニメーションを実行させようとしています。しかし、私がそれをどのように設定したかは奇妙です。
以下に示すように、アニメーションを実行するClick + = AnimatedButton_Clickedハンドラーを追加するAnimatedButtonというサブクラス化されたボタンがあります。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace TransactionApp_2.Views.UITools.Buttons
{
class AnimatedButton : Button
{
/// <summary>
/// Generic Animated Button Subclass that allows for:
/// Button indexing,
/// Simple sleek animations,
/// Sets initial properties and parameters,
/// allows easy background images
/// </summary>
public int ButtonIndex { get; set; }
public AnimatedButton(string buttonText, ImageSource backgroundImage = null, int buttonIndex = 0)
{
ButtonIndex = buttonIndex;
Text = buttonText;
FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label));
Opacity = 0.95;
Margin = new Thickness(8,0,8,0);
Clicked += AnimatedButton_Clicked;
if (backgroundImage != null)
{
//Set Background Image
}
}
private async void AnimatedButton_Clicked(object sender, EventArgs e)
{
//Obsolete version of OnPlatform
#pragma warning disable CS0618 // Type or member is obsolete
var platformSpecific = Device.OnPlatform<uint>(50, 50, 25); //Whats the new version of this??
#pragma warning restore CS0618 // Type or member is obsolete
//Simple Animations
await this.ScaleTo(0.85, platformSpecific, Easing.SinIn);
await this.FadeTo(0.8, platformSpecific, Easing.SinOut);
await this.FadeTo(0.95, platformSpecific, Easing.SinIn);
await this.ScaleTo(1, platformSpecific, Easing.SinOut);
}
}
}
UIバックエンドコードに、以下に示すようにUIのグリッドを更新する新しいクリックイベントも追加します。
for (int i = 0; i < count; i++)
{
grid.RowDefinitions.Add(rowCollection[i]);
AnimatedButton button = new AnimatedButton(menuOptions.ItemDataList[i].TitleText) { ButtonIndex = i+1};
button.Clicked += (s, e) =>
{
RefreshGrid(button.ButtonIndex);
};
grid.Children.Add(button, 0, i + 1);
}
明らかに、ボタンが押されたときに2つのイベントが両方とも正確に同時に実行され、アニメーションが終了する前にグリッドが更新されます。このタイプのセットアップを使用して、サブクラスのアニメーションが終了するまでRefreshGrid()呼び出しを待機させるにはどうすればよいですか?私はこの問題を処理するためにスレッドを使用しようとしていますが、運がありません。
RefreshGrid()が呼び出される前に、イベントまたはアニメーションが終了するのを待つ方法があるか、スレッドをより適切に使用する方法があるのではないかと考えています。
ありがとう!
アニメーションと同じだけ遅延を追加します。
await grid1.TranslateTo(-100, 0, 300);
await Task.Delay(300);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加