| | | 1 | | using Avalonia.Controls; |
| | | 2 | | using Avalonia.Controls.Templates; |
| | | 3 | | using LOCKnet.App.ViewModels; |
| | | 4 | | using System; |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | |
| | | 7 | | namespace LOCKnet.App; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Given a view model, returns the corresponding view if possible. |
| | | 11 | | /// </summary> |
| | | 12 | | [RequiresUnreferencedCode( |
| | | 13 | | "Default implementation of ViewLocator involves reflection which may be trimmed away.", |
| | | 14 | | Url = "https://docs.avaloniaui.net/docs/concepts/view-locator")] |
| | | 15 | | public class ViewLocator : IDataTemplate |
| | | 16 | | { |
| | | 17 | | public Control? Build(object? param) |
| | 3 | 18 | | { |
| | 3 | 19 | | if (param is null) |
| | 1 | 20 | | return null; |
| | | 21 | | |
| | 2 | 22 | | var name = param.GetType().FullName!.Replace("ViewModel", "View", StringComparison.Ordinal); |
| | 2 | 23 | | var type = Type.GetType(name); |
| | | 24 | | |
| | 2 | 25 | | if (type != null) |
| | 1 | 26 | | { |
| | 1 | 27 | | return (Control)Activator.CreateInstance(type)!; |
| | | 28 | | } |
| | | 29 | | |
| | 1 | 30 | | return new TextBlock { Text = "Not Found: " + name }; |
| | 3 | 31 | | } |
| | | 32 | | |
| | | 33 | | public bool Match(object? data) |
| | 2 | 34 | | { |
| | 2 | 35 | | return data is ViewModelBase; |
| | 2 | 36 | | } |
| | | 37 | | } |