OSDN Git Service

ドキュメントリストをシングルトンで保持すると落ちるらしい
authortest <test@yahoo.co.jp>
Mon, 24 Aug 2020 12:30:23 +0000 (21:30 +0900)
committertest <test@yahoo.co.jp>
Mon, 24 Aug 2020 12:30:23 +0000 (21:30 +0900)
UWP/Test/App.xaml.cs
UWP/Test/MainPage.xaml.cs
UWP/Test/MainViewModel.cs

index 886283d..9faa7c4 100644 (file)
@@ -79,7 +79,7 @@ namespace Test
             }
         }
 
-        protected override void OnFileActivated(FileActivatedEventArgs e)
+        protected override async void OnFileActivated(FileActivatedEventArgs e)
         {
             base.OnFileActivated(e);
             Frame rootFrame = Window.Current.Content as Frame;
@@ -90,7 +90,15 @@ namespace Test
                 rootFrame.NavigationFailed += OnNavigationFailed;
                 Window.Current.Content = rootFrame;
             }
-            rootFrame.Navigate(typeof(MainPage), e);
+            if (rootFrame.Content == null)
+            {
+                rootFrame.Navigate(typeof(MainPage), e);
+            }
+            else
+            {
+                MainPage page = rootFrame.Content as MainPage;
+                await page.LoadManyFiles(e);
+            }
             Window.Current.Activate();
         }
 
index 0765948..ffdd845 100644 (file)
@@ -39,16 +39,7 @@ namespace Test
             var fileargs = e.Parameter as FileActivatedEventArgs;
             if (fileargs != null)
             {
-                var filepaths = from file in fileargs.Files
-                                select file.Path;
-
-                //MRUに追加しないと後で開けない
-                foreach (var file in fileargs.Files)
-                {
-                    StorageApplicationPermissions.MostRecentlyUsedList.Add(file, "mrufile");
-                    await this.vm.AddDocumentFromFile(file as IStorageFile);
-                }
-
+                await this.LoadManyFiles(fileargs);
             }
             else if(this.vm.DocumentList.Count == 0)
             {
@@ -61,6 +52,20 @@ namespace Test
             currentView.Hiding += currentView_Hiding;
         }
 
+        public async Task LoadManyFiles(FileActivatedEventArgs fileargs)
+        {
+            var filepaths = from file in fileargs.Files
+                            select file.Path;
+
+            //MRUに追加しないと後で開けない
+            foreach (var file in fileargs.Files)
+            {
+                StorageApplicationPermissions.MostRecentlyUsedList.Add(file, "mrufile");
+                await this.vm.AddDocumentFromFile(file as IStorageFile);
+            }
+
+        }
+
         protected override void OnNavigatedFrom(NavigationEventArgs e)
         {
             base.OnNavigatedFrom(e);
index 310c8a6..63122c7 100644 (file)
@@ -13,7 +13,7 @@ namespace Test
 {
     class MainViewModel : INotifyPropertyChanged
     {
-        static ObservableCollection<Document> _list = new ObservableCollection<Document>();
+        ObservableCollection<Document> _list = new ObservableCollection<Document>();
 
         public MainViewModel()
         {
@@ -23,7 +23,7 @@ namespace Test
         {
             get
             {
-                return MainViewModel._list;
+                return this._list;
             }
         }
 
@@ -63,35 +63,37 @@ namespace Test
             doc.AutoComplete.Items = complete_collection;
             doc.AutoComplete.Enabled = true;
             doc.ShowLineBreak = true;
-            MainViewModel._list.Add(doc);
+            _list.Add(doc);
 
             doc = new Document() { Title = "test2" };
-            MainViewModel._list.Add(doc);
+            _list.Add(doc);
 
-            this.CurrentDocument = MainViewModel._list[0];
+            this.CurrentDocument = _list[0];
         }
 
         public void AddDocument()
         {
-            var doc = new Document() { Title = "test" + MainViewModel._list.Count };
-            MainViewModel._list.Add(doc);
-            this.CurrentDocument = MainViewModel._list.Last();
+            var doc = new Document() { Title = "test" + _list.Count };
+            _list.Add(doc);
+            this.CurrentDocument = _list.Last();
         }
 
         public async Task AddDocumentFromFile(IStorageFile file)
         {
             if (file != null)
             {
-                var doc = new Document() { Title = "test" + MainViewModel._list.Count };
+                var doc = new Document() { Title = "test" + _list.Count };
                 doc.ShowLineBreak = true;
+                doc.ShowFullSpace = true;
+                doc.ShowTab = true;
                 using (var ws = await file.OpenAsync(FileAccessMode.Read))
                 using (var fs = new StreamReader(ws.AsStream()))
                 {
                     await doc.LoadAsync(fs, null);
                 }
                 doc.RequestRedraw();
-                MainViewModel._list.Add(doc);
-                this.CurrentDocument = MainViewModel._list.Last();
+                _list.Add(doc);
+                this.CurrentDocument = _list.Last();
             }
         }