From 74e1d6edb76d6e8281138b0a34e29e54cfc14998 Mon Sep 17 00:00:00 2001 From: test Date: Sun, 23 Aug 2020 14:55:05 +0900 Subject: [PATCH] =?utf8?q?=E9=96=A2=E9=80=A3=E4=BB=98=E3=81=91=E3=81=8B?= =?utf8?q?=E3=82=89=E3=81=AE=E8=B5=B7=E5=8B=95=E3=82=82=E3=81=A7=E3=81=8D?= =?utf8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- UWP/Test/MainPage.xaml.cs | 36 ++++++++++++++++-------------------- UWP/Test/MainViewModel.cs | 33 +++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/UWP/Test/MainPage.xaml.cs b/UWP/Test/MainPage.xaml.cs index e9a21a7..0765948 100644 --- a/UWP/Test/MainPage.xaml.cs +++ b/UWP/Test/MainPage.xaml.cs @@ -32,10 +32,6 @@ namespace Test this.InitializeComponent(); this.DataContext = this.vm; //this.fooTextBox.AllowFocusOnInteraction = true; - PrintManager.GetForCurrentView().PrintTaskRequested += MainPage_PrintTaskRequested; - InputPane currentView = InputPane.GetForCurrentView(); - currentView.Showing += currentView_Showing; - currentView.Hiding += currentView_Hiding; } protected override async void OnNavigatedTo(NavigationEventArgs e) @@ -50,8 +46,7 @@ namespace Test foreach (var file in fileargs.Files) { StorageApplicationPermissions.MostRecentlyUsedList.Add(file, "mrufile"); - this.vm.AddDocument(); - await LoadFile(this.vm.CurrentDocument, file as IStorageFile); + await this.vm.AddDocumentFromFile(file as IStorageFile); } } @@ -59,6 +54,20 @@ namespace Test { this.vm.Initalize(); } + + PrintManager.GetForCurrentView().PrintTaskRequested += MainPage_PrintTaskRequested; + InputPane currentView = InputPane.GetForCurrentView(); + currentView.Showing += currentView_Showing; + currentView.Hiding += currentView_Hiding; + } + + protected override void OnNavigatedFrom(NavigationEventArgs e) + { + base.OnNavigatedFrom(e); + PrintManager.GetForCurrentView().PrintTaskRequested -= MainPage_PrintTaskRequested; + InputPane currentView = InputPane.GetForCurrentView(); + currentView.Showing -= currentView_Showing; + currentView.Hiding -= currentView_Hiding; } void currentView_Hiding(InputPane sender, InputPaneVisibilityEventArgs args) @@ -112,20 +121,7 @@ namespace Test var filepicker = new FileOpenPicker(); filepicker.FileTypeFilter.Add(".txt"); var file = await filepicker.PickSingleFileAsync(); - await LoadFile(this.vm.CurrentDocument, file); - } - - private async Task LoadFile(FooEditEngine.Document doc,IStorageFile file) - { - if (file != null) - { - using (var ws = await file.OpenAsync(FileAccessMode.Read)) - using (var fs = new StreamReader(ws.AsStream())) - { - await doc.LoadAsync(fs, null); - } - doc.RequestRedraw(); - } + await this.vm.AddDocumentFromFile(file); } private async void Print_Button_Click(object sender, RoutedEventArgs e) diff --git a/UWP/Test/MainViewModel.cs b/UWP/Test/MainViewModel.cs index 0250c39..b256439 100644 --- a/UWP/Test/MainViewModel.cs +++ b/UWP/Test/MainViewModel.cs @@ -7,12 +7,13 @@ using System.Text; using System.Threading.Tasks; using FooEditEngine; using FooEditEngine.UWP; +using Windows.Storage; namespace Test { class MainViewModel : INotifyPropertyChanged { - ObservableCollection _list = new ObservableCollection(); + static ObservableCollection _list = new ObservableCollection(); public MainViewModel() { @@ -22,7 +23,7 @@ namespace Test { get { - return this._list; + return MainViewModel._list; } } @@ -61,19 +62,35 @@ namespace Test doc.AutoComplete = new AutoCompleteBox(doc); doc.AutoComplete.Items = complete_collection; doc.AutoComplete.Enabled = true; - this._list.Add(doc); + MainViewModel._list.Add(doc); doc = new Document() { Title = "test2" }; - this._list.Add(doc); + MainViewModel._list.Add(doc); - this.CurrentDocument = this._list[0]; + this.CurrentDocument = MainViewModel._list[0]; } public void AddDocument() { - var doc = new Document() { Title = "test" + this._list.Count }; - this._list.Add(doc); - this.CurrentDocument = this._list.Last(); + var doc = new Document() { Title = "test" + MainViewModel._list.Count }; + MainViewModel._list.Add(doc); + this.CurrentDocument = MainViewModel._list.Last(); + } + + public async Task AddDocumentFromFile(IStorageFile file) + { + if (file != null) + { + var doc = new Document() { Title = "test" + MainViewModel._list.Count }; + 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(); + } } private void OnPropertyChanged(object sender, [System.Runtime.CompilerServices.CallerMemberName] string name = "") -- 2.11.0