ViewLocator.cs 735 B

123456789101112131415161718192021222324252627282930
  1. using Avalonia.Controls;
  2. using Avalonia.Controls.Templates;
  3. using System;
  4. using VeloeMinecraftLauncher.ViewModels;
  5. namespace VeloeMinecraftLauncher
  6. {
  7. public class ViewLocator : IDataTemplate
  8. {
  9. public IControl 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. else
  18. {
  19. return new TextBlock { Text = "Not Found: " + name };
  20. }
  21. }
  22. public bool Match(object data)
  23. {
  24. return data is ViewModelBase;
  25. }
  26. }
  27. }