在Xamarin中的活动之间传递数据 [英] Passing data between activities in Xamarin

查看:83
本文介绍了在Xamarin中的活动之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使遵循此页面的说明,我也无法将MainActivity中的数据共享给Welcome活动:

I can't share the data I have in MainActivity to the Welcome activity, even though I followed the instructions of this page: https://developer.xamarin.com/recipes/android/fundamentals/activity/pass_data_between_activity/

MainActivity(Activity1)

MainActivity (Activity1)

Button button = FindViewById<Button>(Resource.Id.send);
EditText myName = FindViewById<EditText>(Resource.Id.name);
string name = myName.Text;

button.Click += delegate { 
    var welcome = new Intent(this, typeof(Welcome));
    welcome.PutExtra("name", name);
    StartActivity(welcome);
};

欢迎(活动2)

string name = Intent.GetStringExtra("name") ?? "Data not available";

我为空,不知道为什么.有什么建议或建议吗?

I get null, don't know why. Any suggestions or advise?

推荐答案

单击按钮时必须获取文本,否则它将没有任何值(因为在创建UI时,EditText将为空,因此null值那个时候)

You have to fetch the text when the button is clicked otherwise it will have no value( because when the UI is created, EditText would be empty hence null value at that time ) so

string name = null;
button.Click += delegate { 
        name = myName.Text;
        var welcome = new Intent(this, typeof(Welcome));
        welcome.PutExtra("name", name);
        StartActivity(welcome);
    };

这篇关于在Xamarin中的活动之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆