From f3c786bf57daecb47821e2350b27cc5568d76e63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Kub=C3=AD=C4=8Dek?= Date: Sun, 7 Jun 2026 21:57:16 +0200 Subject: [PATCH] =?UTF-8?q?P=C5=99id=C3=A1n=C3=AD=20str=C3=A1nky=20p=C5=99?= =?UTF-8?q?ehledu=20v=C5=A1ech=20pozn=C3=A1mek,=20p=C5=99eps=C3=A1n=C3=AD?= =?UTF-8?q?=20na=20MVVM.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- winui-learning/winui-learning/App.xaml.cs | 16 ----- winui-learning/winui-learning/MainWindow.xaml | 4 +- .../winui-learning/MainWindow.xaml.cs | 2 + .../winui-learning/Models/AllNotesModel.cs | 52 ++++++++++++++ .../winui-learning/Models/NoteModel.cs | 37 ++++++++-- .../winui-learning/Views/AllNotesPage.xaml | 69 +++++++++++++++++++ .../winui-learning/Views/AllNotesPage.xaml.cs | 18 +++++ .../winui-learning/Views/NotePage.xaml | 22 +++--- .../winui-learning/Views/NotePage.xaml.cs | 45 +++--------- .../winui-learning/winui-learning.csproj | 6 +- 10 files changed, 200 insertions(+), 71 deletions(-) create mode 100644 winui-learning/winui-learning/Models/AllNotesModel.cs create mode 100644 winui-learning/winui-learning/Views/AllNotesPage.xaml create mode 100644 winui-learning/winui-learning/Views/AllNotesPage.xaml.cs diff --git a/winui-learning/winui-learning/App.xaml.cs b/winui-learning/winui-learning/App.xaml.cs index 136201d..936e62f 100644 --- a/winui-learning/winui-learning/App.xaml.cs +++ b/winui-learning/winui-learning/App.xaml.cs @@ -1,20 +1,4 @@ using Microsoft.UI.Xaml; -using Microsoft.UI.Xaml.Controls; -using Microsoft.UI.Xaml.Controls.Primitives; -using Microsoft.UI.Xaml.Data; -using Microsoft.UI.Xaml.Input; -using Microsoft.UI.Xaml.Media; -using Microsoft.UI.Xaml.Navigation; -using Microsoft.UI.Xaml.Shapes; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices.WindowsRuntime; -using Windows.ApplicationModel; -using Windows.ApplicationModel.Activation; -using Windows.Foundation; -using Windows.Foundation.Collections; // To learn more about WinUI, the WinUI project structure, // and more about our project templates, see: http://aka.ms/winui-project-info. diff --git a/winui-learning/winui-learning/MainWindow.xaml b/winui-learning/winui-learning/MainWindow.xaml index 132acfc..672a488 100644 --- a/winui-learning/winui-learning/MainWindow.xaml +++ b/winui-learning/winui-learning/MainWindow.xaml @@ -31,7 +31,7 @@ - + diff --git a/winui-learning/winui-learning/MainWindow.xaml.cs b/winui-learning/winui-learning/MainWindow.xaml.cs index cafae12..0bc5dca 100644 --- a/winui-learning/winui-learning/MainWindow.xaml.cs +++ b/winui-learning/winui-learning/MainWindow.xaml.cs @@ -13,6 +13,8 @@ public sealed partial class MainWindow : Window public MainWindow() { InitializeComponent(); + ExtendsContentIntoTitleBar = true; + SetTitleBar(AppTitleBar); } } diff --git a/winui-learning/winui-learning/Models/AllNotesModel.cs b/winui-learning/winui-learning/Models/AllNotesModel.cs new file mode 100644 index 0000000..6c98e59 --- /dev/null +++ b/winui-learning/winui-learning/Models/AllNotesModel.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Threading.Tasks; +using Windows.Storage; + +namespace winui_learning.Models; + +public class AllNotesModel +{ + public ObservableCollection Notes { get; set; } = new ObservableCollection(); + + public AllNotesModel() + { + LoadNotes(); + } + + public async void LoadNotes() + { + Notes.Clear(); + // Get the folder where the notes are stored. + StorageFolder storageFolder = + ApplicationData.Current.LocalFolder; + await GetFilesInFolderAsync(storageFolder); + } + + private async Task GetFilesInFolderAsync(StorageFolder folder) + { + // Each StorageItem can be either a folder or a file. + IReadOnlyList storageItems = + await folder.GetItemsAsync(); + foreach (IStorageItem item in storageItems) + { + if (item.IsOfType(StorageItemTypes.Folder)) + { + // Recursively get items from subfolders. + await GetFilesInFolderAsync((StorageFolder)item); + } + else if (item.IsOfType(StorageItemTypes.File)) + { + StorageFile file = (StorageFile)item; + NoteModel note = new NoteModel() + { + Filename = file.Name, + Text = await FileIO.ReadTextAsync(file), + Date = file.DateCreated.DateTime + }; + Notes.Add(note); + } + } + } +} \ No newline at end of file diff --git a/winui-learning/winui-learning/Models/NoteModel.cs b/winui-learning/winui-learning/Models/NoteModel.cs index a62ec7a..830ea8a 100644 --- a/winui-learning/winui-learning/Models/NoteModel.cs +++ b/winui-learning/winui-learning/Models/NoteModel.cs @@ -1,12 +1,41 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; +using Windows.Storage; namespace winui_learning.Models { - internal class NoteModel + public class NoteModel { + private StorageFolder storageFolder = ApplicationData.Current.LocalFolder; + public string Filename { get; set; } = string.Empty; + public string Text { get; set; } = string.Empty; + public DateTime Date { get; set; } = DateTime.Now; + + public NoteModel() + { + Filename = "notes" + DateTime.Now.ToBinary().ToString() + ".txt"; + } + + public async Task SaveAsync() + { + // Save the note to a file. + StorageFile noteFile = (StorageFile)await storageFolder.TryGetItemAsync(Filename); + if (noteFile is null) + { + noteFile = await storageFolder.CreateFileAsync(Filename, CreationCollisionOption.ReplaceExisting); + } + await FileIO.WriteTextAsync(noteFile, Text); + } + + public async Task DeleteAsync() + { + // Delete the note from the file system. + StorageFile noteFile = (StorageFile)await storageFolder.TryGetItemAsync(Filename); + if (noteFile is not null) + { + await noteFile.DeleteAsync(); + } + } } } + diff --git a/winui-learning/winui-learning/Views/AllNotesPage.xaml b/winui-learning/winui-learning/Views/AllNotesPage.xaml new file mode 100644 index 0000000..55a27f0 --- /dev/null +++ b/winui-learning/winui-learning/Views/AllNotesPage.xaml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/winui-learning/winui-learning/Views/AllNotesPage.xaml.cs b/winui-learning/winui-learning/Views/AllNotesPage.xaml.cs new file mode 100644 index 0000000..1fc321d --- /dev/null +++ b/winui-learning/winui-learning/Views/AllNotesPage.xaml.cs @@ -0,0 +1,18 @@ +using Microsoft.UI.Xaml.Controls; +using winui_learning.Models; + +// To learn more about WinUI, the WinUI project structure, +// and more about our project templates, see: http://aka.ms/winui-project-info. + +namespace winui_learning.Views +{ + public sealed partial class AllNotesPage : Page + { + public AllNotesModel allNotesModel = new AllNotesModel(); + + public AllNotesPage() + { + InitializeComponent(); + } + } +} diff --git a/winui-learning/winui-learning/Views/NotePage.xaml b/winui-learning/winui-learning/Views/NotePage.xaml index 93fc499..c3c7a19 100644 --- a/winui-learning/winui-learning/Views/NotePage.xaml +++ b/winui-learning/winui-learning/Views/NotePage.xaml @@ -19,21 +19,23 @@ - + -