ViewLocator.cs 696 B

12345678910111213141516171819202122232425262728293031
  1. using System;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.Templates;
  4. using MiniMvvm;
  5. namespace SafeAreaDemo
  6. {
  7. public class ViewLocator : IDataTemplate
  8. {
  9. public Control Build(object data)
  10. {
  11. if (data is null)
  12. return null;
  13. var name = data.GetType().FullName!.Replace("ViewModel", "View");
  14. var type = Type.GetType(name);
  15. if (type != null)
  16. {
  17. return (Control)Activator.CreateInstance(type)!;
  18. }
  19. return new TextBlock { Text = name };
  20. }
  21. public bool Match(object? data)
  22. {
  23. return data is ViewModelBase;
  24. }
  25. }
  26. }