我创建了一个页面,将值传递给允许用户更新数据的新页面 . 当用户选择要更新的记录时,编辑表单将打开,但数据不可见 . 如果更改了值并单击了编辑按钮,则会更新该值,但它永远不可见 . 如何显示要编辑的数据?
View Model
namespace QiApp.ViewModels
{
public class EditTodayCasesViewModel
{
private SxCaseDataService _sxCaseDataService = new SxCaseDataService();
public SxCase SelectedSxCase { get; set; }
public ICommand EditSxCaseCommand => new Command(async () =>
{
await _sxCaseDataService.PutSxCase(SelectedSxCase.SxCaseId, SelectedSxCase);
});
}
}
Edit Page xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewModels="clr-namespace:QiApp.ViewModels;assembly=QiApp.UWP"
x:Class="QiApp.Views.EditTodayCasePage">
<ContentPage.BindingContext>
<viewModels:EditTodayCasesViewModel/>
</ContentPage.BindingContext>
<StackLayout>
<Label Text="Surgery Case"/>
<Label Text="{Binding SelectedSxCase.SxCaseId}"/>
<Entry Text="{Binding SelectedSxCase.Record}"/>
<Switch IsToggled="{Binding SelectedSxCase.Complete}"/>
<Button Text="Edit Surgery Case"
Command="{Binding EditSxCaseCommand}"/>
</StackLayout>
</ContentPage>
Code behind
namespace QiApp.Views
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class EditTodayCasePage : ContentPage
{
public EditTodayCasePage(SxCase sxCase)
{
InitializeComponent();
var editTodayCasesViewModel = BindingContext as EditTodayCasesViewModel;
editTodayCasesViewModel.SelectedSxCase = sxCase;
}
}
}
1 回答
一切都没有问题,只是你的视图被绑定到一个视图模型,如果属性被更改,它将保持静默 . 当视图
SelectedSxCase
发生更改时,您的视图无法获取有关何时应更新自身以及UI的任何信息 .值得庆幸的是,通过简单地实现公共接口
INotifyPropertyChanged
并使用代码行扩展绑定属性来提升接口提供的事件,可以非常轻松地完成此操作 .基本上就是这样......
...但是有几种实现可以做得更优雅,如using the CallerMemberName甚至weaving the getter and setter automatically with Fody .