英文:
How to get value of DataModel field in code behind without using an Entry control on Xaml page in MAUI C#?
问题
I'm new to C# and MAUI - so thank you for your help!
What I have:
I've created a very simple 2 page application that works great:
- Page 1 uses a
CollectionView
to display a list of names defined byMyDataModel
(FirstName, LastName, ID). If the user selects an item in the list, then the program navigates to a detail page for that item. - Page 2 displays the FirstName and LastName of the selected item using
{Binding}
to fill<Entry />
controls, plus a Submit button to commit any changes to my database. I do not want to use an<Entry />
to display the ID field to the user.
What I need:
On my details page: How do I reference the value of MyDataModel's ID if I don't have an <Entry />
for it?
For example:
MyDataModel MyRecord = new MyDataModel();
MyRecord.FirstName = FirstNameEntry.Text; // Value from Xaml Entry
MyRecord.LastName = LastNameEntry.Text; // Value from Xaml Entry
MyRecord.ID = __________; // How do I get this value without an Xaml Entry?
More detailed code below.
Data Model
Simple data model with 3 fields per record
public class MyDataModel
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Collection View Page Code Behind
Collection View Selection Changed
private async void OnCollectionViewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.CurrentSelection.FirstOrDefault() != null)
{
MyDataModel MySelectedItem = e.CurrentSelection.FirstOrDefault() as MyDataModel;
await Navigation.PushAsync(new DetailPage
{
BindingContext = MySelectedItem
});
}
}
Detail Page Xaml
<Entry />
fields on Detail Page in Xaml
<Entry
x:Name="FirstNameEntry"
Text="{Binding FirstName}" />
<Entry
x:Name="LastNameEntry"
Text="{Binding LastName}" />
Detail Page Code Behind
How to access value of ID for this record without a Xaml <Entry />
for it?
public DetailPage()
{
InitializeComponent();
BindingContext = this;
}
private async void Submit_Button_Clicked(object sender, EventArgs e)
{
MyDataModel MyRecord = new MyDataModel();
MyRecord.FirstName = FirstNameEntry.Text;
MyRecord.LastName = LastNameEntry.Text;
MyRecord.ID = ___How Do I Get ID Without Using an XAML Entry?___; // int.Parse(IdEntry.Text);
await SaveRecord(MyRecord);
}
Thank you
英文:
I'm new to C# and MAUI - so thank you for your help!
What I have:
I've created a very simple 2 page application that works great:
- Page 1 uses a
CollectionView
to display a list of names defined byMyDataModel
(FirstName, LastName, ID). If the user selects an item in the list, then the program navigates to a detail page for that item. - Page 2 displays the FirstName and LastName of the selected item using
{Binding}
to fill<Entry />
controls, plus a Submit button to commit any changes to my database. I do not want to use an<Entry />
to display the ID field to the user.
What I need:
On my details page: How do I reference the value of MyDataModel's ID if I don't have an <Entry />
for it?
For example:
MyDataModel MyRecord = new MyDataModel();
MyRecord.FirstName = FirstNameEntry.Text; // Value from Xaml Entry
MyRecord.LastName = LastNameEntry.Text; // Value from Xaml Entry
MyRecord.ID = __________; // How do I get this value without an Xaml Entry?
More detailed code below.
Data Model
Simple data model with 3 fields per record
public class MyDataModel
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
Collection View Page Code Behind
Collection View Selection Changed
private async void OnCollectionViewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.CurrentSelection.FirstOrDefault() != null)
{
MyDataModel MySelectedItem = e.CurrentSelection.FirstOrDefault() as MyDataModel;
await Navigation.PushAsync(new DetailPage
{
BindingContext = MySelectedItem
});
}
}
Detail Page Xaml
<Entry />
fields on Detail Page in Xaml
<Entry
x:Name="FirstNameEntry"
Text="{Binding FirstName}" />
<Entry
x:Name="LastNameEntry"
Text="{Binding LastName}" />
Detail Page Code Behind
How to access value of ID for this record without a Xaml <Entry />
for it?
public DetailPage()
{
InitializeComponent();
BindingContext = this;
}
private async void Submit_Button_Clicked(object sender, EventArgs e)
{
MyDataModel MyRecord = new MyDataModel();
MyRecord.FirstName = FirstNameEntry.Text;
MyRecord.LastName = LastNameEntry.Text;
MyRecord.ID = ___How Do I Get ID Without Using an XAML Entry?___; // int.Parse(IdEntry.Text);
await SaveRecord(MyRecord);
}
Thank you
答案1
得分: 3
将模型通过构造函数传递
await Navigation.PushAsync(new DetailPage(MySelectedItem));
然后在 DetailPage
private MyDataModel model;
public DetailPage(MyDataModel data)
{
InitializeComponent();
BindingContext = model = data;
}
private async void Submit_Button_Clicked(object sender, EventArgs e)
{
// 你正在使用数据绑定,所以不需要显式设置任何属性值
await SaveRecord(model);
}
英文:
pass the model via the constructor
await Navigation.PushAsync(new DetailPage(MySelectedItem));
then in DetailPage
private MyDataModel model;
public DetailPage(MyDataModel data)
{
InitializeComponent();
BindingContext = model = data;
}
private async void Submit_Button_Clicked(object sender, EventArgs e)
{
// you are using data binding, so you do not
// explicitly need to set any property values
await SaveRecord(model);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论