Files
Aaru/Aaru.Gui/ViewLocator.cs

66 lines
2.2 KiB
C#
Raw Normal View History

2020-04-17 21:45:50 +01:00
// /***************************************************************************
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : ViewLocator.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : GUI.
//
// --[ Description ] ----------------------------------------------------------
//
// View locator.
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General public License for more details.
//
// You should have received a copy of the GNU General public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2024-12-19 10:45:18 +00:00
// Copyright © 2011-2025 Natalia Portillo
2020-04-17 21:45:50 +01:00
// ****************************************************************************/
2020-04-09 02:26:04 +01:00
using System;
2023-10-05 01:05:23 +01:00
using System.Diagnostics.CodeAnalysis;
2020-04-09 02:26:04 +01:00
using Aaru.Gui.ViewModels;
using Avalonia.Controls;
2023-09-25 22:58:48 +01:00
using Avalonia.Markup.Xaml.Templates;
2020-07-22 13:20:25 +01:00
using JetBrains.Annotations;
2020-04-09 02:26:04 +01:00
namespace Aaru.Gui;
2023-10-05 01:05:23 +01:00
[SuppressMessage("ReSharper", "UnusedMember.Global")]
[SuppressMessage("ReSharper", "UnusedMember.Global")]
2023-09-25 22:58:48 +01:00
public sealed class ViewLocator : DataTemplate
2022-03-06 13:29:38 +00:00
{
public bool SupportsRecycling => false;
2020-04-17 21:45:50 +01:00
2022-03-06 13:29:38 +00:00
[CanBeNull]
2023-10-05 01:05:23 +01:00
public Control Build([JetBrains.Annotations.NotNull] object data)
2022-03-06 13:29:38 +00:00
{
string name = data.GetType().FullName?.Replace("ViewModel", "View");
2020-04-17 21:45:50 +01:00
2024-05-01 04:05:22 +01:00
if(name is null) return null;
2020-04-09 02:26:04 +01:00
2022-03-06 13:29:38 +00:00
var type = Type.GetType(name);
2020-04-17 21:45:50 +01:00
2024-05-01 04:05:22 +01:00
if(type != null) return (Control)Activator.CreateInstance(type);
2020-04-09 02:26:04 +01:00
2022-03-06 13:29:38 +00:00
return new TextBlock
{
Text = "Not Found: " + name
};
2020-04-09 02:26:04 +01:00
}
2022-03-06 13:29:38 +00:00
public bool Match(object data) => data is ViewModelBase;
2020-04-09 02:26:04 +01:00
}