Ver Fonte

其他-修复打开多个office文件,显示异常问题

ZhouJieSheng há 2 anos atrás
pai
commit
cc083f02ef

+ 4 - 5
PDF Office/ViewModels/HomeContentViewModel.cs

@@ -78,11 +78,6 @@ namespace PDF_Office.ViewModels
             CreateFromOtherFile = new DelegateCommand(createFromOtherFile);
             CreateFromHtmlCommnd = new DelegateCommand(createFormHtml);
             CreateFromScanner = new DelegateCommand(createFromScanner);
-
-            System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
-            {
-                toolregion.RequestNavigate(ToolRegionName, "Guid");
-            }));
         }
 
         /// <summary>
@@ -126,6 +121,7 @@ namespace PDF_Office.ViewModels
                 }
 
                 IsLoading = Visibility.Visible;
+                await Task.Delay(10);
                 //在当前页签打开第一个文件
                 var type = System.IO.Path.GetExtension(fileList[0]).ToLower();
                 if(image.Contains(type))
@@ -142,6 +138,7 @@ namespace PDF_Office.ViewModels
                     if (!App.OpenedFileList.Contains(fileList[i]))
                     {
                         App.mainWindowViewModel.AddTabItem(fileList[i]);
+                        await Task.Delay(50);
                     }
                     ToolMethod.SetFileThumbImg(fileList[i]);
                 }
@@ -237,6 +234,8 @@ namespace PDF_Office.ViewModels
             {
                 mainContentViewModel = mainVM;
             }
+
+            toolregion.RequestNavigate(ToolRegionName, "Guid");
         }
 
         public bool IsNavigationTarget(NavigationContext navigationContext)

+ 77 - 27
PDF Office/ViewModels/MainContentViewModel.cs

@@ -34,7 +34,9 @@ namespace PDF_Office.ViewModels
         public string FileName
         {
             get { return fileName; }
-            set { SetProperty(ref fileName, value);
+            set
+            {
+                SetProperty(ref fileName, value);
                 if (IsReNameTextShow == Visibility.Visible)
                 {
                     dorenameFile();
@@ -172,11 +174,11 @@ namespace PDF_Office.ViewModels
 
         private void showInFolder()
         {
-            if(!string.IsNullOrEmpty(PDFViewer.Document.FilePath))
+            if (!string.IsNullOrEmpty(PDFViewer.Document.FilePath))
             {
                 CommonHelper.ShowFileBrowser(PDFViewer.Document.FilePath);
             }
-   
+
         }
 
         private void CloseTabItem(object item)
@@ -210,7 +212,7 @@ namespace PDF_Office.ViewModels
                 {
                     App.OpenedFileList.Add(filePath);
                 }
-         
+
             }
             //打开文件后,不管是新文件还是旧文件都需要更新排序
             SettingHelper.SortRecentOpenFiles(filePath);
@@ -225,11 +227,11 @@ namespace PDF_Office.ViewModels
                 { ParameterNames.MainViewModel, this },
                 { ParameterNames.PDFViewer,PDFViewer}
             };
-            
+
             toolregion.RequestNavigate(MainContentRegionName, "ViewContent", parameters);
 
             IsReNameEnable = true;
-            if(!string.IsNullOrEmpty(PDFViewer.Document.FilePath))
+            if (!string.IsNullOrEmpty(PDFViewer.Document.FilePath))
             {
                 IsShowInFolderEnable = true;
             }
@@ -300,7 +302,7 @@ namespace PDF_Office.ViewModels
             if (PDFViewer.Document == null)
             {
                 AlertsMessage alertsMessage = new AlertsMessage();
-                alertsMessage.ShowDialog("","创建文件失败.","OK");
+                alertsMessage.ShowDialog("", "创建文件失败.", "OK");
                 return false;
             }
 
@@ -382,9 +384,9 @@ namespace PDF_Office.ViewModels
 
                 string ex = System.IO.Path.GetExtension(sourcepath).ToLower();
 
+                await Task.Delay(10);
                 switch (ex)
                 {
-
                     case ".doc":
                     case ".docx":
                     case "docm":
@@ -395,7 +397,6 @@ namespace PDF_Office.ViewModels
                     case ".html":
                         await Task.Run(() =>
                         {
-
                             Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                             Microsoft.Office.Interop.Word.Document document = null;
 
@@ -404,13 +405,17 @@ namespace PDF_Office.ViewModels
                             document = word.Documents.Open(sourcepath);
                             var page = document.PageSetup;
                             page.PaperSize = paperSize;
-                            if(margin>0)
+                            if (margin > 0)
                             {
                                 page.LeftMargin = page.TopMargin = page.RightMargin = page.BottomMargin = (float)margin;
                             }
                             document?.ExportAsFixedFormat(targetPath, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
-                            document?.Close();
-                            word?.Quit();
+                            document?.Close(false);
+                            if (word != null)
+                            {
+                                word.NormalTemplate.Saved = true;
+                                word.Quit();
+                            }
                         });
                         break;
                     case ".xls":
@@ -470,7 +475,7 @@ namespace PDF_Office.ViewModels
                 if (PDFViewer.Document == null)
                 {
                     AlertsMessage alertsMessage = new AlertsMessage();
-                    alertsMessage.ShowDialog("","创建PDF失败","OK");
+                    alertsMessage.ShowDialog("", "创建PDF失败", "OK");
                     return false;
                 }
 
@@ -492,14 +497,45 @@ namespace PDF_Office.ViewModels
                 FileChanged = Visibility.Visible;
                 PDFViewer.SetFormFieldHighlight(true);
 
-                NavigateToViewContent();
+                await  System.Windows.Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
+                {
+                    NavigateToViewContent();
+                }));
 
                 return true;
             }
             catch (Exception ex)
             {
+                string str = "没有安装";
+                //判断是否有安装office软件
+                var officeType = Type.GetTypeFromProgID("Word.Application");
+                if (officeType == null)
+                {
+                    str += "Word;";
+                }
+
+                officeType = Type.GetTypeFromProgID("Excel.Application");
+                if (officeType == null)
+                {
+                    str += "Excel;";
+                }
+
+                officeType = Type.GetTypeFromProgID("Powerpoint.Application");
+                if (officeType == null)
+                {
+                    str += "Excel;";
+                }
+
+
                 AlertsMessage alertsMessage = new AlertsMessage();
-                alertsMessage.ShowDialog("", "没有安装Office", "OK");
+                if (str != "没有安装")
+                {
+                    alertsMessage.ShowDialog("", "没有安装Office", "OK");
+                }
+                else
+                {
+                    alertsMessage.ShowDialog("", "创建PDF失败", "OK");
+                }
                 return false;
             }
         }
@@ -528,38 +564,52 @@ namespace PDF_Office.ViewModels
         public async void OnNavigatedTo(NavigationContext navigationContext)
         {
             mainWindowViewModel = App.mainWindowViewModel;
+            //因为是异步打开多个文件,需要先显示Home界面
+            NavigationParameters parameters = new NavigationParameters();
+            parameters.Add(ParameterNames.MainViewModel, this);
+            if (toolregion.Regions.ContainsRegionWithName(MainContentRegionName))
+            {
+                toolregion.RequestNavigate(MainContentRegionName, "HomeContent", parameters);
+            }
             //常规加载首页的情况
             if (navigationContext.Parameters.Count <= 0)
             {
-                NavigationParameters parameters = new NavigationParameters();
-                parameters.Add(ParameterNames.MainViewModel,this);
-                if (toolregion.Regions.ContainsRegionWithName(MainContentRegionName))
-                {
-                    toolregion.RequestNavigate(MainContentRegionName, "HomeContent", parameters);
-                }
                 return;
             }
 
             //一次打开多个文件的情况
             var filepath = navigationContext.Parameters[ParameterNames.FilePath].ToString();
-            if (filepath!= null)
+            if (filepath != null)
             {
-                if(System.IO.Path.GetExtension(filepath.ToString()).ToLower()==".pdf")
+                bool result = true;
+                if (System.IO.Path.GetExtension(filepath.ToString()).ToLower() == ".pdf")
                 {
                     OpenFile(filepath.ToString());
                 }
-                else if(Properties.Resources.imageex.Contains(System.IO.Path.GetExtension(filepath).ToLower()))
+                else if (Properties.Resources.imageex.Contains(System.IO.Path.GetExtension(filepath).ToLower()))
                 {
                     //图片转PDF
-                    CreateFile(filepath);
+                    result = CreateFile(filepath);
                 }
                 else
                 {
-                    await CreateFileFromOffice(filepath);
+                    result = await CreateFileFromOffice(filepath);
+
                 }
+
+                if (!result)
+                {
+                    mainWindowViewModel.CloseTabItem(mainWindowViewModel.SelectedItem);
+                    return;
+                }
+
+                //更新已打开的文件记录
+                App.OpenedFileList.Add(filepath);
             }
 
-            
+
+          
+
         }
 
         public bool IsNavigationTarget(NavigationContext navigationContext)