-
I try to use .NET 5 FocusAsync() feature with TextEdit.
But got the following error on compilation:
Is there another way to set focus to TextEdit instances? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 5 replies
-
First you need to fix your code <TextEdit @ref="textEdit" />
@code {
TextEdit textEdit;
private async Task SetFocus()
{
await textEdit.FocusAsync();
}
} For above example and 0.9.2.x version you can use And for 0.9.3 you can use:
|
Beta Was this translation helpful? Give feedback.
-
From where do you call |
Beta Was this translation helpful? Give feedback.
-
Aaahhh, the infamous And that is exactly what is happening here. You're trying to do |
Beta Was this translation helpful? Give feedback.
-
PS. Button has a Example <Button Outline="false" Disabled="EditMode" Clicked="@(() => OnTextEdit(true))">@buttonText</Button> |
Beta Was this translation helpful? Give feedback.
-
Solution <Button Outline="false" Disabled="EditMode" Clicked="@(() => OnTextEdit(true))">@buttonText</Button>
@if ( EditMode )
{
<Row>
<Column ColumnSize="ColumnSize.IsFull" Margin="Margin.Is2.OnY">
<TextEdit @ref="textInput" @bind-Text="@currentEdit" />
<Button Color="Color.Primary" Clicked="@(() => OnTextEdit(false))" Margin="Margin.Is2">Store</Button>
</Column>
</Row>
}
@code {
[Parameter]
public string UserAnswer { get; set; } = "";
[Parameter]
public EventCallback<string> UserAnswerChanged { get; set; }
private bool EditMode = false;
TextEdit textInput;
string currentEdit = "";
string buttonText = "___";
bool needFocus;
protected override void OnInitialized()
{
if ( UserAnswer.Length == 0 )
buttonText = "____";
}
protected override Task OnAfterRenderAsync( bool firstRender )
{
if ( needFocus )
{
InvokeAsync( () => textInput.Focus() );
needFocus = false;
}
return base.OnAfterRenderAsync( firstRender );
}
private async Task OnUserAnswerChanged( string answer )
{
UserAnswer = answer;
await UserAnswerChanged.InvokeAsync( answer );
}
private async Task OnTextEdit( bool doEdit )
{
if ( EditMode != doEdit )
{
EditMode = doEdit;
if ( EditMode )
{
currentEdit = UserAnswer;
}
else
{
// store anwser
UserAnswer = currentEdit.Trim();
if ( UserAnswer.Length == 0 )
buttonText = "____";
else
buttonText = UserAnswer;
}
if ( EditMode )
needFocus = true;
await InvokeAsync( () => StateHasChanged() );
}
}
} |
Beta Was this translation helpful? Give feedback.
Solution