由于自定义样式,使用 .NET 本机工具链编译的 UWP 应用程序在启动时崩溃

UWP App compiled with .NET native tool chain crashes on startup due to custom style

我已经确认,在 Xamarin Forms UWP 项目的 App.xaml ResourceDictionary 中仅存在(即使未使用)以下样式会导致 UWP 应用程序在编译时启动时崩溃在发布模式下启用 .NET 本机工具链(x64,但在这种情况下不认为这很重要)。在没有启用 .NET 本机工具链的情况下工作 100% 正常,但真的很想保留这种自定义样式并获得本机编译的更快的应用程序的好处。有谁知道为什么它会崩溃应用程序?除了我在 Windows 事件查看器中看到的内容之外,我什至无法弄清楚如何获得更详细的错误消息,它只是说 Windows.UI.Xaml.dll 是错误模块名称。

更新: 通过关闭 "optimize code" 并在 Release 模式下启用 DEBUG 并从 VS 启用 运行,我能够得到以下内容...... . 这让我找到了一个我正在使用的名为 Xamanimation 的 [很棒的] 动画库...仍然不确定我的 UWP app.xaml 中的自定义样式和这个问题之间的 link 和这个问题,但是修复这个真正的问题当然解决了一切(即风格是一条红鲱鱼)。在下面添加了一个答案。

System.Private.Interop.dll!System.Runtime.InteropServices.McgMarshal.ThrowOnExternalCallFailed(int hr, System.RuntimeTypeHandle typeHnd) Line 1189 at f:\dd\ndp\fxcore\CoreRT\src\System.Private.Interop\src\Shared\McgMarshal.cs(1189)

System.ArgumentException HResult=0x80070057 Message=Value does not fall within the expected range. Source= StackTrace: StackTrace " at System.Runtime.InteropServices.McgMarshal.ThrowOnExternalCallFailed(Int32 hr, RuntimeTypeHandle typeHnd) in f:\dd\ndp\fxcore\CoreRT\src\System.Private.Interop\src\Shared\McgMarshal.cs:line 1189\r\n at __Interop.ComCallHelpers.Call(__ComObject __this, RuntimeTypeHandle __typeHnd, Int32 __targetIndex, Double arg0)\r\n
at __Interop.ForwardComStubs.Stub_22[TThis](__ComObject __this, Double value, Int32 __targetIndex)\r\n at Windows.UI.Xaml.IUIElement__Impl.Stubs.put_Opacity(__ComObject __this, Double value)\r\n at Windows.UI.Xaml.UIElement.put_Opacity(Double value)\r\n at Xamarin.Forms.Platform.UWP.VisualElementTracker2.UpdateOpacity(VisualElement view, FrameworkElement frameworkElement) in D:\a\1\s\Xamarin.Forms.Platform.UAP\VisualElementTracker.cs:line 518\r\n at Xamarin.Forms.Platform.UWP.VisualElementTracker2.UpdateNativeControl() in D:\a\s\Xamarin.Forms.Platform.UAP\VisualElementTracker.cs:line 236\r\n at Xamarin.Forms.Platform.UWP.VisualElementTracker2.OnRedrawNeeded(Object sender, EventArgs e) in D:\a\1\s\Xamarin.Forms.Platform.UAP\VisualElementTracker.cs:line 409\r\n at System.EventHandler1.Invoke(Object sender, TEventArgs e)\r\n at Xamarin.Forms.VisualElement.BatchCommit() in D:\a\s\Xamarin.Forms.Core\VisualElement.cs:line 572\r\n at Xamarin.Forms.AnimationExtensions.HandleTweenerUpdated(Object o, EventArgs args) in D:\a\s\Xamarin.Forms.Core\AnimationExtensions.cs:line 274\r\n
at System.EventHandler.InvokeOpenStaticThunk(Object sender, EventArgs e)\r\n at System.EventHandler.Invoke(Object sender, EventArgs e)\r\n at Xamarin.Forms.Tweener.b__22_0(Int64 step) in D:\a\s\Xamarin.Forms.Core\Tweener.cs:line 92\r\n at System.Func2.Invoke(T arg)\r\n at Xamarin.Forms.Internals.Ticker.SendSignals(Int64 step) in D:\a\1\s\Xamarin.Forms.Core\Internals\Ticker.cs:line 136\r\n<br> at Xamarin.Forms.Internals.Ticker.SendSignals(Int32 timestep) in D:\a\1\s\Xamarin.Forms.Core\Internals\Ticker.cs:line 125\r\n<br> at Xamarin.Forms.Platform.UWP.WindowsTicker.RenderingFrameEventHandler(Object sender, Object args) in D:\a\1\s\Xamarin.Forms.Platform.UAP\WindowsTicker.cs:line 25\r\n at System.EventHandler1.Invoke(Object sender, TEventArgs e)\r\n at __Interop.Intrinsics.HasThisCall__51(Object __this, IntPtr pfn, Object arg0, Object arg1)\r\n at __Interop.ReverseComStubs.Stub_41(Object __this, Void* unsafe_sender, Void* unsafe_args, IntPtr __methodPtr)" string

        <Style x:Key="FormTextBoxStyle" TargetType="uwp:FormsTextBox">
            <Setter Property="MinWidth" Value="{ThemeResource TextControlThemeMinWidth}" />
            <Setter Property="MinHeight" Value="{ThemeResource TextControlThemeMinHeight}" />
            <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
            <Setter Property="ForegroundFocusBrush" Value="{ThemeResource SystemControlForegroundChromeBlackHighBrush}" />
            <Setter Property="PlaceholderForegroundBrush" Value="{ThemeResource SystemControlPageTextBaseMediumBrush}" />
            <Setter Property="PlaceholderForegroundFocusBrush" Value="{ThemeResource SystemControlPageTextChromeBlackMediumLowBrush}" />
            <Setter Property="Background" Value="{ThemeResource SystemControlBackgroundAltHighBrush}" />
            <Setter Property="BackgroundFocusBrush" Value="{ThemeResource SystemControlBackgroundChromeWhiteBrush}" />
            <Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundChromeDisabledLowBrush}" />
            <Setter Property="SelectionHighlightColor" Value="{ThemeResource SystemControlHighlightAccentBrush}" />
            <Setter Property="BorderThickness" Value="0,0,0,2" />
            <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
            <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Hidden" />
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Hidden" />
            <Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="False" />
            <Setter Property="Padding" Value="0" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="uwp:FormsTextBox">
                        <Grid>
                            <Grid.Resources>

                                <Style x:Name="DeleteButtonStyle" TargetType="Button">
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="Button">
                                                <Grid
                                                    x:Name="ButtonLayoutGrid"
                                                    Background="{ThemeResource TextBoxButtonBackgroundThemeBrush}"
                                                    BorderBrush="{ThemeResource TextBoxButtonBorderThemeBrush}"
                                                    BorderThickness="{TemplateBinding BorderThickness}">
                                                    <TextBlock
                                                        x:Name="GlyphElement"
                                                        HorizontalAlignment="Center"
                                                        VerticalAlignment="Center"
                                                        AutomationProperties.AccessibilityView="Raw"
                                                        FontFamily="{ThemeResource SymbolThemeFontFamily}"
                                                        FontSize="12"
                                                        FontStyle="Normal"
                                                        Foreground="{TemplateBinding Foreground}"
                                                        Text="&#xE10A;" />
                                                    <VisualStateManager.VisualStateGroups>
                                                        <VisualStateGroup x:Name="CommonStates">
                                                            <VisualState x:Name="Normal" />
                                                            <VisualState x:Name="PointerOver">
                                                                <Storyboard>
                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="GlyphElement" Storyboard.TargetProperty="Foreground">
                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAccentBrush}" />
                                                                    </ObjectAnimationUsingKeyFrames>
                                                                </Storyboard>
                                                            </VisualState>
                                                            <VisualState x:Name="Pressed">
                                                                <Storyboard>
                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonLayoutGrid" Storyboard.TargetProperty="Background">
                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAccentBrush}" />
                                                                    </ObjectAnimationUsingKeyFrames>
                                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="GlyphElement" Storyboard.TargetProperty="Foreground">
                                                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltChromeWhiteBrush}" />
                                                                    </ObjectAnimationUsingKeyFrames>
                                                                </Storyboard>
                                                            </VisualState>
                                                            <VisualState x:Name="Disabled">
                                                                <Storyboard>
                                                                    <DoubleAnimation
                                                                        Storyboard.TargetName="ButtonLayoutGrid"
                                                                        Storyboard.TargetProperty="Opacity"
                                                                        To="0"
                                                                        Duration="0" />
                                                                </Storyboard>
                                                            </VisualState>
                                                        </VisualStateGroup>
                                                    </VisualStateManager.VisualStateGroups>
                                                </Grid>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>

                            </Grid.Resources>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Border
                                x:Name="BackgroundElement"
                                Grid.Row="1"
                                Grid.RowSpan="1"
                                Grid.ColumnSpan="2"
                                Margin="{TemplateBinding BorderThickness}"
                                Background="{TemplateBinding Background}" />
                            <Border
                                x:Name="BorderElement"
                                Grid.Row="1"
                                Grid.RowSpan="1"
                                Grid.Column="0"
                                Grid.ColumnSpan="2"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}" />
                            <ContentPresenter
                                x:Name="HeaderContentPresenter"
                                Grid.Row="0"
                                Grid.Column="0"
                                Grid.ColumnSpan="2"
                                VerticalAlignment="Bottom"
                                VerticalContentAlignment="Bottom"
                                x:DeferLoadStrategy="Lazy"
                                Content="{TemplateBinding Header}"
                                ContentTemplate="{TemplateBinding HeaderTemplate}"
                                FontWeight="Normal"
                                Foreground="{ThemeResource SystemControlForegroundBaseHighBrush}"
                                Visibility="Collapsed" />
                            <ScrollViewer
                                x:Name="ContentElement"
                                Grid.Row="0"
                                Grid.RowSpan="2"
                                Grid.Column="0"
                                Margin="{TemplateBinding BorderThickness}"
                                Padding="{TemplateBinding Padding}"
                                VerticalAlignment="Center"
                                AutomationProperties.AccessibilityView="Raw"
                                Foreground="{TemplateBinding ForegroundFocusBrush}"
                                HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                                HorizontalScrollMode="{TemplateBinding ScrollViewer.HorizontalScrollMode}"
                                IsDeferredScrollingEnabled="{TemplateBinding ScrollViewer.IsDeferredScrollingEnabled}"
                                IsHorizontalRailEnabled="{TemplateBinding ScrollViewer.IsHorizontalRailEnabled}"
                                IsTabStop="False"
                                IsVerticalRailEnabled="{TemplateBinding ScrollViewer.IsVerticalRailEnabled}"
                                VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
                                VerticalScrollMode="{TemplateBinding ScrollViewer.VerticalScrollMode}"
                                ZoomMode="Disabled" />
                            <ContentControl
                                x:Name="PlaceholderTextContentPresenter"
                                Grid.Row="0"
                                Grid.RowSpan="2"
                                Grid.Column="0"
                                Grid.ColumnSpan="2"
                                Margin="{TemplateBinding BorderThickness}"
                                Padding="{TemplateBinding Padding}"
                                HorizontalAlignment="Left"
                                VerticalAlignment="Center"
                                Content="{TemplateBinding PlaceholderText}"
                                Foreground="{TemplateBinding PlaceholderForegroundBrush}"
                                IsHitTestVisible="False"
                                IsTabStop="False" />

                            <Button
                                x:Name="DeleteButton"
                                Grid.Row="1"
                                Grid.Column="1"
                                MinWidth="34"
                                Margin="{ThemeResource HelperButtonThemePadding}"
                                VerticalAlignment="Stretch"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                FontSize="{TemplateBinding FontSize}"
                                Foreground="{TemplateBinding ForegroundFocusBrush}"
                                IsTabStop="False"
                                Style="{StaticResource DeleteButtonStyle}"
                                Visibility="Collapsed" />

                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="HeaderContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseLowBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundElement" Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledTransparentBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderElement" Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderElement" Storyboard.TargetProperty="BorderBrush">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseLowBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentElement" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledChromeDisabledLowBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PlaceholderTextContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledChromeDisabledLowBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Normal" />

                                    <VisualState x:Name="PointerOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundElement" Storyboard.TargetProperty="Opacity">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource TextControlBackgroundHoverOpacity}" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Focused">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PlaceholderTextContentPresenter" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{Binding PlaceholderForegroundFocusBrush, RelativeSource={RelativeSource TemplatedParent}}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BackgroundElement" Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{Binding BackgroundFocusBrush, RelativeSource={RelativeSource TemplatedParent}}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentElement" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{Binding ForegroundFocusBrush, RelativeSource={RelativeSource TemplatedParent}}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentElement" Storyboard.TargetProperty="RequestedTheme">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Light" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="ButtonStates">
                                    <VisualState x:Name="ButtonVisible">
                                        <Storyboard>

                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DeleteButton" Storyboard.TargetProperty="Visibility">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Visible</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>

                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="ButtonCollapsed" />
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

显然,这个使用 Xamanimation 库为我的 ContentPage Appearing 事件中的某些项目添加动画的小 hack 不适用于 FadeToAnimation(尽管它适用于 TranslateToAnimation):

    <xamanimation:StoryBoard x:Key="EntriesAnimation" Target="{x:Reference UserEntries}">
        <xamanimation:FadeToAnimation Opacity="0" Duration="0" />
        <xamanimation:FadeToAnimation Opacity="1" Duration="500" />
    </xamanimation:StoryBoard>

在调试模式下,或在禁用 .NET Native 编译的发行版中,没有这方面的问题。但是启用它后,它使应用程序崩溃。

解决方法很简单,我只是将目标上的不透明度 属性 直接设置为 0,然后去掉第一行 (<xamanimation:FadeToAnimation Opacity="0" Duration="0" />) 并转瞬即逝 - 一切都很好。