2017-04-13 49 views
1

我希望我的StackLayout中的每个Entry类型都有余量。我想如下指定样式:为什么设置StackLayout的样式会导致NullReferenceException?

<StackLayout IsVisible="{Binding IsLoggingIn, Converter={StaticResource BooleanConverter}}"> 
     <StackLayout.Resources> 
      <Style TargetType="{x:Type Entry}"> 
       <Setter Property="Margin" Value="0,0,0,20"/> 
      </Style> 
     </StackLayout.Resources> 

     <Entry ... /> 
     <Entry ... /> 

    </StackLayout> 

不幸的是,它会导致一个NullReferenceException:

04-13 16:26:15.406 E/mono (17167): Unhandled Exception: 
04-13 16:26:15.406 E/mono (17167): System.NullReferenceException: Object reference not set to an instance of an object. 
04-13 16:26:15.406 E/mono-rt (17167): [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object. 

随着多一点堆栈跟踪以供参考:

Unhandled Exception: 
System.NullReferenceException: Object reference not set to an instance of an object. 
    at MyApp.Views.Login.InitializeComponent() [0x00012] in C:\dev\PathTo\MyApp\obj\Debug\MyApp.Views.Login.xaml.g.cs:22 
    at MyApp.Views.Login..ctor() [0x00008] in C:\dev\PathTo\MyApp\Views\Login.xaml.cs:9 
    at MyApp.App.SetupLoginPage() [0x00001] in C:\dev\PathTo\MyApp\App.xaml.cs:94 
    at MyApp.App+<OnStart>d__4.MoveNext() [0x001ad] in C:\dev\PathTo\MyApp\App.xaml.cs:60 
--- End of stack trace from previous location where exception was thrown --- 
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() [0x0000c] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143 
    at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (System.Object state) [0x00000] in /Users/builder/data/lanes/4009/3a62f1ea/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1018 
    at Android.App.SyncContext+<Post>c__AnonStorey0.<>m__0() [0x00000] in /Users/builder/data/lanes/4009/3a62f1ea/source/xamarin-android/src/Mono.Android/Android.App/SyncContext.cs:35 
    at Java.Lang.Thread+RunnableImplementor.Run() [0x0000b] in /Users/builder/data/lanes/4009/3a62f1ea/source/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:36 
    at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00009] in /Users/builder/data/lanes/4009/3a62f1ea/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Java.Lang.IRunnable.cs:81 
    at (wrapper dynamic-method) System.Object:d83972de-0492-4d07-a14d-24725ac1bfd1 (intptr,intptr) 

什么可能会导致这种情况,我该如何解决?为了记录,如果我指定了内联边距,例如<Entry margin="0,0,0,20"/>,它工作正常。

回答

1

我认为你的语法是错误的。难道不是这个;

<StackLayout.Resources> 
    <ResourceDictionary> 
     <Style TargetType="Entry"> 
      <Setter Property="Margin" Value="0,0,0,20"/> 
     </Style> 
    </ResourceDictionary> 
</StackLayout.Resources> 

注意我如何改变TargetType并添加ResourceDictionary标签周围。

相关问题