24 lines
629 B
C#
24 lines
629 B
C#
using System;
|
|
using Avalonia;
|
|
using Avalonia.Markup.Xaml;
|
|
|
|
namespace AutoCat.Utils;
|
|
|
|
public class ViewModelProvider: MarkupExtension
|
|
{
|
|
private readonly Type _viewModelType;
|
|
|
|
public ViewModelProvider(Type viewModelType)
|
|
{
|
|
_viewModelType = viewModelType;
|
|
}
|
|
|
|
public override object ProvideValue(IServiceProvider serviceProvider)
|
|
{
|
|
if (Application.Current is App app)
|
|
return app.AppHost.Services.GetService(_viewModelType)
|
|
?? throw new Exception("Error finding ViewModel");
|
|
else
|
|
throw new Exception("Error getting App instance");
|
|
}
|
|
} |