当用户单击命令栏上的应用栏按钮时,最后一个文本框中具有焦点的数据不会更新 (UWP)



>我有一个简单的数据页面,用户在其中填写 2 个字段(名字和姓氏(,然后单击CommandBar上的保存AppBarButton

这两个字段都使用x:BindMode=TwoWay来加载/更新值。

如果用户在文本框中填写名字,

然后在文本框中填写姓氏并点击命令栏上的"保存 AppBar按钮",而焦点仍位于"姓氏"文本框上,则仅更新名字。

但是,我还在页面上创建了一个常规Button,如果用户执行相同的操作并改用此按钮,则两个字段都会按预期更新。

如何让命令栏上的 AppBarButton 以与常规按钮相同的方式工作并更新所有数据,而无需在后面添加大量代码?

这是我拥有的简单类定义:

public class UserData
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

下面是 XAML 页面:

<Page
    x:Class="mySampleApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    x:Name="MyPage"
    mc:Ignorable="d">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition  Height="Auto"/>
            <RowDefinition  Height="Auto"/>
            <RowDefinition  Height="Auto"/>
            <RowDefinition  Height="Auto"/>
        </Grid.RowDefinitions>
        <CommandBar Grid.Row="0"  DefaultLabelPosition="Right" >
            <!-- Save button on a command bar-->
            <AppBarButton Icon="Save" Label="Save" LabelPosition="Default" Click="saveStuff_Click" />
        </CommandBar>
        <!-- Sample Data to save-->
        <TextBox x:Name="txtFirstName" Grid.Row="1" Header="Enter Your First Name" Text="{x:Bind currentUserData.FirstName, Mode=TwoWay}" PlaceholderText="First Name"/>
        <TextBox x:Name="txtLastName" Grid.Row="2" Header="Enter Your Last Name" Text="{x:Bind currentUserData.LastName, Mode=TwoWay}" PlaceholderText="Last Name"/>
        <!-- Save button using a regular button that calls the same function-->
        <Button x:Name="saveStuff" Grid.Row="3" Content="Save" Click="saveStuff_Click" />
    </Grid>
</Page>

下面是背后的代码:

using Newtonsoft.Json;
using System;
using Windows.Storage;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace mySampleApp
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        //variables to use in XAML x:Bind
        private UserData currentUserData = new UserData();
        public MainPage()
        {
            this.InitializeComponent();
        }
        private async void saveStuff_Click(object sender, RoutedEventArgs e)
        {
            var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("sampledata.json", CreationCollisionOption.ReplaceExisting);
            await FileIO.WriteTextAsync(file, JsonConvert.SerializeObject(currentUserData, Formatting.Indented));
            var dialog = new MessageDialog("Data saved to: " + file.Path.ToString() + Environment.NewLine + "Data Saved: FirstName: " + currentUserData.FirstName + "; LastName: " + currentUserData.LastName);
            await dialog.ShowAsync();
        }
    }
}

我能够通过保持所有文本框绑定不变(使用 x:Bind (并且仅使用 AllowFocusOnInteraction="True" 更新CommandBar上的AppBarButton来解决此问题。

现在它看起来像这样,一切都按预期工作:

<AppBarButton AllowFocusOnInteraction="True" Icon="Save" Label="Save" LabelPosition="Default" Click="saveStuff_Click" />

正如官方文档中所解释的(向下滚动到 UpdateSourceTrigger)x:Bind TextBox.Text等待文本框失去焦点以将其内容转发到 Viewodel。与从文本框中窃取上下文的普通Button相比,使用 AppBarButton 时,焦点似乎停留在文本框上(如你自己提到的(,因此绑定属性永远不会更新。

将绑定的UpdateSourceTrigger设置为 PropertyChanged 可能会解决问题,因为它会在每次按键时强制刷新 bindig,但不幸的是,x:Bind 不支持这一点。所以我想你最好的选择是使用传统的Binding

Text="{Binding currentUserData.LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

最新更新