以下是大部分代码:

foreach (var file in dialog.Files.Where(someConditionsGoHere).AsEnumerable())
{
    Images imageRec = this.CreateNew<Images>();
    imageRec.Description = file.Name;
    imageRec.AsOfDate = System.DateTime.Now;
    // inits some more fields here

    using (System.IO.Stream fileStream = file.OpenRead())
    {
        byte[] fileBytes = new byte[System.Convert.ToInt32(fileStream.Length)];
        fileStream.Read(fileBytes, 0, fileBytes.Length);

        if (imageRec.Extension.ToUpper() == "TIFF" || imageRec.Extension.ToUpper() == "TIF")
        {
            // pop a yes/no dialog to convert to PDF.
            Action<UI.Interactivity.InteractionRequest.YesNoDialog.YesNoDialogConfirmation> callback = c =>
                {
                    if (c.Yes)
                    {
                        foreach (var tiffFile in dialog.Files)
                        {
                            string ext = tiffFile.Extension.Substring(1);
                            if (ext.ToUpper() != "TIF" && ext.ToUpper() != "TIFF") continue;

                            using (System.IO.Stream tiffFileStream = tiffFile.OpenRead())
                            {
                                byte[] tiffFileBytes =
                                    new byte[System.Convert.ToInt32(tiffFileStream.Length)];
                                tiffFileStream.Read(tiffFileBytes, 0, tiffFileBytes.Length);

                                Images imageRec2 = this.CreateNew<Images>();
                                imageRec2.Description = tiffFile.Name;
                                imageRec2.Extension = "pdf";
                                // some more inits and sutff here too.
                                ImagingUtilities.ConvertImgToPDF(tiffFileBytes, imageRec2)
                            }
                        }
                    }
                    else
                    {
                        this.SaveAndAddImage(imageRec, fileBytes, file.Name);
                    }
                };

            this.OpenYesNoDialog("Do you want to convert Tiff files to PDF before saving them?", callback);

        }
        else
        {
            this.SaveAndAddImage(imageRec, fileBytes, file.Name);
        }
    }
}


当前,它在循环浏览图像时显示YesNo dialog,如果该图像是".tiff"文件,它将通过callback对话框进入YesNo方法,询问用户是否要将其转换为PDF
问题是我不希望它向用户询问每个tiff文件,我只希望它询问一次,因此我应该从callback中取出该for-each loop,但是当我这样做时,我会丢失由于回调的async属性而导致命令被调用的顺序。有人可以帮忙改组吗?

最佳答案

无需进行任何真正的重写,而只需移动代码,您可以执行以下操作:

//any additional logic like filtering for if we have a tiff etc here.

    Action<UI.Interactivity.InteractionRequest.YesNoDialog.YesNoDialogConfirmation> callback = c =>
            {
                if (c.Yes)
                {
                    foreach (var tiffFile in dialog.Files.Where(x=>x.Extension.ToUpper() == "TIFF" || x.Extension.ToUpper() == "TIF"))
                    {


                        using (System.IO.Stream tiffFileStream = tiffFile.OpenRead())
                        {
                            byte[] tiffFileBytes =
                                new byte[System.Convert.ToInt32(tiffFileStream.Length)];
                            tiffFileStream.Read(tiffFileBytes, 0, tiffFileBytes.Length);

                            Images imageRec2 = this.CreateNew<Images>();
                            imageRec2.Description = tiffFile.Name;
                            imageRec2.Extension = "pdf";
                            // some more inits and sutff here too.
                            ImagingUtilities.ConvertImgToPDF(tiffFileBytes, imageRec2)
                        }
                    }
                }
                else
                {
                    this.SaveAndAddImage(imageRec, fileBytes, file.Name);
                }
            };

        this.OpenYesNoDialog("Do you want to convert Tiff files to PDF before saving them?", callback);


    //tiff already handled
    foreach (var file in dialog.Files.Where(x=>x.Conditions&& !x.Extension.ToUpper() == "TIFF" && !x.Extension.ToUpper() == "TIF").AsEnumerable())
    {
        Images imageRec = this.CreateNew<Images>();
        imageRec.Description = file.Name;
        imageRec.AsOfDate = System.DateTime.Now;
        // inits some more fields here

        using (System.IO.Stream fileStream = file.OpenRead())
        {
            byte[] fileBytes = new byte[System.Convert.ToInt32(fileStream.Length)];
            fileStream.Read(fileBytes, 0, fileBytes.Length);


            this.SaveAndAddImage(imageRec, fileBytes, file.Name);

        }

    }

10-01 12:37