如何在 Blazor 组件中使用异步任务的字符串结果?
How to use a string result of an asynchronous task in a blazor component?
我在 mij Index.razor 文件中有这样的东西:
<div class="trials">
@DoSomething("First try")
@(async () => await DoSomething("Second try"))
@DoSomething("Third try").Result
</div>
<div class="purpose">
<h4>@DoSomething("Use the result like a title")</h4>
<textarea placeholder="@DoSomething("Use the result like a placeholder")" />
<button type="button" class="btn">@DoSomething("Show the result on a button")</button>
</div>
@code {
private async Task<string> DoSomething(string text)
{
return await client.DoSomething(text);
}
}
我想在 headers、按钮、占位符等上显示 DoSomething() 的字符串结果。但我无法让它工作。我尝试了不同的解决方案。
第一次尝试:
@DoSomething("First try")
Returns System.Threading.Tasks.Task`1[System.String] 而不是我期望的结果。
第二次尝试:
@(async () => await DoSomething("Second try"))
它说这是无效的,因为:无法将 lambda 表达式转换为类型 'object' 因为它不是委托类型
第三次尝试:
@DoSomething("Third try").Result
不回来,申请就会卡死。
可以将结果存储在变量或 属性 中,但这对我来说不是解决方案,因为我会在任何地方使用它,在按钮、占位符和其他东西上。
如何在 header/button/placeholder/etc 上显示 DoSomething() 的结果?
正确的做法如下
<div class="trials">
@Value1
@Value2
@Value3
</div>
@code
{
protected override async Task OnInitializedAsync()
{
Value1 = "1";
Value2 = await DoSomethingAsync().ConfigureAwait(false);
Value3 = "3";
}
}
我能想到的'cleanest'是作业的组件:
// AsyncHelper.razor
@result
@code {
[Parameter]
public Func<string, Task<string>> Formatter { get; set; }
[Parameter]
public string Text { get; set; }
private string result;
protected override async Task OnParametersSetAsync()
{
result = await Source(Text);
}
}
然后像
一样使用它
<h4><AsyncHelper Formatter="DoSomething" Text ="Use the result like a title" /></h4>
我在 mij Index.razor 文件中有这样的东西:
<div class="trials">
@DoSomething("First try")
@(async () => await DoSomething("Second try"))
@DoSomething("Third try").Result
</div>
<div class="purpose">
<h4>@DoSomething("Use the result like a title")</h4>
<textarea placeholder="@DoSomething("Use the result like a placeholder")" />
<button type="button" class="btn">@DoSomething("Show the result on a button")</button>
</div>
@code {
private async Task<string> DoSomething(string text)
{
return await client.DoSomething(text);
}
}
我想在 headers、按钮、占位符等上显示 DoSomething() 的字符串结果。但我无法让它工作。我尝试了不同的解决方案。
第一次尝试:
@DoSomething("First try")
Returns System.Threading.Tasks.Task`1[System.String] 而不是我期望的结果。
第二次尝试:
@(async () => await DoSomething("Second try"))
它说这是无效的,因为:无法将 lambda 表达式转换为类型 'object' 因为它不是委托类型
第三次尝试:
@DoSomething("Third try").Result
不回来,申请就会卡死。
可以将结果存储在变量或 属性 中,但这对我来说不是解决方案,因为我会在任何地方使用它,在按钮、占位符和其他东西上。
如何在 header/button/placeholder/etc 上显示 DoSomething() 的结果?
正确的做法如下
<div class="trials">
@Value1
@Value2
@Value3
</div>
@code
{
protected override async Task OnInitializedAsync()
{
Value1 = "1";
Value2 = await DoSomethingAsync().ConfigureAwait(false);
Value3 = "3";
}
}
我能想到的'cleanest'是作业的组件:
// AsyncHelper.razor
@result
@code {
[Parameter]
public Func<string, Task<string>> Formatter { get; set; }
[Parameter]
public string Text { get; set; }
private string result;
protected override async Task OnParametersSetAsync()
{
result = await Source(Text);
}
}
然后像
一样使用它<h4><AsyncHelper Formatter="DoSomething" Text ="Use the result like a title" /></h4>