How to get value of DataModel field in code behind without using an Entry control on Xaml page in MAUI C#?

huangapple go评论74阅读模式
英文:

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:

  1. Page 1 uses a CollectionView to display a list of names defined by MyDataModel (FirstName, LastName, ID). If the user selects an item in the list, then the program navigates to a detail page for that item.
  2. 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:

  1. Page 1 uses a CollectionView to display a list of names defined by MyDataModel (FirstName, LastName, ID). If the user selects an item in the list, then the program navigates to a detail page for that item.
  2. Page 2 displays the FirstName and LastName of the selected item using {Binding} to fill &lt;Entry /&gt; controls, plus a Submit button to commit any changes to my database. I do not want to use an &lt;Entry /&gt; 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 &lt;Entry /&gt; 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

&lt;Entry /&gt; fields on Detail Page in Xaml

&lt;Entry 
    x:Name=&quot;FirstNameEntry&quot;
    Text=&quot;{Binding FirstName}&quot; /&gt;

&lt;Entry 
    x:Name=&quot;LastNameEntry&quot;
    Text=&quot;{Binding LastName}&quot; /&gt;

Detail Page Code Behind

How to access value of ID for this record without a Xaml &lt;Entry /&gt; 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);
}

huangapple
  • 本文由 发表于 2023年5月21日 02:12:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定