【问题标题】:Passing data between activities in Xamarin在 Xamarin 中的活动之间传递数据
【发布时间】:2019-11-27 17:41:07
【问题描述】:

我无法将 MainActivity 中的数据共享到 Welcome 活动,即使我按照此页面的说明进行操作:https://developer.xamarin.com/recipes/android/fundamentals/activity/pass_data_between_activity/

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

我得到空值,不知道为什么。有什么建议或建议吗?

【问题讨论】:

    标签: c# android xamarin


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      如果你要去一个子视图,如果你想返回结果,请使用 StartActivityForResult。您可以在ActivityResult中获取返回对象:

      受保护的覆盖无效 OnActivityResult(int requestCode, [GeneratedEnum] 结果resultCode,Intent数据)

      所以这是在活动之间传递数据和返回数据的好方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多