using Avalonia.Controls; using Avalonia.Controls.Templates; using GSS2.UI.Core.ViewModels; namespace GSS2.Test; public class DependencyInjectionViewLocator : IDataTemplate { public Control? Build(object? data) { if (data is null) return null; var name = data.GetType().AssemblyQualifiedName? .Replace("ViewModels", "Views") .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; }