Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin.表单活动指示器和绑定_C#_Binding_Xamarin.forms_Activity Indicator - Fatal编程技术网

C# Xamarin.表单活动指示器和绑定

C# Xamarin.表单活动指示器和绑定,c#,binding,xamarin.forms,activity-indicator,C#,Binding,Xamarin.forms,Activity Indicator,我有一个非常简单的页面(stacklayout中的一个按钮和一个activityindicator)。我尝试使用我用INotifyPropertyChanged编写的通知类使用绑定来激活/禁用activityindicator 当我按下按钮时,我希望activityindicator可见并运行 buttonclick事件中的代码运行(这是一个ftp…我想异步执行,但目前尚未成功),“绑定”属性(可见并运行通知类)似乎会更改状态,但activityindicator不会显示 我不知道这是不是一个绑

我有一个非常简单的页面(stacklayout中的一个按钮和一个activityindicator)。我尝试使用我用INotifyPropertyChanged编写的通知类使用绑定来激活/禁用activityindicator

当我按下按钮时,我希望activityindicator可见并运行

buttonclick事件中的代码运行(这是一个ftp…我想异步执行,但目前尚未成功),“绑定”属性(可见并运行通知类)似乎会更改状态,但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));
                }
            }
        }
    }
}
使用系统;
使用Xamarin.Forms;
使用FlagFtp;
使用系统组件模型;
使用System.Runtime.CompilerServices;
名称空间Geco
{
公共类页面FTP:ContentPage
{
私有通知_Notification=新通知();
公共页面FTP()
{
this.Title=“Carico数据库”;
var stacklayout=新的stacklayout();
stacklayout.HorizontalOptions=LayoutOptions.Center;
stacklayout.VerticalOptions=LayoutOptions.Center;
var activityIndicator=新的activityIndicator();
activityIndicator.IsEnabled=true;
activityIndicator.SetBinding(activityIndicator.IsVisibleProperty,“可见”);
activityIndicator.SetBinding(activityIndicator.IsRunning属性,“正在运行”);
activityIndicator.BindingContext=\u通知;
bool-okFtp=true;
字符串errorFtp=“”;
//验证数据库
var filename=DependencyService.Get().GetFileName(App.DB\u filename);
#如果调试
DependencyService.Get().Delete(文件名);
#恩迪夫
var buttonrery=new按钮();
buttonRetry.Text=“Procedere con il carico del database”;
按钮重试。单击+=(对象发送者,事件参数)=>{
okFtp=ftp(文件名,ref errorFtp);
如果(okFtp)
DependencyService.Get().OpenActivity(App.EnumForms.Login);
否则{
显示警报(“错误”,错误FTP,“正常”);
}
};
stacklayout.Children.Add(按钮重试);
stacklayout.Children.Add(activityIndicator);
this.Content=stacklayout;
}
私有bool ftp(字符串文件名,引用字符串错误){
System.Net.NetworkCredential credentials=新的System.Net.NetworkCredential();
credentials.UserName=“alessandro”;
凭证。密码=“爱尔兰”;
bool ok=false;
尝试
{
_notification.Visible=true;
_notification.Running=true;
FlagFtp.FtpClient FtpClient=新的FtpClient(凭证);
字符串uri=”ftp://192.168.0.102/GECOl.sqlite";
FtpFileInfo FtpFileInfo=ftpClient.GetFileInfo(新Uri(Uri));
FtpStream FtpStream=ftpClient.OpenRead(ftpFileInfo);
字节[]缓冲区=新字节[ftpFileInfo.Length];
ftpstream.Read(缓冲区,0,(int)ftpFileInfo.Length);
DependencyService.Get().SaveBytes(文件名、缓冲区);
ok=正确;
}
捕获(例外情况除外){
错误=例如消息;
}
最后{
_通知。可见=假;
_notification.Running=false;
}
返回ok;
}
公共类通知:INotifyPropertyChanged
{
private bool_visible=false;
公众视野{
获取{return\u visible;}
集合{
if(value.Equals(_可见))
返回;
_可见=价值;
OnPropertyChanged();
}
}
private bool_running=false;
公营学校{
获取{return\u running;}
集合{
if(value.Equals(_running))
返回;
_运行=价值;
OnPropertyChanged();
}
}
公共事件属性更改事件处理程序属性更改;
void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
var handler=PropertyChanged;
if(处理程序!=null)
{
处理程序(这是新的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 ()
        {
        }
    }
}