繁体   English   中英

Xamarin.Forms ActivityIndi​​cator和绑定

[英]Xamarin.Forms ActivityIndicator and Binding

我有一个非常简单的页面(堆栈布局中的一个按钮和一个活动指示器)。 我尝试通过我用INotifyPropertyChanged编写的通知类,使用绑定来激活/停用activityindicator。

当我按下按钮时,我希望活动指示器可见并正在运行。

buttonclick事件中的代码运行(这是一个ftp ...我想使其异步执行,但现在我还没有成功),“ bind”属性(“ Visible and Running of Notification”类)似乎改变了状态,但是activityindicator没有出现。

我不知道这是不是一个装订问题,一个布局问题,还有什么其他问题。 谁能帮我?

这是页面

    using System;
using Xamarin.Forms;
using FlagFtp;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Geco
{
    public class PageFtp: ContentPage
    {
        private Notification _notification = new Notification ();
        public PageFtp ()
        {
            this.Title = "Carico database";

            var stacklayout = new StackLayout ();
            stacklayout.HorizontalOptions = LayoutOptions.Center;
            stacklayout.VerticalOptions = LayoutOptions.Center;

            var activityIndicator = new ActivityIndicator ();
            activityIndicator.IsEnabled = true;
            activityIndicator.SetBinding (ActivityIndicator.IsVisibleProperty, "Visible");
            activityIndicator.SetBinding (ActivityIndicator.IsRunningProperty, "Running");
            activityIndicator.BindingContext = _notification;

            bool okFtp = true;
            string errorFtp = "";

            // Verifico se ho il database
            var filename = DependencyService.Get<IFiles> ().GetFileName (App.DB_FILENAME);
            #if DEBUG
            DependencyService.Get<IFiles>().Delete(filename);
            #endif

            var buttonRetry = new Button ();
            buttonRetry.Text = "Procedere con il carico del database";
            buttonRetry.Clicked += (object sender, EventArgs e) =>{
                okFtp = ftp (filename, ref errorFtp);
                if(okFtp)
                    DependencyService.Get<IOpenActivity>().OpenActivity(App.EnumForms.Login);
                else{
                    DisplayAlert("Errore",errorFtp,"OK");
                }
            };

            stacklayout.Children.Add (buttonRetry);
            stacklayout.Children.Add (activityIndicator);
            this.Content = stacklayout;

        }


        private bool ftp(string filename, ref string error) {
            System.Net.NetworkCredential credentials = new System.Net.NetworkCredential ();
            credentials.UserName = "alessandro";
            credentials.Password = "ireland";
            bool ok = false;
            try
            {
                _notification.Visible = true;
                _notification.Running = true;

                FlagFtp.FtpClient ftpClient = new FtpClient (credentials);
                string uri = "ftp://192.168.0.102/GECOl.sqlite";
                FtpFileInfo ftpFileInfo = ftpClient.GetFileInfo (new Uri (uri));
                FtpStream ftpstream = ftpClient.OpenRead (ftpFileInfo);
                byte[] buffer = new byte[ftpFileInfo.Length];
                ftpstream.Read (buffer, 0,(int) ftpFileInfo.Length);
                DependencyService.Get<IFiles> ().SaveBytes (filename, buffer);
                ok = true;
            }
            catch(Exception ex) {
                error = ex.Message;
            }
            finally {
                _notification.Visible = false;
                _notification.Running = false;
            }

            return ok;
        }

        public class Notification : INotifyPropertyChanged
        {
            private bool _visible = false ;
            public bool Visible {
                get { return _visible; }
                set { 
                    if (value.Equals (_visible))
                        return;
                    _visible = value;
                    OnPropertyChanged ();
                }
            }

            private bool _running = false;
            public bool Running  {
                get { return _running; }
                set { 
                    if (value.Equals (_running))
                        return;
                    _running = value;
                    OnPropertyChanged ();
                }

            }

            public event PropertyChangedEventHandler PropertyChanged;

            void OnPropertyChanged([CallerMemberName]String propertyName=null)
            {
                var handler=PropertyChanged;
                if(handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    }
}

您好,我正在使用进度对话框来执行此操作(Android版本)。 当我想显示加载屏幕时,我只使用:LoadingViewFlag = true / false,而不是使用ContentPage,而是使用我的类(BaseContentPage):

using System;
using Xamarin.Forms;
#if __ANDROID__
using Android.App;
#endif

namespace SEEForgeX.Helpers
{
    public class BaseContentPage : ContentPage
    {
        #region PRIVATE VARIABLES
#if __ANDROID__
        ProgressDialog p = null;
#endif
        #endregion

        #region PROPERTIES
        public bool IsShowing { get; set; }
        public bool LoadingViewFlag {
            get {
                return (bool)GetValue (LoadingProperty);
            }
            set {
                SetValue (LoadingProperty, value);

#if __ANDROID__
                if (value == true)
                {
                    p = new ProgressDialog(Forms.Context);
                    p.SetMessage("Loading...");
                    p.SetCancelable(false);
                    p.Show();
                    IsShowing = true;
                }
                else
                {
                    if (p != null)
                    {
                        p.Dismiss();
                        p = null;
                        IsShowing = false;
                    }
                }
#endif
            }
        }

        public static readonly BindableProperty LoadingProperty = 
            BindableProperty.Create ((BaseContentPage w) => w.LoadingViewFlag, false);
        #endregion

        public BaseContentPage ()
        {
        }
    }
}

Fir iOS我正在使用自定义渲染器。 让我知道你是否需要它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM