PlatformInformationViewModel.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Runtime.InteropServices;
  2. using Avalonia;
  3. using Avalonia.Platform;
  4. using MiniMvvm;
  5. namespace ControlCatalog.ViewModels;
  6. public class PlatformInformationViewModel : ViewModelBase
  7. {
  8. public PlatformInformationViewModel()
  9. {
  10. /* NOTE:
  11. * ------------
  12. * The below API is not meant to be used in production Apps.
  13. * If you need to consume this info, please use:
  14. * - OperatingSystem ( https://learn.microsoft.com/en-us/dotnet/api/system.operatingsystem | if .NET 5 or greater)
  15. * - or RuntimeInformation ( https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.runtimeinformation )
  16. */
  17. var runtimeInfo = AvaloniaLocator.Current.GetService<IRuntimePlatform>()?.GetRuntimeInfo();
  18. if (runtimeInfo is { } info)
  19. {
  20. if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("BROWSER")))
  21. {
  22. if (info.IsDesktop)
  23. {
  24. PlatformInfo = "Platform: Desktop (browser)";
  25. }
  26. else if (info.IsMobile)
  27. {
  28. PlatformInfo = "Platform: Mobile (browser)";
  29. }
  30. else
  31. {
  32. PlatformInfo = "Platform: Unknown (browser) - please report";
  33. }
  34. }
  35. else
  36. {
  37. if (info.IsDesktop)
  38. {
  39. PlatformInfo = "Platform: Desktop (native)";
  40. }
  41. else if (info.IsMobile)
  42. {
  43. PlatformInfo = "Platform: Mobile (native)";
  44. }
  45. else
  46. {
  47. PlatformInfo = "Platform: Unknown (native) - please report";
  48. }
  49. }
  50. }
  51. }
  52. public string? PlatformInfo { get; }
  53. }