引用在XAML中创建的静态对象

时间:2019-06-12 19:31:34

标签: xaml xamarin

我正在尝试使用XAML创建一些设计时数据。恐怕我找不到有关如何通过引用其他XAML创建的对象来创建数组的指南。与此类似...

<MyClass x:Key="Obj1" Prop1="..." Prop2="..." />
<MyClass x:Key="Obj2" Prop1="..." Prop2="..." />
<x:Array Type={x:Type my:MyClass} x:Key="MyObjects">
  <StaticResource Key="Obj1" />
  <StaticResource Key="Obj2" />
</x:Array>

不幸的是,以上方法无效。有什么可行的吗?

我通过使用适用于Mac的最新VS Studio创建一个空的Xamarin Forms应用程序来重现该错误。我将此添加到了ContentPage.Resources ...

    <ContentPage.Resources>
        <ResourceDictionary>
            <n:String x:Key="myString">Hello World</n:String>
            <x:Array x:Key="myArray" Type="{x:Type n:String}">
                <StaticResource Key="myString" />
            </x:Array>
        </ResourceDictionary>
    </ContentPage.Resources>

在应用程序启动时出现以下错误:

Xamarin.Forms.Xaml.XamlParseException
... StaticResource not found for key myString

我正在使用最新版本的Visual Studio for Mac构建Xamarin.Forms应用程序。

此外,我知道我可以将对象创建为数组的直接子级。我试图分别创建它们,以便可以将它们包含在不同的集合中。

更新(根据@ junior-jiang-msft的建议)

我尝试了Junior Jiang的建议,但遇到以下构建错误...

/Users/savasp/Projects/test/test/MainPage.xaml: Error: The given key 'Xamarin.Forms.Xaml.XmlName' was not present in the dictionary.

这是新的香草Xamarin.Forms项目中的示例XAML表单...

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="test.MainPage">
    <ContentPage.Resources>
        <x:String x:Key="m1">hello</x:String>
        <x:String x:Key="m2">world</x:String>
        <x:Array x:Key="array" Type="{x:Type x:String}">
            <Setter Value="{StaticResource m1}" />
            <Setter Value="{StaticResource m2}" />
        </x:Array>
    </ContentPage.Resources>
    <StackLayout>
        <!-- Place new controls here -->
        <ListView ItemsSource="{StaticResource array}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Label Text="{Binding}" />
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage>

2 个答案:

答案 0 :(得分:0)

您应该这样声明:

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

或者,另一个使用颜色对象的示例:

 <ListView.ItemsSource>
            <x:Array Type="{x:Type Color}">
                <Color>Aqua</Color>
                <Color>Black</Color>
                <Color>Blue</Color>
                <Color>Fuchsia</Color>
                <Color>Gray</Color>
                <Color>Green</Color>
                <Color>Lime</Color>
                <Color>Maroon</Color>
                <Color>Navy</Color>
                <Color>Olive</Color>
                <Color>Pink</Color>
                <Color>Purple</Color>
                <Color>Red</Color>
                <Color>Silver</Color>
                <Color>Teal</Color>
                <Color>White</Color>
                <Color>Yellow</Color>
            </x:Array>
        </ListView.ItemsSource>

您可以查看XAML标记文档here

答案 1 :(得分:0)

也许您对ResourceDictionary有误解。对于StaticResource的值和赋值。

<ContentPage...>
    <ContentPage.Resources>
        <x:String x:Key="myStringOne">Hello String One</x:String>
        <x:String x:Key="myStringTwo">Hello String Two</x:String>
        <x:Array x:Key="myArray" Type="{x:Type x:String}">
          <x:String>mono</x:String>
          <x:String>monodroid</x:String>
          <x:String>monotouch</x:String>
          <x:String>monorail</x:String>
          <x:String>monodevelop</x:String>
          <x:String>monotone</x:String>
          <x:String>monopoly</x:String>
          <x:String>monomodal</x:String>
          <x:String>mononucleosis</x:String>
      </x:Array>
    </ContentPage.Resources>

    <StackLayout>
        <ListView ItemSource="{StaicResource myArray}"/>
    </StackLayout>
</ContentPage>

您可以参考本文档Creating and Consuming a ResourceDictionary