Files
GSS2Rework/GSS2.Test/DependencyInjectionViewLocator.cs
T
amkovkov 5a728dbdd9 refactor: move ui core
move ui core classes into separate project
refactor CameraView
2026-02-04 15:10:48 +03:00

33 lines
994 B
C#

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;
}