Xamarin.Forms - 使用enum作为触发器

时间:2017-03-08 10:21:11

标签: c# enums triggers xamarin.forms

我正在尝试根据针对枚举属性的触发器来改变StackLayout的背景颜色。

但是我得到了:"发生了未处理的异常"。

当我移除触发器时,一切都按预期工作。以下是相关代码:

<?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:local="clr-namespace:ParkAndRidePrague"
             xmlns:my="clr-namespace:ParkAndRidePrague.Core.Enums;assembly=ParkAndRidePrague.Core"
             x:Class="ParkAndRidePrague.MainPage">
    <ContentPage.Content>
        <ListView  x:Name="listViewParkings">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout BackgroundColor="#eee"
                        Orientation="Vertical">
                            <StackLayout Orientation="Horizontal">
                                <Label Margin="10, 5, 0, 5" VerticalTextAlignment="Center" Text="{Binding Name}" TextColor="Black" FontSize="20" />
                                <Label Margin="0, 5, 10, 5" VerticalTextAlignment="Center" Text="{Binding NumOfFreePlaces}" HorizontalOptions="EndAndExpand" TextColor="Black" FontSize="20" />
                                <StackLayout.Triggers>
                                    <DataTrigger Binding="{Binding Path=ParkingAvailability}" Value="{x:Static my:ParkingAvailability.Low}">
                                        <Setter Property="BackgroundColor" Value="Red" />
                                    </DataTrigger>
                                </StackLayout.Triggers>
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content> </ContentPage>

这是枚举:

namespace ParkAndRidePrague.Core.Enums {
    public enum ParkingAvailability
    {
        Low,
        Medium,
        High
    } }

我跟着this question/answer,但似乎还有一些我无法找到的东西。

整个例外:

  

03-08 11:33:12.511 I / MonoDroid(14705):UNHANDLED EXCEPTION:03-08   11:33:12.567 I / MonoDroid(14705):   Xamarin.Forms.Xaml.XamlParseException:位置18:38。财产   需要TargetType来创建Xamarin.Forms.DataTrigger对象。   03-08 11:33:12567 I / MonoDroid(14705):at   Xamarin.Forms.Xaml.CreateValuesVisitor.Visit   (Xamarin.Forms.Xaml.ElementNode节点,Xamarin.Forms.Xaml.INode   parentNode)[0x0014b] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Xaml \ CreateValuesVisitor.cs:77   03-08 11:33:12.568 I / MonoDroid(14705):at   Xamarin.Forms.Xaml.ElementNode.Accept   (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor,Xamarin.Forms.Xaml.INode   parentNode)[0x000b1] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Xaml \ XamlNode.cs:176   03-08 11:33:12.568 I / MonoDroid(14705):at   Xamarin.Forms.Xaml.ElementNode.Accept   (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor,Xamarin.Forms.Xaml.INode   parentNode)[0x00054] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Xaml \ XamlNode.cs:171   03-08 11:33:12.568 I / MonoDroid(14705):at   Xamarin.Forms.Xaml.ElementNode.Accept   (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor,Xamarin.Forms.Xaml.INode   parentNode)[0x00089] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Xaml \ XamlNode.cs:173   03-08 11:33:12.568 I / MonoDroid(14705):at   Xamarin.Forms.Xaml.ElementNode.Accept   (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor,Xamarin.Forms.Xaml.INode   parentNode)[0x00089] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Xaml \ XamlNode.cs:173   03-08 11:33:12.568 I / MonoDroid(14705):at   Xamarin.Forms.Xaml.ApplyPropertiesVisitor + LT;&GT; c__DisplayClass32_0.b__0   ()[0x0008a] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Xaml \ ApplyPropertiesVisitor.cs:474   03-08 11:33:12.568 I / MonoDroid(14705):at   Xamarin.Forms.ElementTemplate.CreateContent()[0x00026] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ ElementTemplate.cs:78   03-08 11:33:12.568 I / MonoDroid(14705):at   Xamarin.Forms.DataTemplateExtensions.CreateContent   (Xamarin.Forms.DataTemplate self,System.Object项,   Xamarin.Forms.BindableObject容器)[0x00014] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ DataTemplateExtensions.cs:12   03-08 11:33:12.568 I / MonoDroid(14705):at   Xamarin.Forms.TemplatedItemsList 2[TView,TItem].CreateContent (System.Int32 index, System.Object item, System.Boolean insert) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\TemplatedItemsList.cs:527 03-08 11:33:12.568 I/MonoDroid(14705): at Xamarin.Forms.TemplatedItemsList 2 [TView,TItem] .ConvertContent   (System.Int32 startingIndex,System.Collections.IList项,   System.Boolean forceCreate,System.Boolean setIndex)[0x00014] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ TemplatedItemsList.cs:664   03-08 11:33:12.568 I / MonoDroid(14705):at   Xamarin.Forms.TemplatedItemsList 2[TView,TItem].OnProxyCollectionChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e, System.Boolean fixWindows) [0x0017e] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\TemplatedItemsList.cs:1002 03-08 11:33:12.568 I/MonoDroid(14705): at Xamarin.Forms.TemplatedItemsList 2 [TView,TItem] .OnProxyCollectionChanged   (System.Object sender,   System.Collections.Specialized.NotifyCollectionChangedEventArgs e)   [0x00000] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ TemplatedItemsList.cs:954   03-08 11:33:12.568 I / MonoDroid(14705):at   Xamarin.Forms.ListProxy.OnCollectionChanged   (System.Collections.Specialized.NotifyCollectionChangedEventArgs e)   [0x0000a] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ ListProxy.cs:230   03-08 11:33:12.568 I / MonoDroid(14705):at   Xamarin.Forms.ListProxy + LT;&GT; c__DisplayClass32_0.b__0   ()[0x00018] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ ListProxy.cs:203   03-08 11:33:12.568 I / MonoDroid(14705):at   Xamarin.Forms.ListProxy.OnCollectionChanged(System.Object sender,   System.Collections.Specialized.NotifyCollectionChangedEventArgs e)   [0x000a0] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ ListProxy.cs:222   03-08 11:33:12.568 I / MonoDroid(14705):at   Xamarin.Forms.ListProxy + WeakNotifyProxy.OnCollectionChanged   (System.Object sender,   System.Collections.Specialized.NotifyCollectionChangedEventArgs e)   [0x00031] in   C:\ BuildAgent3 \工作\ ca3766cfc22354a1 \ Xamarin.Forms.Core \ ListProxy.cs:386   03-08 11:33:12.568 I / MonoDroid(14705):at   System.Collections.ObjectModel.ObservableCollection 1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00012] in /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:288 03-08 11:33:12.568 I/MonoDroid(14705): at System.Collections.ObjectModel.ObservableCollection 1 [T] .OnCollectionChanged   (System.Collections.Specialized.NotifyCollectionChangedAction动作,   System.Object项,System.Int32索引)[0x00000] in   /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:351   03-08 11:33:12.568 I / MonoDroid(14705):at   System.Collections.ObjectModel.ObservableCollection 1[T].InsertItem (System.Int32 index, T item) [0x00024] in /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/System/compmod/system/collections/objectmodel/observablecollection.cs:219 03-08 11:33:12.568 I/MonoDroid(14705): at System.Collections.ObjectModel.Collection 1 [T] .Add(T item)[0x00023]   在   /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/mscorlib/system/collections/objectmodel/collection.cs:67   03-08 11:33:12.568 I / MonoDroid(14705):at   ParkAndRidePrague.MainPage + d__7.MoveNext()[0x00217]   在   E:\开发\ JakubHolovskyGit \ ParkAndRidePrague \ ParkAndRidePrague \ ParkAndRidePrague \ MainPage.xaml.cs中:71   03-08 11:33:12.568 I / MonoDroid(14705):---堆栈跟踪结束   抛出异常的先前位置--- 03-08 11:33:12.568   I / MonoDroid(14705):at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   [0x0000c] in   /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143   03-08 11:33:12.568 I / MonoDroid(14705):at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess   (System.Threading.Tasks.Task任务)[0x00047] in   /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:187   03-08 11:33:12.568 I / MonoDroid(14705):at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification   (System.Threading.Tasks.Task任务)[0x0002e] in   /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156   03-08 11:33:12.568 I / MonoDroid(14705):at   System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd   (System.Threading.Tasks.Task任务)[0x0000b] in   /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:128   03-08 11:33:12.568 I / MonoDroid(14705):at   System.Runtime.CompilerServices.TaskAwaiter.GetResult()[0x00000] in   /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:113   03-08 11:33:12.568 I / MonoDroid(14705):at   ParkAndRidePrague.MainPage + d__4.MoveNext()[0x0002c] in   E:\开发\ JakubHolovskyGit \ ParkAndRidePrague \ ParkAndRidePrague \ ParkAndRidePrague \ MainPage.xaml.cs中:36   03-08 11:33:12.569 I / MonoDroid(14705):---堆栈跟踪结束   抛出异常的先前位置--- 03-08 11:33:12.569   I / MonoDroid(14705):at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()   [0x0000c] in   /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143   03-08 11:33:12.569 I / MonoDroid(14705):at   System.Runtime.CompilerServices.AsyncMethodBuilderCore.m__0   (System.Object state)[0x00000] in   /Users/builder/data/lanes/4009/f3074d2c/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018   03-08 11:33:12.569 I / MonoDroid(14705):at   Android.App.SyncContext + c__AnonStorey0。&lt;&gt; m__0()[0x00000] in   /Users/builder/data/lanes/4009/9578cdcd/source/xamarin-android/src/Mono.Android/Android.App/SyncContext.cs:35   03-08 11:33:12.569 I / MonoDroid(14705):at   Java.Lang.Thread + RunnableImplementor.Run()[0x0000b] in   /Users/builder/data/lanes/4009/9578cdcd/source/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:36   03-08 11:33:12.569 I / MonoDroid(14705):at   Java.Lang.IRunnableInvoker.n_Run(System.IntPtr jnienv,System.IntPtr   native__this)[0x00009] in   /Users/builder/data/lanes/4009/9578cdcd/source/monodroid/src/Mono.Android/platforms/android-25/src/generated/Java.Lang.IRunnable.cs:81   03-08 11:33:12.569 I / MonoDroid(14705):at(包装动态方法)   System.Object:dad7aebb-b573-4058-885d-12720b7c63dd(intptr,intptr)

1 个答案:

答案 0 :(得分:0)

我在XAML中遇到了一些错误。主要是我没有使用 StackLayout.Style Style 标签。 TargetType =“{x:Type StackLayout}”也不见了。

以下是更正后的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:local="clr-namespace:ParkAndRidePrague"
             xmlns:my="clr-namespace:ParkAndRidePrague.Core.Enums;assembly=ParkAndRidePrague.Core"
             x:Class="ParkAndRidePrague.MainPage">
    <ContentPage.Content>
        <ListView  x:Name="listViewParkings">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout BackgroundColor="#eee"
                        Orientation="Vertical">
                            <StackLayout Orientation="Horizontal">
                                <Label Margin="10, 5, 0, 5" VerticalTextAlignment="Center" Text="{Binding Name}" TextColor="Black" FontSize="20" />
                                <Label Margin="0, 5, 10, 5" VerticalTextAlignment="Center" Text="{Binding NumOfFreePlaces}" HorizontalOptions="EndAndExpand" TextColor="Black" FontSize="20" />
                                <StackLayout.Style>
                                    <Style TargetType="{x:Type StackLayout}">
                                        <Style.Triggers>
                                            <DataTrigger TargetType="{x:Type StackLayout}" Binding="{Binding Path=ParkingAvailability}" Value="{x:Static my:ParkingAvailability.Low}">
                                                <Setter Property="BackgroundColor" Value="Red" />
                                            </DataTrigger>
                                            <DataTrigger TargetType="{x:Type StackLayout}" Binding="{Binding Path=ParkingAvailability}" Value="{x:Static my:ParkingAvailability.Medium}">
                                                <Setter Property="BackgroundColor" Value="Orange" />
                                            </DataTrigger>
                                            <DataTrigger TargetType="{x:Type StackLayout}" Binding="{Binding Path=ParkingAvailability}" Value="{x:Static my:ParkingAvailability.High}">
                                                <Setter Property="BackgroundColor" Value="Green" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </StackLayout.Style>
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage.Content>
</ContentPage>