为什么为StackLayout设置样式会导致NullReferenceException?

时间:2017-04-13 06:41:16

标签: xamarin xamarin.forms

我希望我的StackLayout中的每个Entry类型都有一个保证金。我正在尝试指定如下样式:

    <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 个答案:

答案 0 :(得分:1)

我认为你的语法错了。不应该这样;

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

请注意我是如何更改TargetType并在其周围添加ResourceDictionary标记的。