dotnet new --install [путь до скачанного шаблона]
dotnet new avalonia.mvvm -o Notebook
<Window xmlns="https://github.com/avaloniaui" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vm="clr-namespace:Notebook.ViewModels;assembly=Notebook" Icon="resm:Notebook.Assets.avalonia-logo.ico" Title="Notebook"> <!--Биндим горячие клавиши--> <Window.KeyBindings> <KeyBinding Gesture="Ctrl+O" Command="{Binding Open}" /> <KeyBinding Gesture="Ctrl+S" Command="{Binding Save}" /> </Window.KeyBindings> <Design.DataContext> <vm:MainWindowViewModel /> </Design.DataContext> <!--Стандартная разметочка гридом--> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <!--Менюшечка--> <Menu Grid.Row="0" Grid.Column="0"> <MenuItem Header="File"> <MenuItem Header="Open" Command="{Binding Open}" /> <MenuItem Header="Save As" Command="{Binding Save}" /> </MenuItem> </Menu> <!--Основное текстовое поле--> <TextBox Grid.Row="1" Grid.Column="0" Text="{Binding Data}" AcceptsReturn="True" /> </Grid> </Window>
private string _data; public string Data { get => _data; set => this.RaiseAndSetIfChanged(ref _data, value); }
public async Task Open() { var dialog = new OpenFileDialog(); string[] result = null; dialog.Filters.Add(new FileDialogFilter() {Name = "Text", Extensions = {"txt"}}); result = await dialog.ShowAsync(); if (result != null) { Data = File.ReadAllText(result.First()); } }
public async Task Save() { var dialog = new SaveFileDialog(); dialog.Filters.Add(new FileDialogFilter() {Name = "Text", Extensions = {"txt"}}); var result = await dialog.ShowAsync(new MainWindow()); if (result != null) { File.WriteAllText(result, Data); } }
Source: https://habr.com/ru/post/438920/