42 lines
1.2 KiB
C#
42 lines
1.2 KiB
C#
using Avalonia.Markup.Xaml;
|
|
using Cowain.Base.Helpers;
|
|
using System.Collections.ObjectModel;
|
|
|
|
namespace Cowain.Base.Extensions;
|
|
|
|
public class MenuEnableExtension : MarkupExtension
|
|
{
|
|
private string _pageName;
|
|
private string _menuKey;
|
|
public MenuEnableExtension(string pageName, string menuKey)
|
|
{
|
|
_pageName = pageName;
|
|
_menuKey = menuKey;
|
|
}
|
|
public override object ProvideValue(IServiceProvider serviceProvider)
|
|
{
|
|
var _executes = GetMenuActions(_pageName);
|
|
if (_executes == null)
|
|
{
|
|
return false;
|
|
}
|
|
return _executes.Any(action => action.Equals(_menuKey, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
public ObservableCollection<string>? GetMenuActions(string? pageName)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(pageName) || !pageName.EndsWith("View"))
|
|
{
|
|
return null;
|
|
}
|
|
var menuKey = pageName.Substring(0, pageName.Length - "View".Length);
|
|
var menu = GlobalData.Instance.CurrentUser?.Menus?.FirstOrDefault(menu => menu.MenuKey == menuKey);
|
|
if (menu == null)
|
|
{
|
|
return null;
|
|
}
|
|
return menu.MenuActions;
|
|
}
|
|
|
|
}
|