在XAML中定义一个集合

在XAML中定义一个集合,xaml,xamarin.forms,markup-extensions,Xaml,Xamarin.forms,Markup Extensions,我想创建一个绑定到XAML中定义的字符串集合 在WPF中,我可以创建一个ArrayList,作为一个带有键的资源,准备用作绑定的源(使用StaticResource) 这在Xamarin形式中可能吗 编辑:我用@Stephane Delcroix提出的解决方案尝试了这个XAML,但我遇到了一个未处理的异常: <?xml version="1.0" encoding="utf-8"?> <ContentPage xmlns="http://xamarin.com/schemas

我想创建一个绑定到XAML中定义的字符串集合

在WPF中,我可以创建一个
ArrayList
,作为一个带有键的资源,准备用作绑定的源(使用StaticResource)

这在Xamarin形式中可能吗

编辑:我用@Stephane Delcroix提出的解决方案尝试了这个XAML,但我遇到了一个未处理的异常:

<?xml version="1.0" encoding="utf-8"?>

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             x:Class="ReferenceApp.Views.GamesPage"
             Title="Games">


    <ContentPage.Resources>
        <x:Array Type="{x:Type sys:String}" x:Key="array">
            <x:String>Hello</x:String>
            <x:String>World</x:String>
        </x:Array>
    </ContentPage.Resources>
    <Grid />

</ContentPage>

你好
世界
但是,如果我删除


我做错了什么?

您可以使用内置的
x:Array


你好
世界
定义为
xmlns:sys=“clr namespace:System;assembly=mscorlib”

或任何您喜欢的收藏,例如
List


你好
世界

由于前面定义了
sys
,并且
scg
xmlns:scg=“clr namespace:System.Collections.Generic;assembly=mscorlib”
我看到您使用的是XF标准标记扩展。您的错误似乎出现在
Type=“{x:Type sys:String}”
中,而不是sys:String,您应该编写出现在公共xmlns:x中的x:String

在这个示例中,我用字符串填充listview

<ListView Margin="10">
    <ListView.ItemsSource>
        <x:Array Type="{x:Type x:String}">
            <x:String>Hello</x:String>
            <x:String>World</x:String>
        </x:Array>
    </ListView.ItemsSource>            
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Label Text="{Binding}" />
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>                
</ListView>

你好
世界

您试图将列表绑定到哪个控件以及该控件的哪个属性?我知道如何为
Picker.Items
执行此操作,但不确定是否使用
ListView.ItemSource
示例。我想使用ListView的ItemsSource您可能仍然可以使用
Picker
示例。我已经尝试过,但在Android中我遇到了一个未处理的异常。请参阅我在OP中的编辑。
字符串
未在
系统中定义。运行时
但在
mscorlib
中定义。如果我没有弄错,修改问题通常会使答案不相关,因此请避免这样做。抱歉,我不知道其他粘贴XAML.BTW的方法,我修改了XAML,将xmlns指向mscorlib,结果是一样的:我得到了一个未处理的异常。