You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.0 KiB
37 lines
1.0 KiB
using System; |
|
using Avalonia.Controls; |
|
using Avalonia.Controls.Templates; |
|
using StabilityMatrix.Avalonia.ViewModels; |
|
using StabilityMatrix.Core.Attributes; |
|
|
|
namespace StabilityMatrix.Avalonia; |
|
|
|
public class ViewLocator : IDataTemplate |
|
{ |
|
public Control Build(object? data) |
|
{ |
|
if (data is null) throw new ArgumentNullException(nameof(data)); |
|
|
|
var name = data.GetType().FullName!; |
|
var type = Type.GetType(name); |
|
var attr = (ViewAttribute) Attribute.GetCustomAttribute(type, typeof(ViewAttribute)); |
|
|
|
if (attr is null) |
|
return new TextBlock |
|
{Text = "Not Found: " + name + ". Did you forget to add the [View] attribute?"}; |
|
|
|
// Get from DI |
|
var view = App.Services.GetService(attr.GetViewType()); |
|
if (view is not null) |
|
{ |
|
return (Control) view; |
|
} |
|
|
|
return new TextBlock {Text = "Not Found: " + name}; |
|
} |
|
|
|
public bool Match(object? data) |
|
{ |
|
return data is ViewModelBase; |
|
} |
|
}
|
|
|