Xamarin Android 将变量从 Activity 传递到 Fragment 返回 null [英] Xamarin Android passing variable from Activity to Fragment returns null

查看:30
本文介绍了Xamarin Android 将变量从 Activity 传递到 Fragment 返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将变量从我的主要活动传递到片段.这就是我尝试这样做的方式:

I'm trying to pass variables from my main activity to a fragment. This is how i'm attempting to do it:

这是在我的活动中:

Bundle args = new Bundle (); 
    args.PutString ("header", header);
    args.PutString ("content", content);
    args.PutString ("footer", header);
    args.PutString ("imageLocation", imageLocation);

    exhibitMainFragment.Arguments = args;

    FragmentManager.BeginTransaction ()
        .Replace (Resource.Id.main_view, exhibitMainFragment)
        .AddToBackStack (null)
        .Commit ();

这是在我的片段中:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Android.OS.Bundle savedInstanceState)
        {

            var ignored = base.OnCreateView(inflater, container, savedInstanceState);

            var view = inflater.Inflate(Resource.Layout.MuseumInformation, null);

            content = this.Activity.Intent.GetStringExtra ("content");
            header = this.Activity.Intent.GetStringExtra ("header");
            footer = this.Activity.Intent.GetStringExtra ("footer");
            imageFilePath = this.Activity.Intent.GetStringExtra ("imageLocation");

但是没有任何变量被传递(它们在片段中都是空的).我在这里显然犯了一个基本错误.有人可以告诉我它是什么吗?或者告诉我一个更好的方法来传递数据.

But none of the variables are being passed (they are all empty in the fragment). I'm clearly making a fundamental error here. Can somebody tell me what it is please. Or show me a better way to pass the data across.

谢谢.

推荐答案

我是这样做的:

content = Arguments.GetString("content"); 
header = Arguments.GetString ("header");
footer = Arguments.GetString("footer");
imageFilePath = Arguments.GetString ("imageLocation");

这篇关于Xamarin Android 将变量从 Activity 传递到 Fragment 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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