Přidání stránky přehledu všech poznámek, přepsání na MVVM.
This commit is contained in:
@@ -1,20 +1,4 @@
|
|||||||
using Microsoft.UI.Xaml;
|
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,
|
// 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.
|
||||||
|
|||||||
@@ -31,7 +31,7 @@
|
|||||||
</TitleBar.IconSource>
|
</TitleBar.IconSource>
|
||||||
</TitleBar>
|
</TitleBar>
|
||||||
|
|
||||||
<Frame x:Name="rootFrame" Grid.Row="1"
|
<Frame x:Name="rootFrame" Grid.Row="1"
|
||||||
SourcePageType="views:NotePage"/>
|
SourcePageType="views:AllNotesPage"/>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Window>
|
</Window>
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ public sealed partial class MainWindow : Window
|
|||||||
public MainWindow()
|
public MainWindow()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
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;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Windows.Storage;
|
||||||
|
|
||||||
namespace winui_learning.Models
|
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="*"/>
|
<ColumnDefinition Width="*"/>
|
||||||
</Grid.ColumnDefinitions>
|
</Grid.ColumnDefinitions>
|
||||||
|
|
||||||
<TextBox x:Name="NoteEditor"
|
<TextBox
|
||||||
AcceptsReturn="True"
|
x:Name="NoteEditor"
|
||||||
TextWrapping="Wrap"
|
Header="{x:Bind note.Date.ToString()}"
|
||||||
PlaceholderText="Enter your note"
|
Text="{x:Bind note.Text, Mode=TwoWay}"
|
||||||
Header="New note"
|
AcceptsReturn="True"
|
||||||
ScrollViewer.VerticalScrollBarVisibility="Auto"
|
TextWrapping="Wrap"
|
||||||
Width="400"
|
PlaceholderText="Enter your note"
|
||||||
Grid.Column="1"/>
|
ScrollViewer.VerticalScrollBarVisibility="Auto"
|
||||||
|
Width="400"
|
||||||
|
Grid.Column="1"/>
|
||||||
|
|
||||||
<StackPanel Orientation="Horizontal"
|
<StackPanel Orientation="Horizontal"
|
||||||
HorizontalAlignment="Right"
|
HorizontalAlignment="Right"
|
||||||
Spacing="4"
|
Spacing="4"
|
||||||
Grid.Row="1" Grid.Column="1">
|
Grid.Row="1" Grid.Column="1">
|
||||||
<Button Content="Save" Style="{StaticResource AccentButtonStyle}" Click="Save"/>
|
<Button Content="Save" Style="{StaticResource AccentButtonStyle}" Click="ButtonClick_Save"/>
|
||||||
<Button Content="Delete" Click="Delete"/>
|
<Button Content="Delete" Click="ButtonClick_Delete"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Page>
|
</Page>
|
||||||
|
|||||||
@@ -1,19 +1,6 @@
|
|||||||
using Microsoft.UI.Xaml;
|
using Microsoft.UI.Xaml;
|
||||||
using Microsoft.UI.Xaml.Controls;
|
using Microsoft.UI.Xaml.Controls;
|
||||||
using Microsoft.UI.Xaml.Controls.Primitives;
|
using winui_learning.Models;
|
||||||
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;
|
|
||||||
|
|
||||||
// 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.
|
||||||
@@ -25,42 +12,26 @@ namespace winui_learning.Views
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed partial class NotePage : Page
|
public sealed partial class NotePage : Page
|
||||||
{
|
{
|
||||||
private StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
|
private NoteModel? note;
|
||||||
private StorageFile? noteFile = null;
|
|
||||||
private string fileName = "note.txt";
|
|
||||||
|
|
||||||
public NotePage()
|
public NotePage()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
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 (note is not null)
|
||||||
|
|
||||||
if (noteFile 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 note.DeleteAsync();
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>WinExe</OutputType>
|
<OutputType>WinExe</OutputType>
|
||||||
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
|
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
|
||||||
@@ -50,7 +50,9 @@
|
|||||||
</Page>
|
</Page>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Views\" />
|
<Page Update="Views\AllNotesPage.xaml">
|
||||||
|
<Generator>MSBuild:Compile</Generator>
|
||||||
|
</Page>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
|
|||||||
Reference in New Issue
Block a user