C# 在活动中按Id获取片段
我的布局如下:C# 在活动中按Id获取片段,c#,android,xamarin,C#,Android,Xamarin,我的布局如下: <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment class="com.x
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
class="com.xamarin.recipes.filepicker.FileListFragment"
android:id="@+id/FileListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
但我在fileListFragment的监视中得到了“未知标识符:fileListFragment”
更新。
活动代码:
using Android.Support.V4.App;
using Android.OS;
namespace com.xamarin.recipes.filepicker
{
[Android.App.Activity(Label = "Choose file", MainLauncher = true, Icon = "@drawable/icon")]
public class FilePickerActivity : FragmentActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.ChooseAudio);
var fileListFragment = (FileListFragment)SupportFragmentManager.FindFragmentById(Resource.Id.FileListFragment);
}
}
}
使用以下命令:
getSupportFragmentManager()
.findFragmentById(R.id.FileListFragment))
我目前没有使用Xamarin,但是我取消删除我的答案,因为我认为我接近解决方案,这可以帮助您。我发现了一些使用Xamarin的教程或GitHub项目。这些搜索并没有给我提供正确的解决方案,但我认为它们为我提供了解决问题的线索。请尝试以下代码:
using Android.App;
using Android.OS;
using Android.Support.V4.App;
// using Fragment = Android.Support.V4.App.Fragment;
namespace com.xamarin.recipes.filepicker
{
[Activity(Label = "Choose file", MainLauncher = true, Icon = "@drawable/icon")]
public class FilePickerActivity : FragmentActivity
{
// use Android.Support.V4.App.Fragment below
private FileListFragment mFileListFragment;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.ChooseAudio);
mFileListFragment = (FileListFragment)SupportFragmentManager.FindFragmentById(Resource.Id.fileListFragment);
// Avoid any confusion: change its id in your layout by "fileListFragment"
}
}
}
我使用Fragment类FileListFragment
更改了var
,该变量似乎未使用Android.Support.V4.App.Fragment
定义,错误是:未知标识符
注意:确保FileListFragment
扩展了Android.Support.V4.App.Fragment
,如下所示:
public class FileListFragment : Android.Support.V4.App.Fragment
{
...
}
要使用var
,我想您需要通过将其转换为Android.Support.V4.App.Fragment
(一个比较器),如下所示:
var fileListFragment = SupportFragmentManager
.FindFragmentById(Resource.Id.fileListFragment)
as FileListFragment; // you need this line
试试这个代码。我不确定,但我认为应该行得通
var fileListFragment = SupportFragmentManager.FindFragmentById<FileListFragment>(Resource.Id.FileListFragment);
var fileListFragment=SupportFragmentManager.findframentbyid(Resource.Id.fileListFragment);
你有没有试着把碎片弄成这样
你已经知道你尝试过的方法行不通,所以我不会再说一遍。 请注意,片段的视图不能像这样访问。 尝试使用ADT创建一个带有选项卡导航的项目,然后查看代码内部,它将澄清您的概念。 向导还将为您生成一个虚拟选项卡片段类,如下所示
public static class FragHomeUI extends Fragment
将在类的以下成员中创建并访问片段视图:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_home, container,
false);
请再说一遍,通过ADT向导创建几个示例项目,并仔细观察代码。这可能会有所帮助
FragmentManager fm = getSupportFragmentManager();
Fragment fragment_byID = fm.findFragmentById(R.id.fragment_id);
//OR
Fragment fragment_byTag = fm.findFragmentByTag("fragment_tag");
请在Xamarin.Android 4.12.02001中尝试此代码:
FileListFragment FileListFragment=Android.Runtime.Extensions.JavaCast(SupportFragmentManager.FindFragmentById(Resource.Id.Id_file_list_fragment));
转到这里,为什么要在java中使用var
?Javascript是另一种语言:)如果您使用support library,请尝试如下操作:getSupportFragmentManager().findFragmentById
。@Hardy这是Xamarin。。。不是Javascript!您可以试试这个:FileListFragment fragment=(FileListFragment)SupportFragmentManager.FindFragmentById(Resource.Id.FileListFragment)
@ManishMulimani,尝试过-没有帮助问题是关于Xamarin的,在中没有键入的参数FindFragmentById@Idsa“FragmentManager”中存在类型化参数。我认为“SupportFragmentManager”也有同样的方法。似乎文档已经过时了。您在回答之前阅读了问题和注释吗?这个问题是关于!这不是使用这种跨平台开发创建片段的正确方法。和其他方法一样:在回答之前,您是否阅读了问题和注释?这个问题是关于!这不是使用此跨平台开发的正确方法。活动也可以使用FragmentManager查找片段,如:var emailList=FragmentManager.FindFragmentById(Resource.Id.email\u list\u fragment);你到底不能做什么?
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_home, container,
false);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment_byID = fm.findFragmentById(R.id.fragment_id);
//OR
Fragment fragment_byTag = fm.findFragmentByTag("fragment_tag");