Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a collection inside XAML

I want to create a Binding to a collection of strings defined inside XAML.

In WPF I could create an ArrayList as a resource with a key, ready to be used as the source of a Binding (using a StaticResource).

Is this possible in Xamarin Forms?

EDIT: I've tried with this XAML with the solution proposed by @Stephane Delcroix, but I'm getting an Unhandled Exception:

<?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>

However, the exception is not thrown if I remove the <x:Array >... </x:Array>

What am I doing wrong?

like image 251
SuperJMN Avatar asked Oct 03 '16 16:10

SuperJMN


People also ask

How do you bind data in collection view in xamarin forms?

Populate a CollectionView with data CollectionView collectionView = new CollectionView(); collectionView. SetBinding(ItemsView. ItemsSourceProperty, "Monkeys"); In this example, the ItemsSource property data binds to the Monkeys property of the connected viewmodel.

What is collection view in xamarin forms?

CollectionView is built to be fast, uses modern, native controls, and removes the concept of ViewCells. CollectionView is available on iOS and Android, but is only partially available on the Universal Windows Platform. In this article, we'll dive into some of the main features of CollectionView in Xamarin Forms .

What is WPF CollectionViewSource?

CollectionViewSource is a proxy for a CollectionView class, or a class derived from CollectionView. CollectionViewSource enables XAML code to set the commonly used CollectionView properties, passing these settings to the underlying view.

Why is XAML?

XAML is just another simple and easy way to design UI elements. With XAML, it doesn't mean that what you can do to design UI elements is the only way. You can either declare the objects in XAML or define them using code. XAML is optional, but despite this, it is at the heart of WPF design.


2 Answers

I see that you are using the XF standard markup extensions. Your mistake seems to be in Type="{x:Type sys:String}", instead of sys:String you should write x:String which appears in the common xmlns:x

In this sample I fill a listview with strings

<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>
like image 141
Fabrice T Avatar answered Oct 07 '22 13:10

Fabrice T


You can use the built-in x:Array

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

with sys defined as xmlns:sys="clr-namespace:System;assembly=mscorlib"

or any collection you like, e.g. List

<scg:List x:TypeArguments="{x:Type sys:String}" x:Key="genericList">
    <x:String>Hello</x:String>
    <x:String>World</x:String>
</scg:List>

with sys defined as before, and scg being xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"

like image 7
Stephane Delcroix Avatar answered Oct 07 '22 11:10

Stephane Delcroix