【问题标题】:Xamarin.Forms - Make ActivityIndicator Disappear when Video LoadsXamarin.Forms - 视频加载时使 ActivityIndi​​cator 消失
【发布时间】:2018-04-26 01:27:19
【问题描述】:

Xamarin.Forms 中,我有一个ActivityIndicator 和一个label,在加载视频时会弹出。视频完全加载后,如何正确禁用它?

以下是我的 xaml (4/26 更新)

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:PreAppStructure"
         xmlns:roxv="clr-namespace:Rox;assembly=Rox.Xamarin.Video.Portable"
         x:Class="PreAppStructure.Page3"
         Title="Welcome to page 3">
<ContentPage.Content>
    <Grid>

        <roxv:VideoView x:Name="VideoView" AutoPlay="True" LoopPlay="True" ShowController="True" Source="http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4" />

        <StackLayout BackgroundColor="Black" HorizontalOptions="Center" VerticalOptions="Center" IsVisible="True">

            <ActivityIndicator Color="White"
                x:Name="loader"
                IsRunning="{Binding IsBusy}" 
            VerticalOptions="Center" HorizontalOptions="Center"

               />
            <Label x:Name ="loadingtext" Text="Loading...Please wait!" HorizontalOptions="Center" TextColor="White" IsVisible="{Binding IsBusy}"/>

        </StackLayout>

    </Grid>


</ContentPage.Content>

下面是我的 C# 类,也是我的几次尝试之一:

...
public partial class Page3 : ContentPage
{
    public Page3 ()
    {

        InitializeComponent ();

        this.BindingContext = this;

        this.IsBusy = true;


        NavigationPage.SetBackButtonTitle(this, "Back");

    }

    async void OnDoSomethingLong()
    {
        if (!this.IsBusy)
        {
            try
            {
                this.IsBusy = true;

                //await long operation here

            }
            finally
            {
                this.IsBusy = false;

                await Task.Run(() => {

                    return VideoView;

                });
            }
        }
    }

【问题讨论】:

  • 你不知道如何禁用ActivityIndi​​cator?还是您不确定视频何时完成加载?还是两者兼而有之?
  • 我相信我用“this.IsBusy = false;”行禁用了 ActivityIndi​​cator,我想说后者是我有点模糊的。我的直觉告诉我视频已在“返回 VideoView”处完成加载,所以我认为 IsBusy 代码行会在此之后进行,但这是不正确的。上线前我也试过了,没有运气。
  • 控件是否完全消失,或者是否它们消失了,但不是您希望它们消失的时间?
  • 您可以像 IsRunning 一样使用 IsVisible 属性来更改可见性。只需在 XAML 活动指示器 中添加代码。它有效
  • 因为你并没有真正展示加载视频的代码,所以很难说它什么时候完成

标签: c# xaml xamarin binding xamarin.forms


【解决方案1】:

您应该使用 IsVisible 属性来隐藏或显示活动指示器

-IsRunning 属性用于旋转活动指示器,但不适用于隐藏或显示活动指示器

  • IsVisible 属性用于隐藏或显示活动指示器,它不会打扰它是否旋转,它只是可见和不可见。

在您的情况下,您应该同时使用 IsVisible 属性和 IsRunning 属性,只要 IsBusy 为真,活动指示器可见并且会旋转,如果它为假,则不会可见并旋转。

移除 stackLayout 并将活动指示器添加到 Grid 的子级。

 <ActivityIndicator Color="White" x:Name="loader" 
 IsRunning="{Binding IsBusy}"
 IsVisible="{Binding IsBusy}"
 VerticalOptions="Center" HorizontalOptions="Center" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多