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

时间:2017-02-08 06:53:48

标签: c# visual-studio 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);
            };
        }

    }
}

2 个答案:

答案 0 :(得分:1)

        Intent intent = new Intent(this, typeof(HomeActivity));
        intent.PutExtra("extrastuff", extraStuff);
        StartActivity(intent);

这就是你要找的东西。

您可以通过执行以下操作检索HomeActivity中的额外内容:

this.Intent.GetStringExtra("extraStuff");

答案 1 :(得分:0)

您应该使用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")

依旧......