質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

Q&A

解決済

1回答

377閲覧

Xamarin.FormsのTriggerAction<VisualElement>で実装したアニメーションが動かない

panda531

総合スコア12

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

0グッド

0クリップ

投稿2018/09/21 00:36

編集2018/09/21 03:54

前提・実現したいこと

Xamarin.Formsを使ったモバイルアプリを作成中です。
UIをアニメーションさせたいと思い実装を行っているのですがうまく動きません。

XAML内でEnterActionとExitActionでのアニメーションを実行しようとしています。
以下の公式サンプルを参考に実装しました。
EnterActions and ExitActions

上記サンプルはBackgroundColorの変更ですが、実際に行いたいことはLabelのFontSizeとTextColorのアニメーションによる変更です。
また、上記サンプルはTriggerですが、実際にはDataTriggerを使ってアニメーションを開始させたいと考えています。

発生している問題・エラーメッセージ

Unhandled Exception: System.ArgumentException: Argument is null or empty Parameter name: handle at Xamarin.Forms.AnimatableKey..ctor (Xamarin.Forms.IAnimatable animatable, System.String handle) [0x0001c] in D:\a\1\s\Xamarin.Forms.Core\AnimatableKey.cs:16 at Xamarin.Forms.AnimationExtensions.AnimateInternal[T] (Xamarin.Forms.IAnimatable self, System.String name, System.Func`2[T,TResult] transform, System.Action`1[T] callback, System.UInt32 rate, System.UInt32 length, Xamarin.Forms.Easing easing, System.Action`2[T1,T2] finished, System.Func`1[TResult] repeat) [0x0001c] in D:\a\1\s\Xamarin.Forms.Core\AnimationExtensions.cs:180 (中略) ================================================================= Got a SIGABRT while executing native code. This usually indicates a fatal error in the mono runtime or one of the native libraries used by your application. =================================================================```

該当のソースコード

以下にサンプルコードを置いています。

XamarinFormsAnimationSample

MainPageは以下の通りです。

XAML

1<StackLayout> 2 <Entry Placeholder="test"> 3 <Entry.Triggers> 4 <Trigger TargetType="Entry" Property="Entry.IsFocused" Value="True"> 5 <Trigger.EnterActions> 6 <trigger:ChangeBackgroundColorTriggerAction StartsFrom="0"/> 7 </Trigger.EnterActions> 8 <Trigger.ExitActions> 9 <trigger:ChangeBackgroundColorTriggerAction StartsFrom="1"/> 10 </Trigger.ExitActions> 11 </Trigger> 12 </Entry.Triggers> 13 </Entry> 14 <Label Text="Welcome to Xamarin.Forms!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" > 15 <Label.Triggers> 16 <DataTrigger TargetType="Label" Binding="{Binding Path=m_IsChangeBackgroundColorAminationStart}" Value="true"> 17 <DataTrigger.EnterActions> 18 <trigger:ChangeBackgroundColorTriggerAction StartsFrom="0"/> 19 </DataTrigger.EnterActions> 20 <DataTrigger.ExitActions> 21 <trigger:ChangeBackgroundColorTriggerAction StartsFrom="1"/> 22 </DataTrigger.ExitActions> 23 </DataTrigger> 24 <DataTrigger TargetType="Label" Binding="{Binding Path=m_IsChangeFontSizeAnimationStart}" Value="true"> 25 <DataTrigger.EnterActions> 26 <trigger:LabelFontSizeTriggerAction FontSize="10"/> 27 </DataTrigger.EnterActions> 28 <DataTrigger.ExitActions> 29 <trigger:LabelFontSizeTriggerAction FontSize="40"/> 30 </DataTrigger.ExitActions> 31 </DataTrigger> 32 </Label.Triggers> 33 </Label> 34 <Button Command="{Binding StartChangeBackgroundColorAnimationCommand}" Text="Change BackgroundColor"/> 35 <Button Command="{Binding StartChangeFontSizeAnimationCommand}" Text="Change FontSize"/> 36</StackLayout>

試したこと

TriggerとDataTriggerの両方で試しましたがどちらも同じエラーでした。

補足情報(FW/ツールのバージョンなど)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

アニメーションの名前を指定してください。

ChangeBackgroundColorTriggerAction

C#

1protected override void Invoke(VisualElement sender) 2{ 3 sender.Animate("ChangeBackgroundColor", new Animation((d) => 4 { 5 var val = StartsFrom == 1 ? d : 1 - d; 6 sender.BackgroundColor = Color.FromRgb(1, val, 1); 7 }), 8 length: 1000, // milliseconds 9 easing: Easing.Linear); 10}

LabelFontSizeTriggerAction

C#

1protected override void Invoke(VisualElement sender) 2{ 3 var animation = new Animation((d) => 4 { 5 var label = sender as Label; 6 label.FontSize = FontSize; 7 }); 8 sender.Animate("LabelFontSize", animation, length: 1000, easing: Easing.Linear); 9}

投稿2018/09/21 01:23

f-miyu

総合スコア1625

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

panda531

2018/09/21 09:24

ありがとうございます!無事アニメーション動作しました! (フォントサイズの変更は意図した動作では無かったですがそれはまた別の問題ですね) 名前入れろってのはちゃんと公式にも書いてありました。。。 (https://docs.microsoft.com/en-us/dotnet/api/xamarin.forms.animationextensions.animate?view=xamarin-forms) An animation key that should be unique among its sibling and parent animations for the duration of the animation. ただ、参考にしたサンプルも公式だと思うんですがこっちは名前入ってないですね(; ・`ω・´) https://docs.microsoft.com/ja-jp/xamarin/xamarin-forms/app-fundamentals/triggers#enterexit このサンプル見て動かない人は他にもいそうな気がします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問