自定义控件的{x 44}资源样式

时间:2016-05-04 18:33:45

标签: xaml styles custom-controls xamarin.forms resourcedictionary

我有一个源自TextEntry的自定义控件Edit。我想为它应用一种样式,但是,自定义xaml命名空间和TargetType不是很好。以下是一段摘录,展示了我尝试解决TargetType问题的风格和不同方式。

资源字典 - 摘录

<?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:controls="clr-namespace:GRES.App.Forms.CustomControls;assembly=GRES.App.Forms"
             x:Class="GRES.App.Forms.CustomControls.GresPage">
  <ContentPage.Resources>
    <ResourceDictionary>
      <!--Entry Styles-->
      <Style x:Key="DefaultEntry" TargetType="{controls:TextEntry}">
      <!--styles-->
      </Style>
    </ResourceDictionary>
  </ContentPage.Resources>
</ContentPage>

尝试一个TargetType="{controls:TextEntry}" 结果: System.ArgumentException: Object of type 'GRES.App.Forms.CustomControls.TextEntry' cannot be converted to type 'System.Type'.

尝试两个TargetType="{x:Type controls:TextEntry}" 结果: System.InvalidOperationException: Sequence contains more than one matching element

尝试三个TargetType="controls:TextEntry" 结果: System.InvalidOperationException: Sequence contains more than one matching element

StackTrace - TestCase 2 (TestCase 3产生相同的堆栈跟踪)

05-04 22:13:46.541 I/MonoDroid(22866): UNHANDLED EXCEPTION:
05-04 22:13:46.541 I/MonoDroid(22866): System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Sequence contains more than one matching element
05-04 22:13:46.541 I/MonoDroid(22866):   at System.Linq.Enumerable.SingleOrDefault[TSource] (IEnumerable`1 source, System.Func`2 predicate) [0x00098] in /Users/builder/data/lanes/2970/46c3f7e0/source/mono/external/referencesource/System.Core/System/Linq/Enumerable.cs:1105 
05-04 22:13:46.541 I/MonoDroid(22866):   at Xamarin.Forms.ReflectionExtensions.GetField (System.Type type, System.Func`2 predicate) [0x00006] in <filename unknown>:0 
05-04 22:13:46.541 I/MonoDroid(22866):   at Xamarin.Forms.BindablePropertyConverter.ConvertFrom (System.Type type, System.String propertyName, IXmlLineInfo lineinfo) [0x00017] in <filename unknown>:0 
05-04 22:13:46.541 I/MonoDroid(22866):   at Xamarin.Forms.BindablePropertyConverter.Xamarin.Forms.IExtendedTypeConverter.ConvertFrom (System.Globalization.CultureInfo culture, System.Object value, IServiceProvider serviceProvider) [0x0013e] in <filename unknown>:0 
05-04 22:13:46.541 I/MonoDroid(22866):   at Xamarin.Forms.Xaml.TypeConversionExtensions.ConvertTo (System.Object value, System.Type toType, System.Func`1 getConverter, IServiceProvider serviceProvider) [0x0003d] in <filename unknown>:0 
05-04 22:13:46.541 I/MonoDroid(22866):   at Xamarin.Forms.Xaml.TypeConversionExtensions.ConvertTo (System.Object value, System.Type toType, System.Func`1 minfoRetriever, IServiceProvider serviceProvider) [0x00021] in <filename unknown>:0 
05-04 22:13:46.541 I/MonoDroid(22866):   at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.SetPropertyValue (System.Object xamlelement, XmlName propertyName, System.Object value, Xamarin.Forms.BindableObject rootElement, INode node, Xamarin.Forms.Xaml.HydratationContext context, IXmlLineInfo lineInfo) [0x00357] in <filename unknown>:0 
05-04 22:13:46.541 I/MonoDroid(22866):   at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.Visit (Xamarin.Forms.Xaml.ValueNode node, INode parentNode) [0x0005e] in <filename unknown>:0 
05-04 22:13:46.541 I/MonoDroid(22866):   at Xamarin.Forms.Xaml.ValueNode.Accept (IXamlNodeVisitor visitor, INode parentNode) [0x00000] in <filename unknown>:0 
05-04 22:13:46.541 I/MonoDroid(22866):   at Xamarin.Forms.Xaml.ElementNode.Accept (IXamlNodeVisitor visitor, INode parentNode) [0x00054] in <filename unknown>:0 
05-04 22:13:46.541 I/MonoDroid(22866):   at Xamarin.Forms.Xaml.ElementNode.Accept (IXamlNodeVisitor visitor, INode parentNode) [0x00089] in <filename unknown>:0 
05-04 22:13:46.541 I/MonoDroid(22866):   at Xamarin.Forms.Xaml.FillResourceDictionariesVisitor.Visit (Xamarin.Forms.Xaml.ElementNode node, INode parentNode) [0x00185] in <filename unknown>:0 
05-04 22:13:46.541 I/MonoDroid(22866):   at Xamarin.Forms.Xaml.ElementNode.Accept (IXamlNodeVisitor visitor, INode parentNode) [0x00008] in <filename unknown>:0 
05-04 22:13:46.541 I/MonoDroid(22866):   at Xamarin.Forms.Xaml.ElementNode.Accept (IXamlNodeVisitor visitor, INode parentNode) [0x00089] in <filename unknown>:0 
05-04 22:13:46.541 I/MonoDroid(22866):   at Xamarin.Forms.Xaml.RootNode.Accept (IXamlNodeVisitor visitor, INode parentNode) [0x0002f] in <filename unknown>:0 
05-04 22:13:46.541 I/MonoDroid(22866):   at Xamarin.Forms.Xaml.XamlLoader.Load (Xamarin.Forms.BindableObject view, System.String xaml) [0x000be] in <filename unknown>:0 
05-04 22:13:46.541 I/MonoDroid(22866):   at Xamarin.Forms.Xaml.XamlLoader.Load (Xamarin.Forms.BindableObject view, System.Type callingType) [0x0002e] in <filename unknown>:0 
05-04 22:13:46.541 I/MonoDroid(22866):   at Xamarin.Forms.Xaml.Extensions.LoadFromXaml[TView] (Xamarin.Forms.Xaml.TView view, System.Type callingType) [0x00000] in <filename unknown>:0 
05-04 22:13:46.541 I/MonoDroid(22866):   at GRES.App.Forms.CustomControls.GresPage.InitializeComponent () [0x00001] in c:\Git\Gres\GRES.Core\GRES.App.Forms\GRES.App.Forms\obj\Debug\GresPage.xaml.g.cs:21 
05-04 22:13:46.541 I/MonoDroid(22866):   at GRES.App.Forms.CustomControls.GresPage..ctor () [0x00008] in c:\Git\Gres\GRES.Core\GRES.App.Forms\GRES.App.Forms\CustomControls\GresPage.xaml.cs:18 
05-04 22:13:46.541 I/MonoDroid(22866):   at GRES.App.Forms.Pages.LoginPage..ctor () [0x00000] in c:\Git\Gres\GRES.Core\GRES.App.Forms\GRES.App.Forms\Pages\LoginPage.xaml.cs:8 
05-04 22:13:46.541 I/MonoDroid(22866):   at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
05-04 22:13:46.541 I/MonoDroid(22866):   at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in /Users/builder/data/lanes/2970/46c3f7e0/source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:644 
05-04 22:13:46.541 I/MonoDroid(22866):   --- End of inner exception stack trace ---

0 个答案:

没有答案