在XAML内定义一个集合

Sup*_*JMN 5 xaml markup-extensions xamarin.forms

我想创建一个绑定到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>
Run Code Online (Sandbox Code Playgroud)

但是,如果我删除了 <x:Array >... </x:Array>

我究竟做错了什么?

Fab*_*e T 10

我看到您正在使用XF 标准标记扩展。你的错误似乎在Type="{x:Type sys:String}", 而不是sys:String你应该写x:String出现在常见的xmlns:x

在这个示例中,我用字符串填充了一个列表视图

<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>
Run Code Online (Sandbox Code Playgroud)


Ste*_*oix 5

您可以使用内置的 x:Array

<x:Array Type="{x:Type sys:String}" x:Key="array">
    <x:String>Hello</x:String>
    <x:String>World</x:String>
</x:Array>
Run Code Online (Sandbox Code Playgroud)

sys定义为xmlns:sys="clr-namespace:System;assembly=mscorlib"

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

<scg:List x:TypeArguments="{x:Type sys:String}" x:Key="genericList">
    <x:String>Hello</x:String>
    <x:String>World</x:String>
</scg:List>
Run Code Online (Sandbox Code Playgroud)

sys如前定义,和scgxmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"