diff options
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; + } +} |
