Параметры компонента следует объявлять как автосвойства, что означает
что они не должны содержать пользовательскую логику в методах доступа get или set
. Например, следующее свойство StartData является
автосвойством:
Код: Выделить всё
[Parameter] public DateTime StartData { get; set; }
Ну, это догма, потому что я не вижу оправдания.
Я согласен, что не должно быть сложного логика, но есть важный случай, когда простая логика должно быть - это проверка.
Пример
Значение параметра theme компонента AdmonitionBlock должно быть либо элемент перечисления StandardThemes или элемент перечисления CustomThemes, который должен быть определен с помощью метода defineThemes перед использованием компонента AdmonitionBlock:
Код: Выделить всё
public partial class AdmonitionBlock : Microsoft.AspNetCore.Components.ComponentBase
{
public static string CSS_NAMESPACE = "AdmonitionBlock";
public enum StandardThemes { regular }
protected internal static Type? CustomThemes;
public static void defineThemes(Type CustomThemes)
{
YDF_ComponentsHelper.ValidateCustomTheme(CustomThemes);
AdmonitionBlock.CustomThemes = CustomThemes;
}
protected string _theme = AdmonitionBlock.StandardThemes.regular.ToString();
[Microsoft.AspNetCore.Components.Parameter]
public object theme
{
get => this._theme;
set => YDF_ComponentsHelper.
AssignThemeIfItIsValid(value, AdmonitionBlock.CustomThemes, ref this._theme);
}
protected internal static bool mustConsiderThemesCSS_ClassesAsCommon = YDF_ComponentsHelper.areThemesCSS_ClassesCommon;
public static void considerThemesAsCommon()
{
AdmonitionBlock.mustConsiderThemesCSS_ClassesAsCommon = true;
}
[Microsoft.AspNetCore.Components.Parameter]
public bool areThemesCSS_ClassesCommon { get; set; } =
YDF_ComponentsHelper.areThemesCSS_ClassesCommon || AdmonitionBlock.mustConsiderThemesCSS_ClassesAsCommon;
}
Код: Выделить всё
public abstract class ComponentsHelper
{
public static bool areThemesCSS_ClassesCommon = false;
public static void ValidateCustomTheme(Type CustomThemes)
{
if (!CustomThemes.IsEnum)
{
throw new CustomYDF_ThemeIsNotEnumerationException();
}
}
public static void AssignThemeIfItIsValid(object value, Type? customThemes, ref string _theme)
{
if (value is TStandardThemes standardTheme)
{
_theme = $"{ standardTheme }";
return;
}
string stringifiedThemeValue = value.ToString() ?? "";
if (customThemes is not null)
{
Type customThemesType = customThemes;
if (customThemesType.IsEnum && Enum.GetNames(customThemesType).Contains(stringifiedThemeValue))
{
_theme = stringifiedThemeValue;
return;
}
}
throw new InvalidThemeParameterForYDF_ComponentException();
}
}
Следуя советам, тему переключили на автоматическое свойство и добавили метод SetParametersAsync:
Код: Выделить всё
public partial class AdmonitionBlock : Microsoft.AspNetCore.Components.ComponentBase
{
public enum StandardThemes { regular }
protected internal static Type? CustomThemes;
public static void defineThemes(Type CustomThemes)
{
ComponentsHelper.ValidateCustomTheme(CustomThemes);
AdmonitionBlock.CustomThemes = CustomThemes;
}
[Microsoft.AspNetCore.Components.Parameter]
public object theme { get; set; } = AdmonitionBlock.StandardThemes.regular;
public string themeName => this.theme.ToString() ?? "";
public override Task SetParametersAsync(Microsoft.AspNetCore.Components.ParameterView parameters)
{
parameters.SetParameterProperties(this);
if (parameters.TryGetValue(nameof(this.theme), out object? value))
{
ComponentsHelper.ValidateTheme(value, AdmonitionBlock.CustomThemes);
}
return base.SetParametersAsync(ParameterView.Empty);
}
}
Код: Выделить всё
public abstract class ComponentsHelper
{
public static void ValidateTheme(object? value, Type? customThemes)
{
if (value is TStandardThemes standardTheme)
{
return;
}
string stringifiedThemeValue = value?.ToString() ?? "";
if (customThemes is not null)
{
Type customThemesType = customThemes;
if (customThemesType.IsEnum && Enum.GetNames(customThemesType).Contains(stringifiedThemeValue))
{
return;
}
}
throw new InvalidThemeParameterForYDF_ComponentException();
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... properties
Мобильная версия