[英]When a bound property gets a input?
Currently im learning to build windows apps with mvvm with the Template Studio.目前我正在学习使用 Template Studio 使用 mvvm 构建 Windows 应用程序。 I added a SettingsPage what contains some elements like:
我添加了一个 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>
My SettingsPage.xaml.cs:我的 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.在调试时,我用我的名字填充该框,并为 SetSettings 任务设置一个制动点。 After clicking on the "Save" Button i can see, that Firstname is null.
单击“保存”按钮后,我可以看到,Firstname 为空。 Do i have to add some more code for gettings the information from the box?
我是否必须添加更多代码才能从框中获取信息?
由于{x:Bind}
具有OneTime
的默认模式,因此您应该将绑定的Mode
设置为TwoWay
,以便您的源属性按预期设置:
Text="{x:Bind ViewModel.Firstname, Mode=TwoWay}"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.