Xamarin Monodroid:WP7 =>Android 和自定义控件? [英] Xamarin Monodroid: WP7 => Android and Custom Control?
问题描述
我正在使用 Xamarin Monodroid 将我的一些 Windows Phone 应用程序移植到 Android.
i am in the process of porting some of my Windows Phone Applications to Android using Xamarin Monodroid.
我对 Xamarin 的东西很陌生,实际上只是买了一个许可证.
I am pretty new with the Xamarin stuff, just bought a license actually.
就在 AXML 中重新创建 XAML UI 而言,到目前为止一切顺利,但我遇到了自定义控件的问题.
So far so good as far as recreating the XAML UI in AXML but i am facing a problem with Custom Controls.
这就是我所说的自定义控件的意思:
Here is what i mean by custom controls:
在 .NET 中,我通过创建继承自UserControl"类的类创建了一堆控件,我创建了逻辑并设置了内容.然后我只是用'new my_control()'等创建新实例......
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. Then i just create new instance with 'new my_control()', etc...
我的一些控件不是以这种方式创建的,而是通过定义 XAML 创建了 UserControl,其中没有特定的逻辑,但是当我需要组合 2 个或更多控件时(例如,带有文本的彩色方块旁边,所以 Rectangle + TextBlock) 再次我只需要执行new my_control()"并将其添加到 XAML UI(网格、ListBox、StackPanel 等...)中的某处.
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...).
我如何使用 Monodroid 实现类似的功能?
How can i achieve something similar with Monodroid?
提前致谢!
推荐答案
您也可以在 Mono for Android 中进行自定义控件" - 一旦您编写了它们,就可以将它们包含在您的 axml 文件中.
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:
如果您在 MyNamespace
中声明一个类 MyControl
并从 Android View
继承该控件,然后您可以设置自定义控件 - 包括从 XML 中提取属性 - 使用如下构造函数:
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) { /* ... */ }
并使用 XML 如下:
and using XML like:
<mynamespace.MyControl android:layout_height='wrap_content' />
<小时>
一个例子可能是来自 https://github.com/Cheesebaron/MonoDroid.HorizontalPager 的控件 - 可以从 xml 使用 xml 使用,如
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"
/>
这篇关于Xamarin Monodroid:WP7 =>Android 和自定义控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!