首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Progressbar在xamarin fomrs的应用

Progressbar在xamarin fomrs的应用
EN

Stack Overflow用户
提问于 2018-01-30 22:25:17
回答 1查看 450关注 0票数 1

我有一个xamarin表单应用程序,其中有创建zip文件的选项。我使用SharpZipLib.Portable插件来创建zip文件。我使用相同的示例解释了这里来创建zip文件。我需要向这个应用程序添加进度条。我这样做:

代码语言:javascript
复制
double FilesCount=0;
double CopyCount = 0;

    private void CreateZipFile()
    {
        string Folder = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).Path;
        FilesCount = Directory.GetFileSystemEntries(Folder).Count();
        string ZipFilePath = Path.Combine(Folder, "File.zip");
        CreateSample(ZipFilePath, Folder);
    }

    public async void CreateSample(string outPathname, string folderName)
    {

        FileStream fsOut = File.Create(outPathname);
        ZipOutputStream zipStream = new ZipOutputStream(fsOut);

        zipStream.SetLevel(3);

        zipStream.Password = null; 

        int folderOffset = folderName.Length + (folderName.EndsWith("\\") ? 0 : 1);

        CompressFolderAsync(folderName, zipStream, folderOffset);

        zipStream.IsStreamOwner = true;
        zipStream.Close();

        await DisplayAlert("Creating Backup", "Creating backup is completed successfully. The buckup file is store in the following path: " + outPathname, "Ok");

    }

    private void CompressFolderAsync(string path, ZipOutputStream zipStream, int folderOffset)
    {
        string[] files = Directory.GetFiles(path);

        foreach (string filename in files)
        {

            FileInfo fi = new FileInfo(filename);

            string entryName = filename.Substring(folderOffset);
            entryName = ZipEntry.CleanName(entryName);
            ZipEntry newEntry = new ZipEntry(entryName);
            newEntry.DateTime = fi.LastWriteTime;
            newEntry.Size = fi.Length;
            zipStream.PutNextEntry(newEntry);

            byte[] buffer = new byte[4096];
            using (FileStream streamReader = File.OpenRead(filename))
            {
                StreamUtils.Copy(streamReader, zipStream, buffer);
            }
            zipStream.CloseEntry();
        }

        CopyCount += 1;
        CreateBackupProgress.ProgressTo(CopyCount / FilesCount, 800, Easing.Linear);


        string[] folders = Directory.GetDirectories(path);

        foreach (string folder in folders)
        {
            CompressFolderAsync(folder, zipStream, folderOffset);
        }

    }

问题是,进度条直到进程完成才会显示动画。我试图使方法异步如下:

代码语言:javascript
复制
await CreateBackupProgress.ProgressTo(CopyCount / FilesCount, 800, Easing.Linear);

但也没用。我尝试使用Task.Run()使方法在后台工作。这是代码:

代码语言:javascript
复制
    private async void CompressFolderAsync(string path, ZipOutputStream zipStream, int folderOffset)
    {
        string[] files = Directory.GetFiles(path);

        await Task.Run(() => {

            foreach (string filename in files)
        {

            FileInfo fi = new FileInfo(filename);

            string entryName = filename.Substring(folderOffset);
            entryName = ZipEntry.CleanName(entryName);
            ZipEntry newEntry = new ZipEntry(entryName);
            newEntry.DateTime = fi.LastWriteTime;
            newEntry.Size = fi.Length;
            zipStream.PutNextEntry(newEntry);

            byte[] buffer = new byte[4096];
            using (FileStream streamReader = File.OpenRead(filename))
            {
                StreamUtils.Copy(streamReader, zipStream, buffer);
            }
            zipStream.CloseEntry();
        }


            CopyCount += 1;

            CreateBackupProgress.ProgressTo(CopyCount / FilesCount, 100, Easing.Linear);

        });

        string[] folders = Directory.GetDirectories(path);

        foreach (string folder in folders)
        {
            CompressFolderAsync(folder, zipStream, folderOffset);
        }

    }

但我知道这个错误:

未处理异常: System.InvalidOperationException: ZipOutputStream已完成

我该如何解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-31 22:21:58

使用以下方法解决问题:

代码语言:javascript
复制
double FilesCount = 0;
double CopyCount = 0;

    public async void CreateSample(string outPathname, string folderName)
    {

        FileStream fsOut = File.Create(outPathname);
        ZipOutputStream zipStream = new ZipOutputStream(fsOut);

        zipStream.SetLevel(3);

        zipStream.Password = null;

        int folderOffset = folderName.Length + (folderName.EndsWith("\\") ? 0 : 1);

        CreateBackupProgress.IsVisible = true;

        Task task1 = new Task(CheckProgressbar);
        task1.Start();

        Task task2 = new Task(() => CompressFolderAsync(folderName, zipStream, folderOffset));
        task2.Start();

        await task1;
        await task2;

        zipStream.IsStreamOwner = true;
        zipStream.Close();

        await DisplayAlert("Creating Backup", "Creating backup is completed successfully. The buckup file is store in the following path: " + outPathname, "Ok");

    }

    private void CompressFolderAsync(string path, ZipOutputStream zipStream, int folderOffset)
    {
        string[] files = Directory.GetFiles(path);

        foreach (string filename in files)
        {

            FileInfo fi = new FileInfo(filename);

            string entryName = filename.Substring(folderOffset);
            entryName = ZipEntry.CleanName(entryName);
            ZipEntry newEntry = new ZipEntry(entryName);
            newEntry.DateTime = fi.LastWriteTime;
            newEntry.Size = fi.Length;
            zipStream.PutNextEntry(newEntry);

            byte[] buffer = new byte[4096];
            using (FileStream streamReader = File.OpenRead(filename))
            {
                StreamUtils.Copy(streamReader, zipStream, buffer);
            }
            zipStream.CloseEntry();
        }

        CopyCount += 1;

        string[] folders = Directory.GetDirectories(path);

        foreach (string folder in folders)
        {
            CompressFolderAsync(folder, zipStream, folderOffset);
        }

    }

    private async void CheckProgressbar()
    {

        while (CopyCount != FilesCount)
        {
            await Task.Run(() => { Thread.Sleep(100); }); 
            await CreateBackupProgress.ProgressTo(CopyCount / FilesCount, 100, Easing.Linear);

        }
        await CreateBackupProgress.ProgressTo(1, 100, Easing.Linear);

    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48531532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档