通过MVVM中的绑定更新具有多个参数的表单
我使用 MVVM 在 Xamarin.Forms 应用程序中实现了一个具有四个输入元素的表单。这四个元素对应于模型类的四个属性。
数据存储在本地 SQLite 数据库中。我设法处理数据的创建、读取和删除,但我一直坚持更新它。我遇到的问题是我想执行一项命令来执行与多个参数绑定的更新。我尝试了几件事,但没有成功。所以我回到了最后的工作。
模型
public class LabWork
{
[PrimaryKey, AutoIncrement]
public int LabWorkId { get; set; }
public DateTime DateLabWork { get; set; }
public string TestName { get; set; }
public float TestValue { get; set; }
public string TestUnit { get; set; }
}
视图
<ContentPage.Resources>
<ResourceDictionary>
<vm:LabWorkDetailVM x:Key="vm"/>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<DatePicker x:Name="dateLabWorkPicked"
Format="dd/MM/yyyy"
FontSize="Large"
Margin="30,0,0,0"
/>
<Entry x:Name="testName"
Margin="25,0,0,0"
/>
<Entry x:Name="testValue"
Margin="25,0,0,0"
FontSize="Large"
Keyboard="Numeric"
/>
<Entry x:Name="testUnit"
Margin="25,0,0,0"
FontSize="Large"
Placeholder="Ex. mg/l, μg/l..."
/>
<Button Text="Sauvegarder modifications"
x:Name="updateLabWorkButton"
Command="{Binding Source={StaticResource vm}, Path=UpdateCommand}"
CommandParameter={?????}/>
<Button Text="Effacer"
x:Name="deleteLabWorkButton"
Command="{Binding Source={StaticResource vm}, Path=DeleteCommand}" />
</StackLayout>
</ContentPage.Content>
ViewModel
public class LabWorkDetailVM
{
public Command DeleteCommand { get; set; }
public Command UpdateCommand { get; set; }
public LabWork SelectedLabWork { get; set; }
public LabWorkDetailVM()
{
DeleteCommand = new Command(Delete);
UpdateCommand = new Command(Update);
}
private void Update(object obj)
{
throw new NotImplementedException();
}
private void Delete()
{
using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation))
{
conn.CreateTable<LabWork>();
int rows = conn.Delete(SelectedLabWork);
App.Current.MainPage.Navigation.PopAsync();
}
}
}
到目前为止,更新工作是通过下面的 View .xaml.cs 文件中的事件处理程序进行的,我想将其删除并替换为 Update在 ViewModel 中,
public partial class LabWorkDetailPage : ContentPage
{
public LabWork selectedLabWork;
public LabWorkDetailPage(LabWork selectedLabWork)
{
InitializeComponent();
(Resources["vm"] as LabWorkDetailVM).SelectedLabWork = selectedLabWork;
this.selectedLabWork = selectedLabWork;
dateLabWorkPicked.Date = selectedLabWork.DateLabWork;
testName.Text = selectedLabWork.TestName;
testValue.Text = selectedLabWork.TestValue.ToString();
testUnit.Text = selectedLabWork.TestUnit;
}
private void UpdateLabWorkButton_Clicked(object sender, EventArgs e)
{
selectedLabWork.DateLabWork = dateLabWorkPicked.Date;
selectedLabWork.TestName = testName.Text;
selectedLabWork.TestValue = float.Parse(testValue.Text, CultureInfo.InvariantCulture.NumberFormat);
selectedLabWork.TestUnit = testUnit.Text;
using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation))
{
conn.CreateTable<LabWork>();
int rows = conn.Update(selectedLabWork);
Navigation.PopAsync();
//if (rows > 0)
// DisplayAlert("Success", "Experience successfully updated", "OK");
//else
// DisplayAlert("Failure", "Failed to update experience", "OK");
}
}
}
我希望获得有关如何在 ViewModel 中编写 Update 方法以及在 View xaml 文件中编写相应实现的帮助。
I implemented a form with four input elements in a Xamarin.Forms app with MVVM. The four elements correspond to four properties of a model class.
Data is stored in a local SQLite database. I managed to deal with the creation, reading and deletion of data, but I am stuck with updating it. The problem I have is that I want to execute a command to perform the update with binding with more than one parameter. I tried several things, to no avail. So I came back to what worked last.
Model
public class LabWork
{
[PrimaryKey, AutoIncrement]
public int LabWorkId { get; set; }
public DateTime DateLabWork { get; set; }
public string TestName { get; set; }
public float TestValue { get; set; }
public string TestUnit { get; set; }
}
View
<ContentPage.Resources>
<ResourceDictionary>
<vm:LabWorkDetailVM x:Key="vm"/>
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
<DatePicker x:Name="dateLabWorkPicked"
Format="dd/MM/yyyy"
FontSize="Large"
Margin="30,0,0,0"
/>
<Entry x:Name="testName"
Margin="25,0,0,0"
/>
<Entry x:Name="testValue"
Margin="25,0,0,0"
FontSize="Large"
Keyboard="Numeric"
/>
<Entry x:Name="testUnit"
Margin="25,0,0,0"
FontSize="Large"
Placeholder="Ex. mg/l, μg/l..."
/>
<Button Text="Sauvegarder modifications"
x:Name="updateLabWorkButton"
Command="{Binding Source={StaticResource vm}, Path=UpdateCommand}"
CommandParameter={?????}/>
<Button Text="Effacer"
x:Name="deleteLabWorkButton"
Command="{Binding Source={StaticResource vm}, Path=DeleteCommand}" />
</StackLayout>
</ContentPage.Content>
ViewModel
public class LabWorkDetailVM
{
public Command DeleteCommand { get; set; }
public Command UpdateCommand { get; set; }
public LabWork SelectedLabWork { get; set; }
public LabWorkDetailVM()
{
DeleteCommand = new Command(Delete);
UpdateCommand = new Command(Update);
}
private void Update(object obj)
{
throw new NotImplementedException();
}
private void Delete()
{
using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation))
{
conn.CreateTable<LabWork>();
int rows = conn.Delete(SelectedLabWork);
App.Current.MainPage.Navigation.PopAsync();
}
}
}
So far updating works from an event handler in View .xaml.cs file below, which I want to remove and replace with Update in the ViewModel
public partial class LabWorkDetailPage : ContentPage
{
public LabWork selectedLabWork;
public LabWorkDetailPage(LabWork selectedLabWork)
{
InitializeComponent();
(Resources["vm"] as LabWorkDetailVM).SelectedLabWork = selectedLabWork;
this.selectedLabWork = selectedLabWork;
dateLabWorkPicked.Date = selectedLabWork.DateLabWork;
testName.Text = selectedLabWork.TestName;
testValue.Text = selectedLabWork.TestValue.ToString();
testUnit.Text = selectedLabWork.TestUnit;
}
private void UpdateLabWorkButton_Clicked(object sender, EventArgs e)
{
selectedLabWork.DateLabWork = dateLabWorkPicked.Date;
selectedLabWork.TestName = testName.Text;
selectedLabWork.TestValue = float.Parse(testValue.Text, CultureInfo.InvariantCulture.NumberFormat);
selectedLabWork.TestUnit = testUnit.Text;
using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation))
{
conn.CreateTable<LabWork>();
int rows = conn.Update(selectedLabWork);
Navigation.PopAsync();
//if (rows > 0)
// DisplayAlert("Success", "Experience successfully updated", "OK");
//else
// DisplayAlert("Failure", "Failed to update experience", "OK");
}
}
}
I'd appreciate some help on how to write the Update method in the ViewModel and corresponding implementation in the View xaml file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将按照您在评论中的要求实现绑定。
在虚拟机中:
i will implement binding as you asked in the comment.
in the VM :
如果我们正确使用MVVM,我们不需要每次都给entry和datePicker赋值,或者自己从每个控件中获取值。
根据您的代码,我创建了一个简单的演示来模拟您的需求。
可以参考如下代码:
MainPage.xaml.cs
MainPage.xaml
LabWorkDetailVM.cs
App.xaml.cs
这里我在
App.xaml.csLabWork
的对象> 作为传递给MainPage
的参数。If we use MVVM correctly, we don't need to assign values to entry and datePicker every time, or get the values from each control ourself.
Based on your code ,I created a simple demo to simulate your requirement.
You can refer to the following code:
MainPage.xaml.cs
MainPage.xaml
LabWorkDetailVM.cs
App.xaml.cs
Here I created a object of
LabWork
inApp.xaml.cs
as a parameter passed toMainPage
.