何时有限的属性获得输入?
目前,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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于
{x:bind}
具有onetige
的默认模式,因此您应将绑定的模式
设置为twoway
让您的源属性按预期设置:Since
{x:Bind}
has a default mode ofOneTime
, you should set theMode
of the binding toTwoWay
for your source property to get set as expected: