首页 > 解决方案 > Xamarin Forms Lottie 图像未显示

问题描述

我有 Xamarin Forms 应用程序。我需要将图像作为 json 放在项目中。

这是 XAML:

 <ContentPage 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:Class="xxx.Views.ScanReadyPage">
    <StackLayout>

            <forms:AnimationView 
                x:Name="lottie" 
                Animation="12240-cash-car.json" 
                Loop="False" 
                AutoPlay="True" 
                WidthRequest="200" 
                HeightRequest="200" 
                Margin="10,0,10,0" 
                VerticalOptions="FillAndExpand" 
                HorizontalOptions="FillAndExpand" />
</StackLayout>

    </ContentPage.Content>
</ContentPage>

我已经安装了 Com.Airbnb.Xamarin.Forms.Lottie 版本 3.0.3 和 Xamarin.Forms 版本 4.3.0.991221

我已将从 lottiefiles.com: 12240-cash-car.json 下载的文件添加到 XamarinForms.Android 项目的 Assets 文件夹中,并将构建操作设置为 Android Asset。

Android 上没有显示任何内容。输出时没有信息,没有发生错误。我已将 Xamarin Forms 更改为 3.6.0 - 没有更改。我已经安装了 Lottie 的所有版本:从 2.6.3 到 3.0.3。- 没有显示图像。(版本 2.6.3 和更早版本有关于缺少密钥库的错误)。

我检查了版本后面的代码,但它也不起作用:

 public MainPage()
                {
                    element = new AnimationView()
                {
                    Loop = true,
                    AutoPlay = true,
                    Animation = "12230-bounce-down-steps.json",
                    WidthRequest = 400,
                    HeightRequest = 400,
                    VerticalOptions = LayoutOptions.FillAndExpand,
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    IsVisible = true,
                    Speed = 1
                };
            }

            AnimationView element = new AnimationView();

            protected override void OnAppearing()
            {
                element.Animation = "12230-bounce-down-steps.json";
                element.Play();
            }

如何使它起作用?

标签: xamarinxamarin.formslottie

解决方案


我在这里找到了解决方案:https ://iinteractive.com/notebook/2017/05/31/lottie-xamarin.html 。在所有 3 个项目中开始安装“Com.Airbnb.Xamarin.Forms.Lottie”。然后在 Android 项目中单独安装另一个库:“Com.Airbnb.Android.Lottie”。现在您可以转到 MainActivity.cs,输入“使用 Lottie.Forms.Droid”和“AnimationViewRenderer.Init(); 并且 json 图像正确显示。感谢Guilherme Nimer的好提示。


推荐阅读