【发布时间】:2016-06-07 05:32:28
【问题描述】:
我只想用 Xamarin.Forms 创建一个自定义控件。此自定义控件应采用 xaml 中的视图数组/列表(例如 Stacklayout)。
我的 XAML 如下所示:
<controls:MyLayout>
<ContentView BackgroundColor="Green">
<Label Text="Test 1" />
</ContentView>
<ContentView BackgroundColor="Red">
<Label Text="Test 1" />
</ContentView>
</controls:MyLayout>
还有MyLayout的类:
[ContentProperty("Children")]
public class MyLayout: ScrollView, IViewContainer<View>
{
public IList<View> Children { get; set; }
// And some more properties and methods ...
}
但是当我编译它并在我的 Android 手机上运行它时,我收到以下错误:
调用的目标已抛出异常。 ---> Xamarin.Forms.Xaml.XamlParseException:位置 29:8。无法分配 属性“儿童”:“Xamarin.Forms.ContentView”之间的类型不匹配 和“System.Collections.Generic.IList`1[Xamarin.Forms.View]”
我该如何解决这个问题?我错过了什么?
【问题讨论】:
-
不,这不是问题所在。使用“ContentView”时,我遇到了同样的错误:“Xamarin.Forms.ContentView”和“System.Collections.Generic.IList`1[Xamarin.Forms.ContentView]”之间的类型不匹配
-
您可以参考
Xamarin.Forms源来创建您的控件,因为它是开源的。你可以在github.com/xamarin/Xamarin.Forms找到代码
标签: xaml xamarin custom-controls xamarin.forms