From 046411460b711e269602edeabb69e05a1aa1de10 Mon Sep 17 00:00:00 2001 From: countincognito Date: Sun, 19 Jan 2025 23:50:55 +0000 Subject: [PATCH] Add ability to embed view models (with corresponding view) into MsBoxStandardView. --- Exmaple2.0/App.axaml | 7 ++ Exmaple2.0/EmbeddedViewLocator.cs | 40 +++++++++++ Exmaple2.0/ViewModels/EmbeddedViewModel.cs | 6 ++ Exmaple2.0/Views/EmbeddedView.axaml | 23 +++++++ Exmaple2.0/Views/EmbeddedView.axaml.cs | 11 +++ Exmaple2.0/Views/MainWindow.axaml | 1 + Exmaple2.0/Views/MainWindow.axaml.cs | 19 +++++ .../Controls/MsBoxStandardView.axaml | 69 +++++++++++-------- .../Controls/MsBoxStandardView.axaml.cs | 11 +++ .../Dto/MessageBoxStandardParams.cs | 5 ++ MsBox.Avalonia/MessageBoxManager.cs | 4 +- .../ViewModels/MsBoxStandardViewModel.cs | 9 +++ 12 files changed, 174 insertions(+), 31 deletions(-) create mode 100644 Exmaple2.0/EmbeddedViewLocator.cs create mode 100644 Exmaple2.0/ViewModels/EmbeddedViewModel.cs create mode 100644 Exmaple2.0/Views/EmbeddedView.axaml create mode 100644 Exmaple2.0/Views/EmbeddedView.axaml.cs diff --git a/Exmaple2.0/App.axaml b/Exmaple2.0/App.axaml index b8dbed0..5b7f261 100644 --- a/Exmaple2.0/App.axaml +++ b/Exmaple2.0/App.axaml @@ -11,4 +11,11 @@ + + + + + + + \ No newline at end of file diff --git a/Exmaple2.0/EmbeddedViewLocator.cs b/Exmaple2.0/EmbeddedViewLocator.cs new file mode 100644 index 0000000..cb6e17a --- /dev/null +++ b/Exmaple2.0/EmbeddedViewLocator.cs @@ -0,0 +1,40 @@ +using Avalonia.Controls; +using Avalonia.Controls.Templates; + +namespace Exmaple2._0; + +public class EmbeddedViewLocator + : IDataTemplate +{ + public static Control BuildView(object? data) + { + if (data is not null) + { + var name = data.GetType().AssemblyQualifiedName!.Replace("ViewModel", "View"); + var type = Type.GetType(name); + + if (type != null) + { + return (Control)Activator.CreateInstance(type)!; + } + else + { + return new TextBlock { Text = "Not Found: " + name }; + } + } + else + { + return new TextBlock { Text = "Data object is null" }; + } + } + + public Control Build(object? data) + { + return BuildView(data); + } + + public bool Match(object? data) + { + return data is not null; + } +} diff --git a/Exmaple2.0/ViewModels/EmbeddedViewModel.cs b/Exmaple2.0/ViewModels/EmbeddedViewModel.cs new file mode 100644 index 0000000..909f575 --- /dev/null +++ b/Exmaple2.0/ViewModels/EmbeddedViewModel.cs @@ -0,0 +1,6 @@ +namespace Exmaple2._0.ViewModels; + +public class EmbeddedViewModel : ViewModelBase +{ + public string Message => "This is an embedded message!"; +} diff --git a/Exmaple2.0/Views/EmbeddedView.axaml b/Exmaple2.0/Views/EmbeddedView.axaml new file mode 100644 index 0000000..4d0e202 --- /dev/null +++ b/Exmaple2.0/Views/EmbeddedView.axaml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + diff --git a/Exmaple2.0/Views/EmbeddedView.axaml.cs b/Exmaple2.0/Views/EmbeddedView.axaml.cs new file mode 100644 index 0000000..5f80cb5 --- /dev/null +++ b/Exmaple2.0/Views/EmbeddedView.axaml.cs @@ -0,0 +1,11 @@ +using Avalonia.Controls; + +namespace Exmaple2._0.Views; + +public partial class EmbeddedView : UserControl +{ + public EmbeddedView() + { + InitializeComponent(); + } +} \ No newline at end of file diff --git a/Exmaple2.0/Views/MainWindow.axaml b/Exmaple2.0/Views/MainWindow.axaml index 2f1a228..737840b 100644 --- a/Exmaple2.0/Views/MainWindow.axaml +++ b/Exmaple2.0/Views/MainWindow.axaml @@ -26,6 +26,7 @@