问题
I've been able to set focus on my view's Close button using the following code in the MainPage.xaml.cs OnNavigatedTo() event handler.
this.close.Focus(FocusState.Programmatic);`
// or
Windows.UI.Xaml.Input.FocusManager.TryFocusAsync(this.Close, FocusState.Programmatic);`
I'd prefer a way to declaratively set this in the xaml markup. Is there a way to do that?
I've tried following samples provided in SO post hits on this subject showing approaches llike the following using FocussedElement, which appears to have been deprecated, and am having no luck.
<Page . . . xmlns:xi="using:Windows.UI.Xaml.Input">
<StackPanel . . . FocusManager.FocusedElement="{Binding ElementName=Close}">
<!-- or -->
<Button Name="Close" . . . xi:FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}">Close</Button>
来源:https://stackoverflow.com/questions/61349354/uwp-xaml-declaratively-defining-control-that-has-focus-by-default