Xamarin.Forms自定义控件中引发的序列异常

贾德·埃里克(JadedEric)

我有一个非常基本的跨平台应用程序。该解决方案包含用于应用程序注入的PCL和一个Android应用程序。

我的MainPage.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:Crossplatform.app"
             xmlns:dragonfly="clr-namespace:Crossplatform.control;assembly=Crossplatform.control"
             x:Class="Crossplatform.app.MainPage">
    <ContentPage.Content>

        <dragonfly:Wizard DataItem="{Binding WizardData}" />

    </ContentPage.Content>
</ContentPage>

MainPage.xaml.cs

public partial class MainPage : ContentPage
{
    private string wizarddata;
    public string WizardData
    {
        get { return wizarddata; }
        set { wizarddata = value; }
    }

    public MainPage()
    {
        InitializeComponent();

        WizardData = "Hello from ViewModel";
    }
}

这一切都是在定义一个字符串属性,然后将其分配给自定义控件中的BindableProperty。

自定义控制代码:

public class Wizard : StackLayout
{
    public static readonly BindableProperty DataContextProperty =
        BindableProperty.Create(
            propertyName: nameof(DataItem),
            returnType: typeof(string),
            declaringType: typeof(Wizard),
            defaultValue: "Show Me",
            defaultBindingMode: BindingMode.TwoWay,
            validateValue: null,
            propertyChanged: OnDataContextPropertyChanged);

    public string DataItem
    {
        get
        {
            return (string)GetValue(DataContextProperty);
        }
        set
        {
            SetValue(DataContextProperty, value);
        }
    }

    public Wizard()
    {

    }

    static void OnDataContextPropertyChanged(BindableObject obj, object oldvalue, object newvalue)
    {
        ;
    }
}

这里没什么疯狂的。我正在做的就是定义一个名为DataContextProperty的可绑定属性,并使用该属性通知通过控件公开的DataItem属性的更改(根据上述main.xaml)。

该代码成功击中了Wizard控件的ctor,但由于出现Sequence异常(下面的堆栈跟踪)而失败并失败,导致Android的MainActivity失败。

我已经在最近4小时内调试了此程序,但看不到为什么它在给定的堆栈上失败。

任何帮助将不胜感激,因为我确定我在这里拥有的一切都是完全有效的。

堆栈跟踪:

  at System.Linq.Enumerable.First[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Func`2[T,TResult] predicate) [0x00011] in <b5bd9d990a0b4733885e90ca5ec6c0fb>:0 
  at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.TryAddToProperty (System.Object element, System.String localName, System.Object value, System.Xml.IXmlLineInfo lineInfo, Xamarin.Forms.Xaml.Internals.XamlServiceProvider serviceProvider, System.Exception& exception) [0x0005d] in C:\agent\_work\3\s\Xamarin.Forms.Xaml\ApplyPropertiesVisitor.cs:510 
  at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.SetPropertyValue (System.Object xamlelement, Xamarin.Forms.Xaml.XmlName propertyName, System.Object value, System.Object rootElement, Xamarin.Forms.Xaml.INode node, Xamarin.Forms.Xaml.HydratationContext context, System.Xml.IXmlLineInfo lineInfo) [0x000a2] in C:\agent\_work\3\s\Xamarin.Forms.Xaml\ApplyPropertiesVisitor.cs:334 
  at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.Visit (Xamarin.Forms.Xaml.ElementNode node, Xamarin.Forms.Xaml.INode parentNode) [0x00152] in C:\agent\_work\3\s\Xamarin.Forms.Xaml\ApplyPropertiesVisitor.cs:124 
  at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x000ab] in C:\agent\_work\3\s\Xamarin.Forms.Xaml\XamlNode.cs:149 
  at Xamarin.Forms.Xaml.ElementNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00043] in C:\agent\_work\3\s\Xamarin.Forms.Xaml\XamlNode.cs:143 
  at Xamarin.Forms.Xaml.RootNode.Accept (Xamarin.Forms.Xaml.IXamlNodeVisitor visitor, Xamarin.Forms.Xaml.INode parentNode) [0x00043] in C:\agent\_work\3\s\Xamarin.Forms.Xaml\XamlNode.cs:201 
  at Xamarin.Forms.Xaml.XamlLoader.Visit (Xamarin.Forms.Xaml.RootNode rootnode, Xamarin.Forms.Xaml.HydratationContext visitorContext) [0x0007b] in C:\agent\_work\3\s\Xamarin.Forms.Xaml\XamlLoader.cs:141 
  at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.String xaml) [0x0004b] in C:\agent\_work\3\s\Xamarin.Forms.Xaml\XamlLoader.cs:89 
  at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.Type callingType) [0x0002f] in C:\agent\_work\3\s\Xamarin.Forms.Xaml\XamlLoader.cs:68 
  at Xamarin.Forms.Xaml.Extensions.LoadFromXaml[TXaml] (TXaml view, System.Type callingType) [0x00000] in C:\agent\_work\3\s\Xamarin.Forms.Xaml\ViewExtensions.cs:36 
  at Crossplatform.app.MainPage.InitializeComponent () [0x00001] in C:\repos\Crossplatform.app\Crossplatform.app\Crossplatform.app\obj\Debug\Crossplatform.app.MainPage.xaml.g.cs:19 
  at Crossplatform.app.MainPage..ctor () [0x00008] in C:\repos\Crossplatform.app\Crossplatform.app\Crossplatform.app\MainPage.xaml.cs:21 
  at Crossplatform.app.App..ctor () [0x0000f] in C:\repos\Crossplatform.app\Crossplatform.app\Crossplatform.app\App.xaml.cs:16 
  at Crossplatform.app.Droid.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x00028] in C:\repos\Crossplatform.app\Crossplatform.app\Crossplatform.app.Android\MainActivity.cs:25 
厦门猫王-MSFT

该代码成功击中了Wizard控件的ctor,但由于出现Sequence异常(下面的堆栈跟踪)而失败并失败,导致Android的MainActivity失败。

我已经在最近4小时内调试了此程序,但看不到为什么它在给定的堆栈上失败。

之所以会出现此异常,是因为您使用不同的Xaml属性名称(DataContextProperty定义了一个公共属性(DataItem ),这是不允许的。

要解决此问题,您需要更改DataContextPropertyDataItemProperty

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Xamarin.Forms中自定义进度栏外观

在Xamarin.Forms中绑定自定义视图

Xamarin Forms XAML-创建没有UI的自定义控件?

xamarin.forms中我的自定义控件的HeightRequest的自定义BindableProperty

Xamarin Forms Framework中的自定义控件导致Null引用异常

Xamarin IOS自定义控件属性不显示

Xamarin.forms-自定义ViewCell中的BoxView

Xamarin Forms自定义步进器

Xamarin Forms自定义控件数据绑定问题

Xamarin.Forms中的自定义嵌套ListView

自定义控件中的Xamarin.Forms绑定集合

Xamarin.Forms:颜色资源不适用于自定义控件的颜色属性

Xamarin Forms中的自定义控件不起作用

[Xamarin.Forms] ListView中的自定义控件

ReactiveUI中的Xamarin.Forms控件是否需要自定义绑定?

自定义Xamarin.Forms布局

自定义控件的Xamarin数据绑定值评估为Xamarin.Forms.Binding

Xamarin Forms 自定义控件和可绑定属性无法按预期工作

在 xamarin 中创建自定义 iOS 控件

在 Xamarin.forms 中自定义导航栏

自定义控件中的 Xamarin BindableProperty

xamarin 中的自定义控件示例?

如何在我自己的 ContentView(自定义控件)中使用 Xamarin.Forms.Setter 类?

Xamarin.Forms 绑定不适用于嵌套的自定义控件

如何在 Xamarin 中创建自定义控件?

在 Xamarin.forms 中自定义框架的角半径

Xamarin Forms - 获取自定义控件运行时(使用 valueconverter?)

在 Xamarin Forms 中绑定自定义条目

如何在 Xamarin.Forms 中访问自定义控件的 BindingContext