简体   繁体   English

Xamarin Monodroid:WP7 => Android和自定义控制?

[英]Xamarin Monodroid: WP7 => Android and Custom Control?

i am in the process of porting some of my Windows Phone Applications to Android using Xamarin Monodroid. 我正在使用Xamarin Monodroid将我的一些Windows Phone应用程序移植到Android。

I am pretty new with the Xamarin stuff, just bought a license actually. 我对Xamarin的东西很新,只是买了一张牌照。

So far so good as far as recreating the XAML UI in AXML but i am facing a problem with Custom Controls. 到目前为止,在AXML中重新创建XAML UI非常好,但我遇到了自定义控件的问题。

Here is what i mean by custom controls: 这就是我所说的自定义控件:

In .NET, i created a bunch of controls by creating class that inherit from the 'UserControl' class, i created the logic and set the Content. 在.NET中,我通过创建继承自'UserControl'类的类创建了一堆控件,我创建了逻辑并设置了内容。 Then i just create new instance with 'new my_control()', etc... 然后我用'new my_control()'等创建新实例......

Some of my controls are not created this way but instead i created the UserControl by defining the XAML, where there is no specific logics but when i need to combine 2 or more controls(for example, a colored square with text beside it, so Rectangle + TextBlock) and again i just need to do 'new my_control()' and add it somewhere in the XAML UI(Grid, ListBox, StackPanel, etc...). 我的一些控件不是以这种方式创建的,而是通过定义XAML来创建UserControl,其中没有特定的逻辑,但是当我需要组合2个或更多控件时(例如,带有文本的彩色正方形,所以Rectangle + TextBlock)我再次需要做'new my_control()'并将其添加到XAML UI(Grid,ListBox,StackPanel等)中的某个位置。

How can i achieve something similar with Monodroid? 我怎么能用Monodroid实现类似的东西?

Thanks in advance! 提前致谢!

You can make your own custom view by inheriting the View class. 您可以通过继承View类来创建自己的自定义视图。 This allows you to do anything. 这可以让你做任何事情。 Then you can reference it in your AXML with: 然后,您可以在AXML中引用它:

<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. 只需确保AXML中的命名空间名称都是小写,否则它将不会提取它。

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. 但是如果您只需要一个非常简单的AXML布局,那么您可以创建一个新的AXML文件并使用include标记将其放在那里。

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 关于Android的一些布局技巧有一些更一般的信息,这对于Android的Mono也适用: https//developer.android.com/resources/articles/layout-tricks-merge.html

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. 您也可以在Mono for Android中执行“自定义控件” - 一旦您编写了它们,就可以将它们包含在您的axml文件中。

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 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: 如果在MyNamespace声明一个类MyControl并从Android View继承该控件,则可以使用如下构造函数设置自定义控件 - 包括从XML中提取属性 -

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

and using XML like: 并使用XML如:

<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 其中一个例子可能是来自https://github.com/Cheesebaron/MonoDroid.Horizo​​ntalPager的控件 - 可以使用xml中的xml来使用

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

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 当我给crop_image_view xml充气时,Init方法会导致循环依赖

I have tried with public, private but no luck... here is my code: https://github.com/slown1/Xamarin.CircleImageCropper 我试过公共,私有但没有运气......这是我的代码: https//github.com/slown1/Xamarin.CircleImageCropper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM