本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!