2017-02-08 2 views
-1

Я пытаюсь передать дату (имя пользователя) из LoginPage в HomePage, который открывается кнопкой, я знаю, что мне нужно добавить код в функцию кнопки.Xamarin native shared, передает данные на другую страницу

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

    } 
} 

ответ

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

Это то, что вы ищете ,

Вы можете получить дополнительный в вашем HomeActivity, выполнив:

this.Intent.GetStringExtra("extraStuff"); 
+0

ли я хранить эту строку в HomeActivity в виде строки, а затем использовать его? –

+0

@ TareqAl-Abbar Nope! первый фрагмент кода, который вы добавили в MainActivity. Вы создаете намерение типа HomeActivity и устанавливаете дополнительную строку. Затем вы запускаете HomeActivity по StartActivity (намерение). Например, на экране HomeActivity в OnCreate вы можете получить дополнительную строку – Mittchel

0

Вы должны использовать StartActivity и задать настройки.

Войти кнопку Page пунктКодЧтобы

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") 

и так далее ..

Смежные вопросы