【问题标题】:UWP App compiled with .NET native tool chain crashes on startup due to custom style由于自定义样式,使用 .NET 本机工具链编译的 UWP 应用在启动时崩溃
【发布时间】:2020-05-16 11:36:18
【问题描述】:

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

更新:通过关闭“优化代码”并在发布模式下启用调试并从 VS 运行,我能够得到以下...。这让我看到了一个 [很棒的] 动画我使用的名为Xamanimation 的库仍然不确定我的UWP app.xaml 中的自定义样式与这个问题之间的联系,但是解决这个真正的问题当然解决了所有问题(即样式是红鲱鱼)。在下面添加了一个答案。

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

System.ArgumentException HResult=0x80070057 消息=值不 落在预期范围内。来源= StackTrace:StackTrace“在 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章 RuntimeTypeHandle __typeHnd, Int32 __targetIndex, Double arg0)\r\n
在 __Interop.ForwardComStubs.Stub_22[TThis](__ComObject __this, Double 值,Int32 __targetIndex)\r\n 在 Windows.UI.Xaml.IUIElement__Impl.Stubs.put_Opacity(__ComObject __this, 双值)\r\n 在 Windows.UI.Xaml.UIElement.put_Opacity(双 值)\r\n 在 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() 在 D:\a\1\s\Xamarin.Forms.Platform.UAP\VisualElementTracker.cs:line 236\r\n 在 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(对象发件人,TEventArgs e)\r\n 在 Xamarin.Forms.VisualElement.BatchCommit() 中 D:\a\1\s\Xamarin.Forms.Core\VisualElement.cs:line 572\r\n at Xamarin.Forms.AnimationExtensions.HandleTweenerUpdated(对象 o, EventArgs 参数)在 D:\a\1\s\Xamarin.Forms.Core\AnimationExtensions.cs:第 274 行\r\n
在 System.EventHandler.InvokeOpenStaticThunk(对象发送者,EventArgs e)\r\n 在 System.EventHandler.Invoke(Object sender, EventArgs e)\r\n 在 Xamarin.Forms.Tweener.b__22_0(Int64 step) 中 D:\a\1\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
at Xamarin.Forms.Internals.Ticker.SendSignals(Int32 timestep) in D:\\a\\1\\s\\Xamarin.Forms.Core\\Internals\\Ticker.cs:line 125\r\n
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.EventHandler
1.Invoke(Object sender, TEventArgs e)\r\n at __Interop.Intrinsics.HasThisCall__51(对象 __this,IntPtr pfn,对象 arg0,对象 arg1)\r\n 在 __Interop.ReverseComStubs.Stub_41(对象 __this, Void* unsafe_sender, Void* unsafe_args, IntPtr __methodPtr)" 字符串

        <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>

【问题讨论】:

  • ,根据您的描述,我无法重现您的问题,所以您能否在github提供一个简单的可以重现您的问题,我会下载您的示例进行测试。

标签: c# xaml xamarin.forms uwp uwp-xaml


【解决方案1】:

显然,这个使用 Xamanimation 库为我的 ContentPage Appearing 事件中的某些项目添加动画的小技巧不适用于 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 编译的版本中没有问题。但是启用它后,应用程序就会崩溃。

解决方法很简单,我只是直接将 Target 上的 Opacity 属性设置为 0,然后去掉第一行 (&lt;xamanimation:FadeToAnimation Opacity="0" Duration="0" /&gt;) 并转为 - 一切都很好。

【讨论】:

    猜你喜欢
    • 2018-08-13
    • 2021-02-02
    • 2017-09-18
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 2016-04-17
    • 1970-01-01
    相关资源
    最近更新 更多