Xamarin 本机共享,将数据传递到另一个页面



我正在尝试将日期(用户名(从LoginPage传递到由按钮打开的HomePage,我知道我需要在按钮函数中添加一些代码。

namespace UniLife.Droid
{
    [Activity (Label = "UniLife.Droid", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        public Task NavigationPage { get; private set; }
        MyClass myclass = new MyClass();
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.LogInPage);
            // Get our buttons and TextBoxes from the layout resource
            Button LoginButton = FindViewById<Button>(Resource.Id.LoginButton);
            EditText Username = FindViewById<EditText>(Resource.Id.UsernameTextBox);
            EditText Password = FindViewById<EditText>(Resource.Id.PasswordTextBox);
            // Set a function to the button on click
            LoginButton.Click += delegate
            {
                SetContentView(Resource.Layout.HomePage);
            };
        }
    }
}
        Intent intent = new Intent(this, typeof(HomeActivity));
        intent.PutExtra("extrastuff", extraStuff);
        StartActivity(intent);

这就是你要找的。

您可以通过执行以下操作在"家庭活动"中检索额外的内容:

this.Intent.GetStringExtra("extraStuff");

您应该使用StartActivity并设置首选项。

登录页面按钮单击代码

   ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
        ISharedPreferencesEditor editor = prefs.Edit();
        editor.PutString("Var1", "Val1");
        editor.PutString("Var2", "val2");

        editor.Apply();
        Intent intent = new Intent(this, typeof(HomePageActivity));
        intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);
        StartActivity(intent);
        Finish();

然后在主页代码的 OnCreate 方法中

ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
    var val1 = prefs.GetString("var1", "0")

等等..

相关内容

最新更新