【问题标题】:Why does setting a style for StackLayout result in a NullReferenceException?为什么为 StackLayout 设置样式会导致 NullReferenceException?
【发布时间】:2017-04-13 06:41:15
【问题描述】:

我希望 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)

这可能是什么原因造成的,我该如何解决?作为记录,如果我指定内联边距,例如&lt;Entry margin="0,0,0,20"/&gt;,效果很好。

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    我认为你的语法是错误的。不应该是这样吗?

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-19
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-05
      • 2013-06-16
      相关资源
      最近更新 更多