From 088a09bbfd9bf8ebcdf3dd193cef688f37efe3c5 Mon Sep 17 00:00:00 2001 From: Weicao-CatilGrass <1992414357@qq.com> Date: Thu, 18 Dec 2025 01:12:54 +0800 Subject: First commit --- JVDesktop/ViewLocator.cs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 JVDesktop/ViewLocator.cs (limited to 'JVDesktop/ViewLocator.cs') diff --git a/JVDesktop/ViewLocator.cs b/JVDesktop/ViewLocator.cs new file mode 100644 index 0000000..f289d15 --- /dev/null +++ b/JVDesktop/ViewLocator.cs @@ -0,0 +1,31 @@ +using System; +using Avalonia.Controls; +using Avalonia.Controls.Templates; +using JVDesktop.ViewModels; + +namespace JVDesktop; + +public class ViewLocator : IDataTemplate +{ + + public Control? Build(object? param) + { + if (param is null) + return null; + + var name = param.GetType().FullName!.Replace("ViewModel", "View", StringComparison.Ordinal); + var type = Type.GetType(name); + + if (type != null) + { + return (Control)Activator.CreateInstance(type)!; + } + + return new TextBlock { Text = "Not Found: " + name }; + } + + public bool Match(object? data) + { + return data is ViewModelBase; + } +} -- cgit