main
syneffort 1 year ago
parent 9039c49ad3
commit 7ef194a4cb
  1. 6
      MyFirstMauiApp/NotesNet8/AppShell.xaml
  2. 6
      MyFirstMauiApp/NotesNet8/AppShell.xaml.cs
  3. 16
      MyFirstMauiApp/NotesNet8/Models/About.cs
  4. 39
      MyFirstMauiApp/NotesNet8/Models/AllNotes.cs
  5. 15
      MyFirstMauiApp/NotesNet8/Models/Note.cs
  6. 7
      MyFirstMauiApp/NotesNet8/NotesNet8.csproj
  7. 27
      MyFirstMauiApp/NotesNet8/Pages/NotePage.xaml.cs
  8. 17
      MyFirstMauiApp/NotesNet8/Views/AboutPage.xaml
  9. 7
      MyFirstMauiApp/NotesNet8/Views/AboutPage.xaml.cs
  10. 37
      MyFirstMauiApp/NotesNet8/Views/AllNotesPage.xaml
  11. 34
      MyFirstMauiApp/NotesNet8/Views/AllNotesPage.xaml.cs
  12. 10
      MyFirstMauiApp/NotesNet8/Views/NotePage.xaml
  13. 50
      MyFirstMauiApp/NotesNet8/Views/NotePage.xaml.cs

@ -3,17 +3,17 @@
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:NotesNet8"
xmlns:page="clr-namespace:NotesNet8.Pages"
xmlns:views="clr-namespace:NotesNet8.Views"
Title="NotesNet8"
Shell.FlyoutBehavior="Disabled">
<TabBar>
<ShellContent Title="Notes"
ContentTemplate="{DataTemplate page:NotePage}"
ContentTemplate="{DataTemplate views:AllNotesPage}"
Icon="{OnPlatform 'icon_notes.png'}" />
<ShellContent Title="About"
ContentTemplate="{DataTemplate page:AboutPage}"
ContentTemplate="{DataTemplate views:AboutPage}"
Icon="{OnPlatform 'icon_about.png'}" />
</TabBar>

@ -5,6 +5,12 @@
public AppShell()
{
InitializeComponent();
RegisterRoutes();
}
private void RegisterRoutes()
{
Routing.RegisterRoute(nameof(Views.NotePage), typeof(Views.NotePage));
}
}
}

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NotesNet8.Models
{
internal class About
{
public string Title => AppInfo.Name;
public string Version => AppInfo.VersionString;
public string MoreInfoUrl => "https://aka.ms/maui";
public string Message => "This app is written in XAML and C# with .NET MAUI";
}
}

@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NotesNet8.Models
{
internal class AllNotes
{
public ObservableCollection<Note> Notes { get; set; } = new ObservableCollection<Note>();
public AllNotes()
{
LoadNotes();
}
public void LoadNotes()
{
Notes.Clear();
string appDataPath = FileSystem.AppDataDirectory;
IEnumerable<Note> notes = Directory
.EnumerateFiles(appDataPath, "*.notes.txt")
.Select(filename => new Note()
{
Filename = filename,
Text = File.ReadAllText(filename),
Date = File.GetCreationTime(filename)
})
.OrderBy(note => note.Date);
foreach (Note note in notes)
{
Notes.Add(note);
}
}
}
}

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NotesNet8.Models
{
internal class Note
{
public string Filename { get; set; }
public string Text { get; set; }
public DateTime Date { get; set; }
}
}

@ -68,10 +68,13 @@
</ItemGroup>
<ItemGroup>
<MauiXaml Update="Pages\AboutPage.xaml">
<MauiXaml Update="Views\AboutPage.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
<MauiXaml Update="Pages\NotePage.xaml">
<MauiXaml Update="Views\AllNotesPage.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
<MauiXaml Update="Views\NotePage.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
</ItemGroup>

@ -1,27 +0,0 @@
namespace NotesNet8.Pages;
public partial class NotePage : ContentPage
{
string _fileName = Path.Combine(FileSystem.AppDataDirectory, "notes.txt");
public NotePage()
{
InitializeComponent();
if (File.Exists(_fileName))
TextEditor.Text = File.ReadAllText(_fileName);
}
private void SaveButton_Clicked(object sender, EventArgs e)
{
File.WriteAllText(_fileName, TextEditor.Text);
}
private void DeleteButton_Clicked(object sender, EventArgs e)
{
if (File.Exists(_fileName))
File.Delete(_fileName);
TextEditor.Text = string.Empty;
}
}

@ -1,16 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="NotesNet8.Pages.AboutPage"
<ContentPage x:Class="NotesNet8.Views.AboutPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:models="clr-namespace:NotesNet8.Models"
Title="AboutPage">
<ContentPage.BindingContext>
<models:About />
</ContentPage.BindingContext>
<VerticalStackLayout Margin="10" Spacing="10">
<HorizontalStackLayout Spacing="10">
<Image HeightRequest="64" SemanticProperties.Description="The dot net bot waving hello!" Source="dotnet_bot.png" />
<Label FontAttributes="Bold" FontSize="22" Text="Notes" VerticalOptions="End" />
<Label FontSize="22" Text="v1.0" VerticalOptions="End" />
<Label FontAttributes="Bold" FontSize="22"
Text="{Binding Title}"
VerticalOptions="End" />
<Label FontSize="22"
Text="{Binding Version}"
VerticalOptions="End" />
</HorizontalStackLayout>
<Label Text="This app is written in XAML and C# with .NET MAUI" />
<Label Text="{Binding Message}" />
<Button Clicked="LearnMore_Clicked" Text="Learn more..." />
</VerticalStackLayout>
</ContentPage>

@ -1,4 +1,4 @@
namespace NotesNet8.Pages;
namespace NotesNet8.Views;
public partial class AboutPage : ContentPage
{
@ -9,6 +9,9 @@ public partial class AboutPage : ContentPage
private async void LearnMore_Clicked(object sender, EventArgs e)
{
await Launcher.Default.OpenAsync("https://aka.ms/maui");
if (BindingContext is Models.About about)
{
await Launcher.Default.OpenAsync(about.MoreInfoUrl);
}
}
}

@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="NotesNet8.Views.AllNotesPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:models="clr-namespace:NotesNet8.Models"
Title="AllNotesPage">
<ContentPage.ToolbarItems>
<ToolbarItem Clicked="Add_Clicked"
IconImageSource="{FontImage Glyph='+',
Color=Black,
Size=22}"
Text="Add" />
</ContentPage.ToolbarItems>
<ContentPage.BindingContext>
<models:AllNotes />
</ContentPage.BindingContext>
<CollectionView x:Name="notesCollection"
Margin="20"
ItemsSource="{Binding Notes}"
SelectionChanged="notesCollection_SelectionChanged" SelectionMode="Single">
<CollectionView.ItemsLayout>
<LinearItemsLayout ItemSpacing="10" Orientation="Vertical" />
</CollectionView.ItemsLayout>
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label FontSize="22" Text="{Binding Text}" />
<Label FontSize="14"
Text="{Binding Date}"
TextColor="Silver" />
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ContentPage>

@ -0,0 +1,34 @@
namespace NotesNet8.Views;
public partial class AllNotesPage : ContentPage
{
public AllNotesPage()
{
InitializeComponent();
BindingContext = new Models.AllNotes();
}
protected override void OnAppearing()
{
base.OnAppearing();
((Models.AllNotes)BindingContext).LoadNotes();
}
private async void Add_Clicked(object sender, EventArgs e)
{
await Shell.Current.GoToAsync(nameof(NotePage));
}
private async void notesCollection_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.CurrentSelection.Count == 0)
return;
var note = (Models.Note)e.CurrentSelection[0];
await Shell.Current.GoToAsync($"{nameof(NotePage)}?{nameof(NotePage.ItemId)}={note.Filename}");
notesCollection.SelectedItem = null;
}
}

@ -1,11 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="NotesNet8.Pages.NotePage"
<ContentPage x:Class="NotesNet8.Views.NotePage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:models="clr-namespace:NotesNet8.Models"
Title="NotePage">
<ContentPage.BindingContext>
<models:Note />
</ContentPage.BindingContext>
<VerticalStackLayout Margin="5" Spacing="10">
<Editor x:Name="TextEditor"
HeightRequest="100" Placeholder="Enter your note" />
HeightRequest="100" Placeholder="Enter your note"
Text="{Binding Text}"/>
<Grid ColumnDefinitions="*,*" ColumnSpacing="4">
<Button Grid.Column="0"
Clicked="SaveButton_Clicked" Text="Save" />

@ -0,0 +1,50 @@
namespace NotesNet8.Views;
[QueryProperty(nameof(ItemId), nameof(ItemId))]
public partial class NotePage : ContentPage
{
public string ItemId { set { LoadNote(value); } }
public NotePage()
{
InitializeComponent();
string appDataPath = FileSystem.AppDataDirectory;
string randomFileName = $"{Path.GetRandomFileName()}.notes.txt";
LoadNote(Path.Combine(appDataPath, randomFileName));
}
private void LoadNote(string fileName)
{
Models.Note noteModel = new Models.Note();
noteModel.Filename = fileName;
if (File.Exists(fileName))
{
noteModel.Date = File.GetCreationTime(fileName);
noteModel.Text = File.ReadAllText(fileName);
}
BindingContext = noteModel;
}
private async void SaveButton_Clicked(object sender, EventArgs e)
{
if (BindingContext is Models.Note note)
File.WriteAllText(note.Filename, TextEditor.Text);
await Shell.Current.GoToAsync("..");
}
private async void DeleteButton_Clicked(object sender, EventArgs e)
{
if (BindingContext is Models.Note note)
{
if (File.Exists(note.Filename))
File.Delete(note.Filename);
}
await Shell.Current.GoToAsync("..");
}
}
Loading…
Cancel
Save