events - 如何将事件绑定到 ContentView 的 IsVisible 属性?
问题描述
我在Xamarin.Forms中创建了一个控件,允许我显示一个可重用的错误视图,以及动画。ContentView
Lottie
我的视图包含:Lottie
动画、2 Labels
(标题和描述)和“重试” Button
。
Lotie 动画是这样设置的,因为我希望动画只显示一次:
<lottie:AnimationView
x:Name="networkErrorAnimationView"
Animation="resource://lottie_error_no_network.json?assembly=ShellAppSample"
AnimationSource="EmbeddedResource"
BackgroundColor="Transparent"
AutoPlay="True"
HeightRequest="200" WidthRequest="200"
VerticalOptions="Center" HorizontalOptions="Center"
Clicked="NetworkErrorAnimationView_Clicked"/>
问题是当我从父视图显示错误控件时动画已经播放:
<ctrl:ErrorView IsVisible="{Binding ShowErrorView, Converter={StaticResource BoolToVisibilityConverter}}"
Title="{Binding ErrorTitle}"
Description="{Binding ErrorDescription}"
ErrorKind="{Binding ErrorKind}"
RetryButtonCommand="{Binding RetryCommand}" />
我想知道是否有办法将事件绑定到控件的 IsVisible 属性,以在此时重新启动相应的动画?
解决方案
您可以为您的 添加一个属性ctrl:ErrorView
,然后AutoPlay
在开始时将 设置为 false 。
这是ctrl:ErrorView
的布局。
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:forms="clr-namespace:Lottie.Forms;assembly=Lottie.Forms"
x:Name="MyErrorView"
x:Class="App13.ErrorView">
<ContentView.Content>
<StackLayout>
<forms:AnimationView
x:Name="animationView"
Animation="1.json"
AnimationSource="AssetOrBundle"
RepeatCount="1"
AutoPlay="False"
WidthRequest="100"
HeightRequest="100"/>
<Label x:Name="title" Text="test" />
<Label Text="Hello Xamarin.Forms!" />
</StackLayout>
</ContentView.Content>
</ContentView>
这是ctrl:ErrorView
的背景代码。然后我添加IsStartAnimationView
属性。如果 IsStartAnimationView
的绑定值为真。然后播放动画。
public partial class ErrorView : ContentView
{
public string TitleText
{
get { return (string)GetValue(TitleTextProperty); }
set { SetValue(TitleTextProperty, value); }
}
public static readonly BindableProperty TitleTextProperty = BindableProperty.Create(
propertyName: "TitleText",
returnType: typeof(string),
declaringType: typeof(ErrorView),
defaultValue: "",
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: TitleTextPropertyChanged);
private static void TitleTextPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
// var control = (ErrorView)bindable;
// control.title.Text = newValue.ToString();
}
public bool IsStartAnimationView
{
get { return (bool)GetValue(IsStartAnimationViewProperty); }
set { SetValue(IsStartAnimationViewProperty, value); }
}
public static readonly BindableProperty IsStartAnimationViewProperty = BindableProperty.Create(
propertyName: "IsStartAnimationView",
returnType: typeof(bool),
declaringType: typeof(ErrorView),
defaultValue: false,
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: IsStartAnimationViewChanged);
private static void IsStartAnimationViewChanged(BindableObject bindable, object oldValue, object newValue)
{
// throw new NotImplementedException();
var MyValue =(bool) newValue;
if (MyValue == true) {
var control = (ErrorView)bindable;
control.animationView.PlayAnimation();
}
}
public ErrorView()
{
InitializeComponent();
this.Content.BindingContext = this;
}
}
}
在 Contentpage 中使用此控件。
<ctrl:ErrorView IsVisible="{Binding Isfavourite}" TitleText="{Binding Name}" IsStartAnimationView="{Binding IsStart}" ></ctrl:ErrorView>
我使用按钮单击事件进行测试。
private async void Button_Clicked(object sender, System.EventArgs e)
{
myViewModel.IsStart = !myViewModel.IsStart;
}
这是运行的gif。
推荐阅读
- jquery - jQuery自动完成过滤问题
- javascript - 关键道具
- c# - 当应用程序以管理员身份运行时,打开第二个表单时是否可以要求用户权限?
- node.js - 搜索栏车把+猫鼬
- c++ - C++中的动态字符数组用C风格的字符串初始化
- javascript - 实现多语言 url nextjs 和 next-i18n-next
- java - 得到了错误的答案,但在 Google Kickstart 任务中的输出是正确的
- flutter - Flutter:有没有办法在 Flutter 中访问 Gmail(邮箱)?
- javascript - .obj 纹理和颜色未在 Three.js 中显示
- php - PHP / MySQL插入约400,000个字符失败作为cron作业