diff options
| author | Weicao-CatilGrass <1992414357@qq.com> | 2025-12-18 01:12:54 +0800 |
|---|---|---|
| committer | Weicao-CatilGrass <1992414357@qq.com> | 2025-12-18 01:12:54 +0800 |
| commit | 088a09bbfd9bf8ebcdf3dd193cef688f37efe3c5 (patch) | |
| tree | 2f3f4f46456ec3d54a936d028fac207a2d851778 /JVDesktop/ViewLocator.cs | |
| parent | 764c177237dfeebfd53895a68f42145f892e9eda (diff) | |
First commit
Diffstat (limited to 'JVDesktop/ViewLocator.cs')
| -rw-r--r-- | JVDesktop/ViewLocator.cs | 31 |
1 files changed, 31 insertions, 0 deletions
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; + } +} |
