Android MapFragment as custom control in MvvmCross

2019-09-17 22:14发布

问题:

I'm working on a project in which I can use MapFragment or SupportMapFragment as a custom control. I tried to mix custom control(N-18) and Fraggle(N-26) tutorial but I can't make it work.

My Custom Control Class:

public class CustomMapView : MvxFragment
{
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        var ignored = base.OnCreateView(inflater, container, savedInstanceState);
        return this.BindingInflate(Resource.Layout.CustomMapViewLayout, null);
    }
}

My CustomMapViewLayout.axml

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />

In the FirstView.axml I reference the CustomMapView:

<cc.CustomMapView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

(I use the ViewNamespaceAbbreviations in the Setup.cs)

When I try to deploy I get these errors:

MvxBind:Error View Type Not Found - cc.CustomMapView (I'm sure the namespace is correct) Android.Views.InflateException:Loading...

Is there anybody who managed to solve this kind problem?

回答1:

As far as I know, fragments have to be used within FragmentActivity parents and have to be loaded either using the fragment xml tag or using one of the (support) fragment manager methods for dynamic loading within a parent viewgroup.

The Mvx code which is reporting View Type Not Found - cc.CustomMapView definitely won't work - it's looking for a class which inherits Android View in that namespace, not looking for a Fragment.

There may be ways to write more 'dynamic' fragment xml code - but none of the mvvmcross contributors have unearthed any of these yet. For now, I think you'll have to use the Fragments more 'conventionally' - like the ways shown in the N=26 sample