何时有限的属性获得输入?

发布于 2025-02-12 14:00:29 字数 1885 浏览 0 评论 0原文

目前,IM学习使用模板工作室使用MVVM构建Windows应用程序。我添加了一个settingspage,其中包含某些元素,例如:

<StackPanel x:Name="ContentArea">
        <CommandBar Background="Transparent" OverflowButtonVisibility="Collapsed">
            <AppBarButton Click="BtnSave_Click" Icon="Save" />          
        </CommandBar>
        <Pivot>
            <PivotItem x:Uid="Settings_Personal">
                <StackPanel Margin="{StaticResource XSmallTopMargin}">
                    <TextBlock x:Uid="Settings_Personal_Firstname" />
                    <TextBox x:Uid="Settings_Personal_FirstnameBox" Text="{x:Bind ViewModel.Firstname, Mode=TwoWay}" />                        
                </StackPanel>
            </PivotItem>
        <Pivot>
</StackPanel>

我的settingspage.xaml.cs:

public sealed partial class SettingsPage : Page
{
      public SettingsViewModel ViewModel { get; }

      public SettingsPage()
      {
         ViewModel = App.GetService<SettingsViewModel>();
         InitializeComponent();
      }

      public void BtnSave_Click(object sender, RoutedEventArgs routedEventArgs)
      {
         ViewModel.SetSetting();
      }
}

和我的viewModel:

public class SettingsViewModel : ObservableRecipient
{
private string _firstname;

public string Firstname
{
    get => _firstname;
    set => SetProperty(ref _firstname, value);
}

public SettingsViewModel(ILocalSettingsService localSettingsService)
{
    _localSettingsService = localSettingsService;
}

public async Task SetSetting()
{
    string test = Firstname;  // < My Breakpoint
    await _localSettingsService.SaveSettingAsync("Firstname", Firstname);
}

}

在调试时间,我正在用我的firstName填充盒子,并为设置设置任务设置一个brakpoint。单击我可以看到的“保存”按钮后,第一个名称为null。 我是否必须添加更多代码来获取框中的信息?

Currently im learning to build windows apps with mvvm with the Template Studio. I added a SettingsPage what contains some elements like:

<StackPanel x:Name="ContentArea">
        <CommandBar Background="Transparent" OverflowButtonVisibility="Collapsed">
            <AppBarButton Click="BtnSave_Click" Icon="Save" />          
        </CommandBar>
        <Pivot>
            <PivotItem x:Uid="Settings_Personal">
                <StackPanel Margin="{StaticResource XSmallTopMargin}">
                    <TextBlock x:Uid="Settings_Personal_Firstname" />
                    <TextBox x:Uid="Settings_Personal_FirstnameBox" Text="{x:Bind ViewModel.Firstname, Mode=TwoWay}" />                        
                </StackPanel>
            </PivotItem>
        <Pivot>
</StackPanel>

My SettingsPage.xaml.cs:

public sealed partial class SettingsPage : Page
{
      public SettingsViewModel ViewModel { get; }

      public SettingsPage()
      {
         ViewModel = App.GetService<SettingsViewModel>();
         InitializeComponent();
      }

      public void BtnSave_Click(object sender, RoutedEventArgs routedEventArgs)
      {
         ViewModel.SetSetting();
      }
}

And my Viewmodel:

public class SettingsViewModel : ObservableRecipient
{
private string _firstname;

public string Firstname
{
    get => _firstname;
    set => SetProperty(ref _firstname, value);
}

public SettingsViewModel(ILocalSettingsService localSettingsService)
{
    _localSettingsService = localSettingsService;
}

public async Task SetSetting()
{
    string test = Firstname;  // < My Breakpoint
    await _localSettingsService.SaveSettingAsync("Firstname", Firstname);
}

}

At debug time, i'm filling the box with my firstname and set a brakpoint to the SetSettings Task. After clicking on the "Save" Button i can see, that Firstname is null.
Do i have to add some more code for gettings the information from the box?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想念有你 2025-02-19 14:00:30

由于{x:bind}具有onetige的默认模式,因此您应将绑定的模式设置为twoway让您的源属性按预期设置:

Text="{x:Bind ViewModel.Firstname, Mode=TwoWay}"

Since {x:Bind} has a default mode of OneTime, you should set the Mode of the binding to TwoWay for your source property to get set as expected:

Text="{x:Bind ViewModel.Firstname, Mode=TwoWay}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文