如何在Xamarin.Forms中为旋转木马实现活动指示器?

时间:2015-09-03 17:12:52

标签: c# xamarin xamarin.forms

我试图在启动Carousel页面之前合并一个ActivityIndi​​cator;这样,当Carousel页面内容很重时;用户将知道应用程序仍在加载/没有挂起。

为了解决这个问题,我在这个使用Carousel页面的示例中工作: https://github.com/chrisriesgo/xamarin-forms-carouselview

我的主要方法是尝试修改按钮以在按钮命令中创建ActivityIndi​​cator:

来自" SwitcherPage.cs"

的原始样本
var none = new Button {
            HorizontalOptions = LayoutOptions.Center,
            Text = "No pager indicator",
            Command = new Command((obj) => Navigation.PushAsync(new HomePage(CarouselLayout.IndicatorStyleEnum.None)))
        };

我的尝试

var indicator = new ActivityIndicator ();

        var none = new Button {
            HorizontalOptions = LayoutOptions.Center,
            Text = "No pager indicator",
            Command = new Command(
                indicator.IsRunning,
                indicator.IsVisible,
                Task.Delay(2500), // to simulate a long loading time
                (obj) => Navigation.PushAsync(new HomePage(CarouselLayout.IndicatorStyleEnum.None)))
        };

到目前为止我所知道的

Intellisense已经告诉我这是错的;我无法通过这种方式在命令中定义它。但希望这说明了我想要做的事情;实质上在等待页面加载时触发活动指示器。

我已经在ActivityIndi​​cator上查找了一般资源,但问题是我找不到任何我可以弄清楚如何将其转换为在按下按钮等待时激活的内容要加载的Carousel页面。

https://developer.xamarin.com/api/type/Xamarin.Forms.ActivityIndicator/ http://forums.xamarin.com/discussion/34502/xamarin-forms-activity-indicator Xamarin.Forms - How to overlay an ActivityIndicator in the middle of a StackLayout programmatically

2 个答案:

答案 0 :(得分:0)

我不确定我是否正确理解了您的问题,但是:

    var indicator = new ActivityIndicator ();

    var none = new Button {
        HorizontalOptions = LayoutOptions.Center,
        Text = "No pager indicator",
        Command = new Command(async() => {
            indicator.IsRunning = true;
            indicator.IsVisible = true;
            await Task.Delay(2500);
            indicator.IsRunning = false;
            indicator.IsVisible = false;
            Navigation.PushAsync(new HomePage(CarouselLayout.IndicatorStyleEnum.None));
        })
    };

但是,为ViewModel属性设置适当的绑定并从那里做事情会更加清晰。

答案 1 :(得分:0)

另一种解决方案,使用Device.StartTimer

var indicator = new ActivityIndicator ();

var none = new Button {
    HorizontalOptions = LayoutOptions.Center,
    Text = "No pager indicator",
    Command = new Command(() => {
        indicator.IsRunning = true;
        indicator.IsVisible = true;
        Device.StartTimer(Timespan.FromSeconds(2.5),()=>
        {
            indicator.IsRunning = false;
            indicator.IsVisible = false;
            Navigation.PushAsync(new HomePage(CarouselLayout.IndicatorStyleEnum.None));
            return false;
        });
    });

};