diff --git a/src/BootstrapBlazor.Shared/Pages/Samples/Displays.razor b/src/BootstrapBlazor.Shared/Pages/Samples/Displays.razor index d7f370786fa7806121150858b2bb4462b80b1d91..6b09464001954d26eb26bbb2d093fd6b270dc583 100644 --- a/src/BootstrapBlazor.Shared/Pages/Samples/Displays.razor +++ b/src/BootstrapBlazor.Shared/Pages/Samples/Displays.razor @@ -104,7 +104,7 @@
设置 Formatter
- +
diff --git a/src/BootstrapBlazor.Shared/Pages/Samples/Displays.razor.cs b/src/BootstrapBlazor.Shared/Pages/Samples/Displays.razor.cs index 08eb4f370d2597453b8bb7815ebb476d839abca6..05a8d592edba7d0c1d0b3eecba3ac1c8ace18987 100644 --- a/src/BootstrapBlazor.Shared/Pages/Samples/Displays.razor.cs +++ b/src/BootstrapBlazor.Shared/Pages/Samples/Displays.razor.cs @@ -11,6 +11,7 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; +using System.Threading.Tasks; namespace BootstrapBlazor.Shared.Pages { @@ -31,7 +32,11 @@ namespace BootstrapBlazor.Shared.Pages private byte[] ByteArray { get; set; } = new byte[] { 0x01, 0x12, 0x34, 0x56 }; - private static string ByteArrayFormatter(byte[] source) => Convert.ToBase64String(source); + private async Task ByteArrayFormatter(byte[] source) + { + await Task.Delay(10); + return Convert.ToBase64String(source); + } private static string DateTimeFormatter(DateTime source) => source.ToString("yyyy-MM-dd"); diff --git a/src/BootstrapBlazor/Components/Display/Display.razor.cs b/src/BootstrapBlazor/Components/Display/Display.razor.cs index b30e06b6fd609517f21178900151c800fea9a5a4..b3896a62f1637d91fcce41c1ce743df6b5445074 100644 --- a/src/BootstrapBlazor/Components/Display/Display.razor.cs +++ b/src/BootstrapBlazor/Components/Display/Display.razor.cs @@ -23,7 +23,13 @@ namespace BootstrapBlazor.Components protected string? CurrentTextAsString { get; set; } /// - /// 获得/设置 格式化字符串 + /// 获得/设置 异步格式化字符串 + /// + [Parameter] + public Func>? FormatterAsync { get; set; } + + /// + /// 获得/设置 同步格式化字符串 /// [Parameter] public Func? Formatter { get; set; } @@ -42,7 +48,7 @@ namespace BootstrapBlazor.Components { await base.OnParametersSetAsync(); - CurrentTextAsString = FormatValueAsString(Value); + CurrentTextAsString = await FormatTextAsString(Value); } /// @@ -50,11 +56,13 @@ namespace BootstrapBlazor.Components /// /// /// - protected override string? FormatValueAsString(TValue value) => Formatter != null - ? Formatter.Invoke(value) - : (!string.IsNullOrEmpty(FormatString) && value != null - ? Utility.Format((object)value, FormatString) - : FormatText(value)); + protected virtual async Task FormatTextAsString(TValue value) => FormatterAsync != null + ? await FormatterAsync(value) + : Formatter != null + ? Formatter(value) + : (!string.IsNullOrEmpty(FormatString) && value != null + ? Utility.Format((object)value, FormatString) + : FormatText(value)); private string? FormatText(TValue value) { @@ -70,7 +78,7 @@ namespace BootstrapBlazor.Components { ret = ConvertArrayToString(value); } - else if (value is not string && type.IsAssignableTo(typeof(IEnumerable))) + else if (type.IsGenericType && type.IsAssignableTo(typeof(IEnumerable))) { // 泛型集合 IEnumerable ret = ConvertEnumerableToString(value);