簡體   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