带有 AppBarButton 的 TextBox 中的焦点错误 - UWP

Focus error in TextBox with AppBarButton - UWP

我正在使用此代码在 UWP 的 MVVM 项目中 XAML 创建一个按钮。

为此使用此代码:

<CommandBar Background="Yellow" >
    <AppBarButton Label="Pruebas" Command="{Binding cmd_Process}" >
          <AppBarButton.Icon>
               <SymbolIcon Symbol="Setting"/>
          </AppBarButton.Icon>
     </AppBarButton>
</CommandBar>

在 "clicking" 'Command' 的那一刻,我需要的方法在 ViewModel 中执行,但我必须发送 TextBlock 的内容才能在 ViewModel 中使用它。

很好,但是在发送的时候它给我发送null,所以我不发送null我必须将Focus的配置更改为TextBlock,但我必须先做,然后执行命令。

如果我使用普通按钮 link 命令可以完美运行,但不能与 AppBarButton 一起使用。

如何让 AppBarButton 在执行命令之前改变 TextBlock 的焦点?或者我如何从 ViewModel 控制 fucus,知道我必须从 ViewModel 调用页面 class?

您可以在执行命令之前或在命令函数内通过 ViewModel 甚至从您的页面 class 设置焦点。使用 Control.Focus() 方法。以下链接中的更多信息: https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.control.focus https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.input.focusmanager https://docs.microsoft.com/en-us/windows/communitytoolkit/developer-tools/focustracker

您可能会在 TextBox 中失去焦点 属性:

UpdateSourceTrigger=PropertyChanged

在文本框中使用...示例:

<TextBox x:Name="txt_Name" Header="Name:" Margin="5" Text="{Binding PropertyBindingToViewModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>