使用FreshEssentials BindablePicker时出现无效的强制转换异常

我在其他项目中使用了FreshEssentials BindablePicker,但是对于特定的Page / PageModel我遇到了一个非常奇怪的问题 .

我已将Page和PageModel剥离到以下内容:Page:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="Manager.Pages.Config.RemoteConnectionsPage"
             xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:fe="clr-namespace:FreshEssentials;assembly=FreshEssentials">
    <ContentPage.Content>
        <StackLayout Orientation="Vertical">
            <Label HorizontalOptions="CenterAndExpand" Text="Main Page" />
            <fe:BindablePicker DisplayProperty="Name" ItemsSource="{Binding Environments}" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

PageModel:

using System.Collections.Generic;
using FreshMvvm;
using PropertyChanged;

namespace Manager.PageModels.Config
{
    [ImplementPropertyChanged]
    public class RemoteConnectionsPageModel : FreshBasePageModel
    {
        public enum Environment
        {
            Live,
            Test,
            Third
        }

        public class EnvironmentPickerItem
        {
            public string Name {
                get {
                    return Env.ToString ();
                }
            }

            public Environment Env {
                get;
                set;
            }
        }
        public RemoteConnectionsPageModel ()
        {
            List<EnvironmentPickerItem> _Environments;
            _Environments = new List<EnvironmentPickerItem> ();
            _Environments.Add (new EnvironmentPickerItem () { Env = Environment.Live });
            _Environments.Add (new EnvironmentPickerItem () { Env = Environment.Test });
            _Environments.Add (new EnvironmentPickerItem () { Env = Environment.Third });
            this.Environments = _Environments;
        }

        public List<EnvironmentPickerItem> Environments {
            get;
            set;
        }
    }
}

问题是当我运行应用程序时,我得到以下异常(完整堆栈跟踪被截断):

System.InvalidCastException:指定的强制转换无效 . at FreshEssentials.Droid.BindablePickerRendererDroid.OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs1 [TElement] e)[0x00031]在C:\ Users \ Michael \ Documents \ FreshEssentials \ src \ Droid \ Renderers \ BindablePickerRendererDroid.cs:46 at Xamarin.Forms.Platform.Android.VisualElementRenderer1 [TElement] .SetElement(TElement element)[0x000f4]位于C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementRenderer.cs:190 at Xamarin.Forms.Platform .Android.VisualElementRenderer`1 [TElement] .Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement(Xamarin.Forms.VisualElement element)[0x00027]在C:\ BuildAgent3 \ work \ ca3766cfc22354a1 \ Xamarin.Forms.Platform.Android \ VisualElementRenderer.cs:131

从FreshEssentials存储库查看BindablePickerRendererDroid的源代码,它似乎是一行:

((ObservableCollection<string>)e.NewElement.Items).CollectionChanged += RowsCollectionChanged;

在这种方法中 .

protected override void OnElementChanged(ElementChangedEventArgs<BindablePicker> e)
        {
            if (e.OldElement != null)
                ((ObservableCollection<string>)e.OldElement.Items).CollectionChanged -= RowsCollectionChanged;

            if (e.NewElement != null)
            {
                ((ObservableCollection<string>)e.NewElement.Items).CollectionChanged += RowsCollectionChanged;
                if (Control == null)
                {
                    var button = new AButton(Context) { Focusable = false, Clickable = true, Tag = this, Text = e.NewElement.Title };
                    button.SetOnClickListener(PickerListener.Instance);
                    SetNativeControl(button);
                }
                UpdatePicker();
            }

            base.OnElementChanged(e);
        }

奇怪的是,将上述确切代码纳入一个全新的项目工作正常 . 我检查了FreshEssentials和FreshMVVM的版本,它们在工作和非工作项目之间是相同的 .

有人遇到这个,和/或任何想法?

完整的Xamarin Studio和其他版本信息如下:

=== Xamarin工作室社区===版本6.1.4(版本1)安装UUID:462e6ba8-9cb7-4b87-acc8-9dfb0ab2c6f7运行时:Mono 4.6.2(mono-4.6.0-branch / ac9e222)(64- bit)GTK 2.24.23(Raleigh主题)包版本:406020016 === NuGet ===版本:3.4.3.0 === Xamarin.Profiler ===版本:0.33.2位置:/ Applications / Xamarin Profiler.app/目录/ MacOS / Xamarin Profiler === Xamarin.Android ===版本:7.0.2.42(Xamarin工作室社区)Android SDK:/ Users / jameslavery / Library / Developer / Xamarin / android-sdk-macosx支持的Android版本:2.3( API级别10)4.0.3(API级别15)4.1(API级别16)4.3(API级别18)4.4(API级别19)5.0(API级别21)5.1(API级别22)6.0(API级别23)SDK工具版本:24.4.1 SDK平台工具版本:23.1 SDK构建工具版本:23.0.1 Java SDK:/ usr java version“1.7.0_71”Java(TM)SE运行时环境(版本1.7.0_71-b14)Java HotSpot(TM) )64位服务器VM(内置24.71-b01,混合模式)Android Designer EPL代码可在此处获取:https: //github.com/xamarin/AndroidDesigner.EPL === Xamarin Android Player ===版本:0.6.5位置:/ Applications / Xamarin Android Player.app === Apple Developer Tools === Xcode 8.0(11246)Build 8A218a === Xamarin.iOS ===版本:10.3.1.8(Xamarin工作室社区)哈希:7beaef4分支:cycle8-xi Build 日期:2016-12-20 02:58:14-0500 === Xamarin.Mac = ==版本:2.10.0.120(Xamarin工作室社区)===构建信息===版本号:601040001 Git版本:e606823f2dd01b4552216c013b597a73bec2068f构建日期:2017-01-10 17:28:57-05 Xamarin插件:c92d0626d347aaa02839689eaac2961d24c9f446构建通道: monodevelop-lion-cycle8 ===操作系统=== Mac OS X 10.11.6 Darwin Jamess-MacBook-Pro.local 15.6.0 Darwin内核版本15.6.0 Thu Jun 23 18:25:34 PDT 2016 root:xnu- 3248.60.10~1 / RELEASE_X86_64 x86_64 ===启用用户安装的插件=== StyleCop支持1.0.1.9 Gorilla播放器0.9.1.2 XamlStyler 1.0.2

回答(0)