From f0651027fb99161d570e713838012da2c8f5a8d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wies=C5=82aw=20=C5=A0olt=C3=A9s?= Date: Fri, 28 Jan 2022 13:42:03 +0100 Subject: [PATCH] Add more samples --- .../Samples/{BorderPage.txt => Border.txt} | 3 +- src/XamlToy/Samples/Button.txt | 43 ++++++++++++++ src/XamlToy/ViewModels/MainViewModel.cs | 58 ++++++++++++++++++- src/XamlToy/ViewModels/SampleViewModel.cs | 27 +++++++++ src/XamlToy/Views/MainView.axaml | 20 ++++++- 5 files changed, 146 insertions(+), 5 deletions(-) rename src/XamlToy/Samples/{BorderPage.txt => Border.txt} (91%) create mode 100644 src/XamlToy/Samples/Button.txt create mode 100644 src/XamlToy/ViewModels/SampleViewModel.cs diff --git a/src/XamlToy/Samples/BorderPage.txt b/src/XamlToy/Samples/Border.txt similarity index 91% rename from src/XamlToy/Samples/BorderPage.txt rename to src/XamlToy/Samples/Border.txt index c5f1a0f..10fe58b 100644 --- a/src/XamlToy/Samples/BorderPage.txt +++ b/src/XamlToy/Samples/Border.txt @@ -1,4 +1,5 @@ - + A control which decorates a child with a border and background + A button control + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/XamlToy/ViewModels/MainViewModel.cs b/src/XamlToy/ViewModels/MainViewModel.cs index dcf3c9b..a3c9ff0 100644 --- a/src/XamlToy/ViewModels/MainViewModel.cs +++ b/src/XamlToy/ViewModels/MainViewModel.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.ObjectModel; using System.IO; +using System.Linq; using System.Windows.Input; using Avalonia.Controls; using ReactiveUI; @@ -9,14 +11,55 @@ namespace XamlToy.ViewModels { public class MainViewModel : ViewModelBase { + private ObservableCollection _samples; + private SampleViewModel? _selectedSample; private string? _xaml; private IControl? _control; public MainViewModel() { - RunCommand = ReactiveCommand.Create(Run); + _samples = new ObservableCollection(); - _xaml = LoadResourceString("XamlToy.Samples.BorderPage.txt"); + var assembly = typeof(MainViewModel).Assembly; + var resourceNames = assembly.GetManifestResourceNames(); + + foreach (var resourceName in resourceNames) + { + var xaml = LoadResourceString(resourceName); + if (xaml is { } && resourceName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase)) + { + var name = GetName(resourceName); + if (name is { }) + { + _samples.Add(new SampleViewModel(name, xaml)); + } + } + } + + _selectedSample = _samples.FirstOrDefault(); + _xaml = _selectedSample?.Xaml; + + this.WhenAnyValue(x => x.SelectedSample) + .WhereNotNull() + .Subscribe(x => + { + Xaml = x.Xaml; + Control = null; + }); + + RunCommand = ReactiveCommand.Create(Run); + } + + public ObservableCollection Samples + { + get => _samples; + set => this.RaiseAndSetIfChanged(ref _samples, value); + } + + public SampleViewModel? SelectedSample + { + get => _selectedSample; + set => this.RaiseAndSetIfChanged(ref _selectedSample, value); } public string? Xaml @@ -33,6 +76,17 @@ namespace XamlToy.ViewModels public ICommand RunCommand { get; } + private string? GetName(string resourceName) + { + var parts = resourceName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries); + if (parts.Length >= 2) + { + return $"{parts[parts.Length - 2]}"; + } + + return null; + } + private string? LoadResourceString(string name) { var assembly = typeof(MainViewModel).Assembly; diff --git a/src/XamlToy/ViewModels/SampleViewModel.cs b/src/XamlToy/ViewModels/SampleViewModel.cs new file mode 100644 index 0000000..38070bc --- /dev/null +++ b/src/XamlToy/ViewModels/SampleViewModel.cs @@ -0,0 +1,27 @@ +using ReactiveUI; + +namespace XamlToy.ViewModels; + +public class SampleViewModel : ViewModelBase +{ + private string _name; + private string _xaml; + + public string Name + { + get => _name; + set => this.RaiseAndSetIfChanged(ref _name, value); + } + + public string Xaml + { + get => _xaml; + set => this.RaiseAndSetIfChanged(ref _xaml, value); + } + + public SampleViewModel(string name, string xaml) + { + _name = name; + _xaml = xaml; + } +} diff --git a/src/XamlToy/Views/MainView.axaml b/src/XamlToy/Views/MainView.axaml index a879623..b2f94ea 100644 --- a/src/XamlToy/Views/MainView.axaml +++ b/src/XamlToy/Views/MainView.axaml @@ -12,8 +12,21 @@ - - + + + + + + +