ViewLocator.cs 618 B

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