forked from amkovkov/GranuSightSoftware2
29 lines
892 B
C#
29 lines
892 B
C#
using Avalonia.Controls;
|
|
using Avalonia.Controls.Templates;
|
|
|
|
namespace GSS2.Test;
|
|
|
|
public class DependencyInjectionViewLocator : IDataTemplate
|
|
{
|
|
public Control? Build(object? data)
|
|
{
|
|
if (data is null)
|
|
return null;
|
|
|
|
var name = data.GetType().FullName?.Replace("ViewModel", "View");
|
|
if (name is null)
|
|
return new TextBlock { Text = $"Not found: {data}" };
|
|
var type = Type.GetType(name);
|
|
if (type is null)
|
|
return new TextBlock { Text = $"Not found type: {name} for {data}" };
|
|
|
|
var control = Program.ApplicationHost.Services.GetRequiredService(type) as Control;
|
|
if (control is null)
|
|
return new TextBlock { Text = $"Not found in DI: {type} for {data}" };
|
|
|
|
control.DataContext = data;
|
|
return control;
|
|
}
|
|
|
|
public bool Match(object? data) => data is ViewModelBase;
|
|
} |