C# Xamarin本机共享,将数据传递到另一页

C# Xamarin本机共享,将数据传递到另一页,c#,visual-studio,xamarin,C#,Visual Studio,Xamarin,我试图将日期(用户名)从LoginPage传递到通过按钮打开的主页,我知道我需要在按钮功能中添加一些代码 namespace UniLife.Droid { [Activity (Label = "UniLife.Droid", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { public Task NavigationPage {

我试图将日期(用户名)从
LoginPage
传递到通过按钮打开的
主页
,我知道我需要在按钮功能中添加一些代码

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);
            };
        }

    }
}
namespace UniLife.Droid
{
[活动(Label=“UniLife.Droid”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共课活动:活动
{
公共任务导航页面{get;private set;}
MyClass MyClass=新的MyClass();
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.LogInPage);
//从布局资源中获取按钮和文本框
按钮LoginButton=findviewbyd(Resource.Id.LoginButton);
EditText用户名=FindViewById(Resource.Id.UsernameTextBox);
EditText Password=findviewbyd(Resource.Id.PasswordTextBox);
//将功能设置为单击按钮
登录按钮。单击+=委派
{
SetContentView(Resource.Layout.HomePage);
};
}
}
}

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

        Intent intent = new Intent(this, typeof(HomeActivity));
        intent.PutExtra("extrastuff", extraStuff);
        StartActivity(intent);
这就是你要找的

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

this.Intent.GetStringExtra("extraStuff");

我是否将该行作为字符串存储在HomeActivity中,然后使用它?@TareqAl Abbar Nope!您在MainActivity中输入的第一段代码。您正在创建HomeActivity类型的意图,并在其上设置额外的字符串。然后通过StartActivity(意图)启动HomeActivity。例如,在OnCreate的HomeActivity屏幕中,您可以检索额外的字符串