xaml.cs

using System.Collections.ObjectModel;


namespace MauiNotatnikApp

{

    public partial class MainPage : ContentPage

    {

        public ObservableCollection<Note> Notes { get; } = new();

        private Note? CurrentNote;


        public MainPage()

        {

            InitializeComponent();

            BindingContext = this;

        }


        private void OnAddButtonClicked(object sender, EventArgs e)

        {

            Editor.Text = ""; // Czyścimy edytor

            Editor.IsEnabled = true; // Aktywujemy pole edycji

            SaveButton.IsEnabled = true; // Aktywujemy przycisk Save

            CurrentNote = null; // Tworzymy nową notatkę

        }


        private async void OnSaveButtonClicked(object sender, EventArgs e)

        {

            if (string.IsNullOrWhiteSpace(Editor.Text))

            {

                await DisplayAlert("Błąd", "Treść notatki nie może być pusta!", "OK");

                return;

            }


            string? noteTitle = await DisplayPromptAsync("Zapisz notatkę", "Podaj nazwę notatki:");

            if (string.IsNullOrWhiteSpace(noteTitle))

            {

                await DisplayAlert("Błąd", "Nazwa notatki nie może być pusta!", "OK");

                return;

            }


            Notes.Add(new Note { Title = noteTitle, Content = Editor.Text });


            Editor.Text = ""; // Czyścimy pole edytora

            Editor.IsEnabled = false; // Dezaktywujemy pole edycji

            SaveButton.IsEnabled = false; // Dezaktywujemy przycisk Save

        }


        private void OnNoteSelected(object sender, SelectionChangedEventArgs e)

        {

            if (e.CurrentSelection.Count > 0)

            {

                CurrentNote = (Note)e.CurrentSelection[0];

                Editor.Text = CurrentNote.Content;

                Editor.IsEnabled = true;

                SaveButton.IsEnabled = true;

            }

        }

    }


    public class Note

    {

        public string Title { get; set; } = string.Empty;

        public string Content { get; set; } = string.Empty;

    }

}



xaml


<?xml version="1.0" encoding="utf-8" ?>

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"

             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"

             x:Class="MauiNotatnikApp.MainPage"

             Title="Maui Notatnik App">


    <Grid RowDefinitions="Auto, 1*,3*, Auto" Padding="10">

        <HorizontalStackLayout Grid.Row="0" Spacing="10">

            <Button Text="+" WidthRequest="40" HeightRequest="40" Clicked="OnAddButtonClicked" />

            <Button Text="Save" Clicked="OnSaveButtonClicked" IsEnabled="False" x:Name="SaveButton" />

        </HorizontalStackLayout>


        <Frame Grid.Row="1" BorderColor="Gray" CornerRadius="5" Padding="10" Margin="0,10,0,10">

            <CollectionView ItemsSource="{Binding Notes}" SelectionMode="Single" SelectionChanged="OnNoteSelected">

                <CollectionView.ItemTemplate>

                    <DataTemplate>

                        <StackLayout>

                            <Label Text="{Binding Title}" FontSize="16" Padding="5,0" />

                        </StackLayout>

                    </DataTemplate>

                </CollectionView.ItemTemplate>

            </CollectionView>

        </Frame>


        <Editor x:Name="Editor" Grid.Row="2" Placeholder="Pole notatki" Margin="0,10,0,10" FontSize="16" IsEnabled="False" />

    </Grid>

</ContentPage>