Files
LogViewerControl/CSharp/Applications/AvaloniaSerilogDI/ViewLocator.cs
T

37 lines
1012 B
C#
Raw Normal View History

2026-04-04 13:30:13 +02:00
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using AvaloniaSerilogDI.ViewModels;
using System;
2026-04-04 15:36:30 +02:00
using System.Diagnostics.CodeAnalysis;
2026-04-04 13:30:13 +02:00
namespace AvaloniaSerilogDI;
2026-04-04 15:36:30 +02:00
// <summary>
/// Given a view model, returns the corresponding view if possible.
/// </summary>
[RequiresUnreferencedCode(
"Default implementation of ViewLocator involves reflection which may be trimmed away.",
Url = "https://docs.avaloniaui.net/docs/concepts/view-locator")]
2026-04-04 13:30:13 +02:00
public class ViewLocator : IDataTemplate
{
2026-04-04 15:36:30 +02:00
public Control? Build(object? param)
2026-04-04 13:30:13 +02:00
{
2026-04-04 15:36:30 +02:00
if (param is null)
return null;
var name = param.GetType().FullName!.Replace("ViewModel", "View", StringComparison.Ordinal);
var type = Type.GetType(name);
2026-04-04 13:30:13 +02:00
if (type != null)
{
return (Control)Activator.CreateInstance(type)!;
}
return new TextBlock { Text = "Not Found: " + name };
}
2026-04-04 15:36:30 +02:00
public bool Match(object? data)
2026-04-04 13:30:13 +02:00
{
return data is ViewModelBase;
}
2026-04-04 15:36:30 +02:00
}