English 中文(简体)
How to optimize detail page reset button in C# MAUI?
原标题:
  • 时间:2023-05-23 15:30:10
  •  标签:
  • c#
  • maui

I ve implemented a Reset button on a Detail page in C# MAUI and it works, but I can t figure out why I need to be so explicit in the way I capture the initial data to perform the reset, when requested.

Here s my code:

Simple 3 field data model:

public class MyDataModel
{
    [PrimaryKey, AutoIncrement]
    public int my_id { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
}

Reset button - when clicked, the form refills with the initial data perfectly using this:

private void Reset_Button_Clicked(object sender, EventArgs e)
{
    BindingContext = model = InitialData;
}

Detail page constructor - here s where my question is...

private MyDataModel model;

MyDataModel InitialData = new MyDataModel();

public DetailPageMyDataModel MyData)
{
    InitializeComponent();
    BindingContext = model = MyData;

    InitialData = MyData;   // Here s what I d like to do, but it doesn t reset the fields

    InitialData.first_name = MyData.first_name;   // Instead, I have to use these two lines
    InitialData.last_name = MyData.last_name;     // Why???
}

Why isn t InitialData = MyData alone setting my InitialData and giving me the ability to reset my form later?

Unfortunately, it doesn t do anything - the reset doesn t work if I only use that line.

So, instead, I have to use the two lines below to get my reset button to work. (Note: I can remove InitialData = MyData line as long as I include the other two.

Any help is appreciated!

Thanks

问题回答

From document BindableObject.BindingContextChanged Event, we can find that :

event BindingContextChanged Raised whenever the BindingContext property changes.

Though it is a xamrin form document, it also applies to MAUI.

If we change the value of the properties of the current BindingContext, event BindingContextChanged will be triggered.

But if you use code InitialData = MyData;, the InitialData will refer to another instance that MyData point to. That is to say, InitialData will no longer point to the previous object and memory space. Therefore, in this case, the BindingContextChanged event of the current page s BindingContext object will no longer be fired.

InitialData.first_name = MyData.first_name;   // Instead, I have to use these two lines
InitialData.last_name = MyData.last_name;     // Why???

Conversely, if you use above code, you are not changing the object pointed to by the BindingContext of the current page, you are only changing the value of the property of this object.In this case, event BindingContextChanged will raise.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签