ViewLocator.cs 610 B

123456789101112131415161718192021
  1. using System;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.Templates;
  4. using SlamCross.ViewModels;
  5. namespace SlamCross;
  6. public class ViewLocator : IDataTemplate
  7. {
  8. public Control? Build(object? param)
  9. {
  10. if (param is null) return null;
  11. var name = param.GetType().FullName!.Replace("ViewModel", "View", StringComparison.Ordinal);
  12. var type = Type.GetType(name);
  13. return type != null
  14. ? (Control)Activator.CreateInstance(type)!
  15. : new TextBlock { Text = "Not Found: " + name };
  16. }
  17. public bool Match(object? data) => data is ViewModelBase;
  18. }