Přidání stránky přehledu všech poznámek, přepsání na MVVM.

This commit is contained in:
Matěj Kubíček
2026-06-07 21:57:16 +02:00
parent a4d7985f48
commit f3c786bf57
10 changed files with 200 additions and 71 deletions
-16
View File
@@ -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.
@@ -31,7 +31,7 @@
</TitleBar.IconSource>
</TitleBar>
<Frame x:Name="rootFrame" Grid.Row="1"
SourcePageType="views:NotePage"/>
<Frame x:Name="rootFrame" Grid.Row="1"
SourcePageType="views:AllNotesPage"/>
</Grid>
</Window>
@@ -13,6 +13,8 @@ public sealed partial class MainWindow : Window
public MainWindow()
{
InitializeComponent();
ExtendsContentIntoTitleBar = true;
SetTitleBar(AppTitleBar);
}
}
@@ -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<NoteModel> Notes { get; set; } = new ObservableCollection<NoteModel>();
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<IStorageItem> 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);
}
}
}
}
@@ -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();
}
}
}
}
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="utf-8"?>
<Page
x:Class="winui_learning.Views.AllNotesPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:models="using:winui_learning.Models"
mc:Ignorable="d">
<Page.Resources>
<DataTemplate x:Key="NoteItemTemplate"
x:DataType="models:NoteModel">
<ItemContainer CornerRadius="{StaticResource OverlayCornerRadius}">
<Grid Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
BorderThickness="1"
BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
CornerRadius="{StaticResource OverlayCornerRadius}">
<Grid.RowDefinitions>
<RowDefinition Height="120"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="{x:Bind Text}" Margin="12,8"
TextWrapping="Wrap"
TextTrimming="WordEllipsis"
/>
<Border Grid.Row="1" Padding="8,6,0,6"
Background="{ThemeResource SubtleFillColorSecondaryBrush}">
<TextBlock Text="{x:Bind Date}"
Style="{StaticResource CaptionTextBlockStyle}"
Foreground="{ThemeResource TextFillColorSecondaryBrush}"/>
</Border>
</Grid>
</ItemContainer>
</DataTemplate>
</Page.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<CommandBar DefaultLabelPosition="Right">
<AppBarButton Icon="Add" Label="New note"/>
<CommandBar.Content>
<TextBlock Text="Quick notes" Margin="16,8"
Style="{ThemeResource SubtitleTextBlockStyle}"/>
</CommandBar.Content>
</CommandBar>
<ItemsView
ItemsSource="{x:Bind allNotesModel.Notes}"
Grid.Row="1" Padding="16"
ItemTemplate="{StaticResource NoteItemTemplate}">
<ItemsView.Layout>
<UniformGridLayout MinItemWidth="200"
MinColumnSpacing="12"
MinRowSpacing="12"
ItemsJustification="Start"/>
</ItemsView.Layout>
</ItemsView>
</Grid>
</Page>
@@ -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();
}
}
}
@@ -19,21 +19,23 @@
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="NoteEditor"
AcceptsReturn="True"
TextWrapping="Wrap"
PlaceholderText="Enter your note"
Header="New note"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Width="400"
Grid.Column="1"/>
<TextBox
x:Name="NoteEditor"
Header="{x:Bind note.Date.ToString()}"
Text="{x:Bind note.Text, Mode=TwoWay}"
AcceptsReturn="True"
TextWrapping="Wrap"
PlaceholderText="Enter your note"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Width="400"
Grid.Column="1"/>
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Right"
Spacing="4"
Grid.Row="1" Grid.Column="1">
<Button Content="Save" Style="{StaticResource AccentButtonStyle}" Click="Save"/>
<Button Content="Delete" Click="Delete"/>
<Button Content="Save" Style="{StaticResource AccentButtonStyle}" Click="ButtonClick_Save"/>
<Button Content="Delete" Click="ButtonClick_Delete"/>
</StackPanel>
</Grid>
</Page>
@@ -1,19 +1,6 @@
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 System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
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.
@@ -25,42 +12,26 @@ namespace winui_learning.Views
/// </summary>
public sealed partial class NotePage : Page
{
private StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
private StorageFile? noteFile = null;
private string fileName = "note.txt";
private NoteModel? note;
public NotePage()
{
InitializeComponent();
Loaded += NotePage_Loaded;
}
private async void NotePage_Loaded(object sender, RoutedEventArgs e)
private async void ButtonClick_Save(object sender, RoutedEventArgs e)
{
noteFile = (StorageFile)await storageFolder.TryGetItemAsync(fileName);
if (noteFile is not null)
if (note is not null)
{
NoteEditor.Text = await FileIO.ReadTextAsync(noteFile);
await note.SaveAsync();
}
}
private async void Save(object sender, RoutedEventArgs e)
private async void ButtonClick_Delete(object sender, RoutedEventArgs e)
{
if (noteFile is null)
if (note is not null)
{
noteFile = await storageFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
}
await FileIO.WriteTextAsync(noteFile, NoteEditor.Text);
}
private async void Delete(object sender, RoutedEventArgs e)
{
if (noteFile is not null)
{
await noteFile.DeleteAsync();
noteFile = null;
NoteEditor.Text = string.Empty;
await note.DeleteAsync();
}
}
}
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
@@ -50,7 +50,9 @@
</Page>
</ItemGroup>
<ItemGroup>
<Folder Include="Views\" />
<Page Update="Views\AllNotesPage.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<!--