Seva0309/ViewLocator.cs
2024-12-22 21:37:21 +03:00

33 lines
751 B
C#

using Avalonia.Controls;
using Avalonia.Controls.Templates;
using Seva0309.ViewModels;
using System;
namespace Seva0309
{
public class ViewLocator : IDataTemplate
{
public Control? Build(object? param)
{
if (param is null)
return null;
var name = param.GetType().FullName!.Replace("ViewModel", "View", StringComparison.Ordinal);
var type = Type.GetType(name);
if (type != null)
{
return (Control)Activator.CreateInstance(type)!;
}
return new TextBlock { Text = "Not Found: " + name };
}
public bool Match(object? data)
{
return data is ViewModelBase;
}
}
}