首页 > 解决方案 > Xamarin - 如何将对象传递给事件处理程序

问题描述

免责声明- 我需要一些指导,因为我对 C# 和 Xamarin非常陌生。非常感谢示例。

基本上,我在 Page1 中有一个名为“newuserdata”的对象,我将其传递给 Page2。

第 1 页 - RegistrationUserPage.xaml.cs

...
    else
                    {

                        Userdata newuserdata = new Userdata();
                        newuserdata.firstname   = firstname;
                        newuserdata.lastname    = lastname;
                        newuserdata.birthday    = birthday;
                        newuserdata.phone       = phone;
                        newuserdata.password    = password;

                        var RegistrationUserBillingPage = new RegistrationUserBillingPage(newuserdata);
                        await Navigation.PushAsync(RegistrationUserBillingPage);

第 2 页 - RegistrationUserBillingPage.xaml.cs

...
[XamlCompilation(XamlCompilationOptions.Compile)]
        public partial class RegistrationUserBillingPage : ContentPage
        {
            public Userdata newuserdata;

        public RegistrationUserBillingPage(Userdata newuserdata)
        {
            InitializeComponent();
            this.newuserdata = newuserdata;

            // this works fine and I get the data
            first.Text = newuserdata.firstname;
        }

        async void SubmitButton_Clicked(object sender, EventArgs e)
        {
            //How do I get the object to this event so that I could get or set the values?

            // I would like to get the value
            /*
            second.Text = newuserdata.lastname;
            */

            // .. and I would like to set the value
            /*
            newuserdata.city = CityEntry.Text;
            */
        }
    }
}

所以问题是 - 我如何在我的“SubmbitButton_Clicked”方法中访问对象?

标签: c#xamarin

解决方案


你试过 this.newuserdata.lastname 吗?- 一个朋友

我没有正确触发事件。谢谢@朋友!


推荐阅读