【问题标题】:Unable to Resolve: FragmentManager.FindFragmentById<MapFragment>无法解决:FragmentManager.FindFragmentById<MapFragment>
【发布时间】:2015-10-19 04:25:45
【问题描述】:

以下行无法编译:

var mapFragment = FragmentManager.FindFragmentById&lt;MapFragment&gt;(Resource.Id.map);

错误 CS0120 非静态字段、方法或属性“FragmentManager.FindFragmentById(int)”需要对象引用

我的 axml 如下:

  <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/map"
            android:layout_width="200dp"
            android:layout_height="200dp"
            class="com.google.android.gms.maps.MapFragment" />

我引用了以下documentation

有什么建议吗?

【问题讨论】:

  • 在哪个上下文中,你是在执行代码获取片段吗?

标签: android android-fragments xamarin xamarin.forms mapfragment


【解决方案1】:

根据您的目标文档建议,我找到了这个。

_myMapFragment = MapFragment.NewInstance();
 FragmentTransaction tx = FragmentManager.BeginTransaction();
 tx.Add(Resource.Id.map, _myMapFragment);
 tx.Commit();
 GoogleMap map = myMapFragment.Map;
 if (map != null) {
 // The GoogleMap object is ready to go.
 }

【讨论】:

  • 我得到一个编译错误: FragmentTransaction tx = FragmentManager.BeginTransaction(); "非静态字段需要对象引用"
【解决方案2】:

FragmentManager 是 Activity 类的 getter 属性,如果您的代码不在 Activity 的上下文(方法)中执行,则需要离开上下文对象。

【讨论】:

    猜你喜欢
    • 2015-02-25
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 2016-03-30
    • 2018-04-21
    相关资源
    最近更新 更多