Xamarin.Forms 自定义控件中抛出序列异常
Sequence Exception thrown in Xamarin.Forms custom control
我有一个非常基本的跨平台应用程序。该解决方案包含用于应用程序注入的 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 控件的构造函数,但在 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\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\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\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\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\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\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\s\Xamarin.Forms.Xaml\XamlLoader.cs:141
at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.String xaml) [0x0004b] in C:\agent\_work\s\Xamarin.Forms.Xaml\XamlLoader.cs:89
at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.Type callingType) [0x0002f] in C:\agent\_work\s\Xamarin.Forms.Xaml\XamlLoader.cs:68
at Xamarin.Forms.Xaml.Extensions.LoadFromXaml[TXaml] (TXaml view, System.Type callingType) [0x00000] in C:\agent\_work\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
The code successfully hits the ctor for the Wizard control, but falls through and fails in Android's MainActivity with a Sequence exception, the stack trace below.
I have been debugging this for the last 4 hours and cannot see why it's failing on the given stack.
你得到这个异常是因为你定义了一个 public 属性(DataItem) 与不同的 Xaml 属性 名称 (DataContextProperty
),这是不允许。
要解决此问题,您需要将 DataContextProperty
更改为 DataItemProperty
。
我有一个非常基本的跨平台应用程序。该解决方案包含用于应用程序注入的 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 控件的构造函数,但在 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\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\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\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\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\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\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\s\Xamarin.Forms.Xaml\XamlLoader.cs:141
at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.String xaml) [0x0004b] in C:\agent\_work\s\Xamarin.Forms.Xaml\XamlLoader.cs:89
at Xamarin.Forms.Xaml.XamlLoader.Load (System.Object view, System.Type callingType) [0x0002f] in C:\agent\_work\s\Xamarin.Forms.Xaml\XamlLoader.cs:68
at Xamarin.Forms.Xaml.Extensions.LoadFromXaml[TXaml] (TXaml view, System.Type callingType) [0x00000] in C:\agent\_work\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
The code successfully hits the ctor for the Wizard control, but falls through and fails in Android's MainActivity with a Sequence exception, the stack trace below.
I have been debugging this for the last 4 hours and cannot see why it's failing on the given stack.
你得到这个异常是因为你定义了一个 public 属性(DataItem) 与不同的 Xaml 属性 名称 (DataContextProperty
),这是不允许。
要解决此问题,您需要将 DataContextProperty
更改为 DataItemProperty
。