Dodělání aplikace podle tutoriálu. Nyní lze uložit, smazat a otevírát různé poznámky.

This commit is contained in:
Matěj Kubíček
2026-06-07 22:07:13 +02:00
parent f3c786bf57
commit b0b0b3ee34
6 changed files with 43 additions and 6 deletions
@@ -3,7 +3,6 @@
x:Class="winui_learning.MainWindow" x:Class="winui_learning.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:winui_learning"
xmlns:views="using:winui_learning.Views" xmlns:views="using:winui_learning.Views"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
@@ -25,7 +24,10 @@
</Grid.RowDefinitions> </Grid.RowDefinitions>
<TitleBar x:Name="AppTitleBar" <TitleBar x:Name="AppTitleBar"
Title="WinUI Notes"> Title="WinUI Notes"
IsBackButtonVisible="True"
IsBackButtonEnabled="{x:Bind rootFrame.CanGoBack, Mode=OneWay}"
BackRequested="AppTitleBar_BackRequested">
<TitleBar.IconSource> <TitleBar.IconSource>
<FontIconSource Glyph="&#xF4AA;"/> <FontIconSource Glyph="&#xF4AA;"/>
</TitleBar.IconSource> </TitleBar.IconSource>
@@ -1,4 +1,6 @@
using ABI.Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml;
using TitleBar = Microsoft.UI.Xaml.Controls.TitleBar;
// To learn more about WinUI, the WinUI project structure, // To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info. // and more about our project templates, see: http://aka.ms/winui-project-info.
@@ -16,5 +18,13 @@ public sealed partial class MainWindow : Window
ExtendsContentIntoTitleBar = true; ExtendsContentIntoTitleBar = true;
SetTitleBar(AppTitleBar); SetTitleBar(AppTitleBar);
} }
private void AppTitleBar_BackRequested(TitleBar titleBar, object args)
{
if (rootFrame.CanGoBack)
{
rootFrame.GoBack();
}
}
} }
@@ -46,7 +46,7 @@
</Grid.RowDefinitions> </Grid.RowDefinitions>
<CommandBar DefaultLabelPosition="Right"> <CommandBar DefaultLabelPosition="Right">
<AppBarButton Icon="Add" Label="New note"/> <AppBarButton Icon="Add" Label="New note" Click="NewNoteButton_Click"/>
<CommandBar.Content> <CommandBar.Content>
<TextBlock Text="Quick notes" Margin="16,8" <TextBlock Text="Quick notes" Margin="16,8"
Style="{ThemeResource SubtitleTextBlockStyle}"/> Style="{ThemeResource SubtitleTextBlockStyle}"/>
@@ -56,7 +56,10 @@
<ItemsView <ItemsView
ItemsSource="{x:Bind allNotesModel.Notes}" ItemsSource="{x:Bind allNotesModel.Notes}"
Grid.Row="1" Padding="16" Grid.Row="1" Padding="16"
ItemTemplate="{StaticResource NoteItemTemplate}"> ItemTemplate="{StaticResource NoteItemTemplate}"
SelectionMode="None"
IsItemInvokedEnabled="True"
ItemInvoked="ItemsView_ItemInvoked">
<ItemsView.Layout> <ItemsView.Layout>
<UniformGridLayout MinItemWidth="200" <UniformGridLayout MinItemWidth="200"
@@ -1,3 +1,4 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls;
using winui_learning.Models; using winui_learning.Models;
@@ -14,5 +15,15 @@ namespace winui_learning.Views
{ {
InitializeComponent(); InitializeComponent();
} }
private void NewNoteButton_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(NotePage));
}
private void ItemsView_ItemInvoked(ItemsView sender, ItemsViewItemInvokedEventArgs args)
{
Frame.Navigate(typeof(NotePage), args.InvokedItem);
}
} }
} }
@@ -3,7 +3,6 @@
x:Class="winui_learning.Views.NotePage" x:Class="winui_learning.Views.NotePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:winui_learning"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> mc:Ignorable="d">
@@ -1,5 +1,6 @@
using Microsoft.UI.Xaml; using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Navigation;
using winui_learning.Models; using winui_learning.Models;
// To learn more about WinUI, the WinUI project structure, // To learn more about WinUI, the WinUI project structure,
@@ -12,13 +13,20 @@ namespace winui_learning.Views
/// </summary> /// </summary>
public sealed partial class NotePage : Page public sealed partial class NotePage : Page
{ {
private NoteModel? note; private NoteModel note = new();
public NotePage() public NotePage()
{ {
InitializeComponent(); InitializeComponent();
} }
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
note = e.Parameter as NoteModel ?? new NoteModel();
Bindings.Update();
}
private async void ButtonClick_Save(object sender, RoutedEventArgs e) private async void ButtonClick_Save(object sender, RoutedEventArgs e)
{ {
if (note is not null) if (note is not null)
@@ -33,6 +41,10 @@ namespace winui_learning.Views
{ {
await note.DeleteAsync(); await note.DeleteAsync();
} }
if (Frame.CanGoBack)
{
Frame.GoBack();
}
} }
} }
} }