ViewLocator.cs 677 B

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