DisplayAlert aync未显示消息框

时间:2018-08-24 11:53:18

标签: c# asynchronous xamarin ftp

当试图在FTP服务器上检测到文件时,我试图在我的应用程序上显示是/否消息框,询问用户是否要删除/替换现有文件,或取消文件上传。

到目前为止,我已经获得了用于按下按钮的代码

btnUpload.Clicked += delegate
        {   
            try
            {
                if (GetDatabaseSettings() == true)
                {
                    if ( UploadtoFTPAsync().Result == true)
                    {
                        DisplayAlert("Complete", "Database file was uploaded to FTP", "OK");
                    }                                               
                }
                else
                {
                    DisplayAlert("Error", "Could not connect to FTP server. Please check your settings and try again.", "OK");
                };                           
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);

                DisplayAlert("Error", "Could not upload to FTP server. Please check your settings and try again.", "OK");
            };
        };

和这段代码来处理FTP方面的事情。

async System.Threading.Tasks.Task<bool> UploadtoFTPAsync()
        {
            string startPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "EPOSDatabase.db3");
            string zipPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "EPOSDB.zip");

            string fileToUpload = zipPath.Remove(0, zipPath.Length - 11);

            if (File.Exists(zipPath))
            {
                File.Delete(zipPath);
            }

            ZipFile.CreateFromDirectory(startPath, zipPath);
            File.SetAttributes(zipPath, FileAttributes.Normal);

            if (zipPath.StartsWith("/") == false)
            {
                zipPath = "/" + zipPath;
            }

            FtpWebRequest requestGetFile = (FtpWebRequest)FtpWebRequest.Create(ftpLocation + fileToUpload);
            requestGetFile.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
            requestGetFile.Method = WebRequestMethods.Ftp.GetFileSize;

            try
            {
                FtpWebResponse response = (FtpWebResponse)requestGetFile.GetResponse();

                var action = await DisplayAlert("", "This file already exists on the FTP area - would you like to replace it?", "No", "Yes");

                if (Convert.ToBoolean(action))
                {
                    FtpWebRequest requestDeleteFile = (FtpWebRequest)FtpWebRequest.Create(ftpLocation + fileToUpload);
                    requestDeleteFile.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
                    requestDeleteFile.Method = WebRequestMethods.Ftp.DeleteFile;

                    using (FtpWebResponse responseDelete = (FtpWebResponse)requestDeleteFile.GetResponse())
                    {
                        Console.WriteLine(response.StatusDescription);
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (WebException ex)
            {
                FtpWebResponse response = (FtpWebResponse)ex.Response;

                if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
                {
                    Console.WriteLine(ex.Message);
                }
                else
                {
                    FtpWebRequest requestDeleteFile = (FtpWebRequest)FtpWebRequest.Create(ftpLocation + fileToUpload);
                    requestDeleteFile.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
                    requestDeleteFile.Method = WebRequestMethods.Ftp.DeleteFile;

                    using (FtpWebResponse responseDelete = (FtpWebResponse)requestDeleteFile.GetResponse())
                    {
                        Console.WriteLine(response.StatusDescription);
                    }
                }

                return false;
            };

            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(ftpLocation + fileToUpload);
            request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
            request.Method = WebRequestMethods.Ftp.GetFileSize;

            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;

            FileStream stream = File.OpenRead(zipPath);
            byte[] buffer = new byte[stream.Length];

            stream.Read(buffer, 0, buffer.Length);
            stream.Close();

            Stream reqStream = request.GetRequestStream();

            int offset = 0;
            int chunk = (buffer.Length > 2048) ? 2048 : buffer.Length;

            while (offset < buffer.Length)
            {
                reqStream.Write(buffer, offset, chunk); offset += chunk;
                chunk = (buffer.Length - offset < chunk) ? (buffer.Length - offset) : chunk;
            }

            reqStream.Close();

            return true;
        };

问题在于,到达var action = await DisplayAlert("", "This file already exists on the FTP area - would you like to replace it?", "No", "Yes");行时,消息框将永远不会显示。

我在某处做错了吗?什么会停止显示确认,我该如何解决?

1 个答案:

答案 0 :(得分:0)

您应该在主线程上运行DisplayAlert。试试这个:

 Xamarin.Forms.Device.BeginInvokeOnMainThread(async() =>
 {
     var action = await DisplayAlert("", "This file already exists on the FTP area - would you like to replace it?", "No", "Yes");
     ...
 });