Xamarin Monodroid: WP7 => Android and Custom Control?

一个人想着一个人 提交于 2019-12-06 00:59:06

You can do "custom controls" in Mono for Android too - and once you've written them, then you can include them in your axml files.

I'm afraid I don't have any perfectly simple examples to hand, but there's a complicated example in:

https://github.com/slodge/MvvmCross/blob/master/Sample%20-%20CirriousConference/Cirrious.Conference.UI.Droid/Resources/Layout/ChildPage_Twitter.axml

If you declare a class MyControl in MyNamespace and inherit that control from an Android View and you can then setup your custom control - including pulling in attributes from the XML - using a constructor like:

public MyControl(Context context, IAttributeSet attrs) { /* ... */ }

and using XML like:

<mynamespace.MyControl android:layout_height='wrap_content' />

One example of this could be the control from https://github.com/Cheesebaron/MonoDroid.HorizontalPager - which could be used from xml using xml like

 <mynamespace.controls.HorizontalPager
    android:id="@+id/MyPageHost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

You can make your own custom view by inheriting the View class. This allows you to do anything. Then you can reference it in your AXML with:

<your.awesome.namespace.AwesomeViewName
  android:id="@+id/awesomeView"
  android:layout...
  />

Just make sure your namespace name in the AXML is all lower case, otherwise it won't pick it up.

But if you just need a very simple AXML layout that you are going to use a lot, you can create a new AXML file and use the include tag to put it in there.

There is some more general info on some Layout Tricks for Android which will work for Mono for Android as well here: https://developer.android.com/resources/articles/layout-tricks-merge.html

Any reason why it won't enter the constructor? Here is my constructor:

protected CropImageView(IntPtr javaReference, JniHandleOwnership transfer)
        : base(javaReference, transfer)
    {

    }

The Init method causes the circular dependency when I inflate the crop_image_view xml

I have tried with public, private but no luck... here is my code: https://github.com/slown1/Xamarin.CircleImageCropper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!