我不能在Device.StartTimer()上使用等待动画吗?

时间:2016-10-14 22:59:52

标签: xamarin.ios async-await xamarin.forms

我正在使用Xamarin.forms pcl。

制作应用程序

如果我在iOS和动画上同时使用UIRefreshControl和Scrollview。 它崩溃了!

这是UIRefreshControl的错误吗?

我在这里提供了重现示例代码。 https://github.com/myallb/test_pulltorefresh.git

示例代码非常简单。 有什么错吗?

感谢。

public partial class testPage : ContentPage
    {
        public PullToRefreshLayout RefreshView = null;
        AbsoluteLayout layout;

        public testPage()
        {
            InitializeComponent();

            layout = new AbsoluteLayout()
            {
                BackgroundColor = Color.Purple,
            };

            ScrollView scrollview = new ScrollView()
            {
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                Content = layout
            };

            RefreshView = new PullToRefreshLayout
            {
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                Content = scrollview,
                RefreshColor = Color.Red,
                RefreshCommand = new Command(RefreshStart)
            };
            RefreshView.IsPullToRefreshEnabled = true;

            Content = RefreshView;

            Device.StartTimer(new TimeSpan(0, 0, 1), ani);
        }


        bool ani()
        {
            Label z = new Label()
            {
                Text = "Z",
                TextColor = Color.White,
                FontAttributes = FontAttributes.Bold,
                FontSize = new Random().Next(22, 35)
            };

            AbsoluteLayout.SetLayoutBounds(z, new Rectangle(0.67 + new Random().Next(0, 10) / 100.0, 0.13 + new Random().Next(0, 10) / 100.0, 40, 40));
            AbsoluteLayout.SetLayoutFlags(z, AbsoluteLayoutFlags.PositionProportional);
            layout.Children.Add(z);

            Device.BeginInvokeOnMainThread(async () =>
            {
                Task t1 = z.FadeTo(0, 3500);
                Task t2 = z.TranslateTo(0, -70, 3500, Easing.SinInOut);

                await Task.WhenAll(t1, t2);
                layout.Children.Remove(z);
            });

            return true;
        }

        void RefreshStart()
        {
            Debug.WriteLine("RefreshStart");

            if (RefreshView != null)
                RefreshView.IsRefreshing = true;

            Device.BeginInvokeOnMainThread(async () =>
            {
                await Task.Delay(20);

                Debug.WriteLine("RefreshEnd");
                RefreshView.IsRefreshing = false;
            });
        }
    }

0 个答案:

没有答案