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 @@