Brainf_ckSharp icon indicating copy to clipboard operation
Brainf_ckSharp copied to clipboard

Double Check Microsoft.UI.Xaml Replaces Windows.UI.Xaml

Open LanceMcCarthy opened this issue 4 years ago • 16 comments

When right-clicking on the Textbox, the application crashes. The following output is available in the Windows Event Viewer

Event Viewer

Faulting application name: Brainf_ckSharp.Uwp.exe, version: 1.0.0.0, time stamp: 0x5ee155af
Faulting module name: Windows.UI.Xaml.dll, version: 10.0.18362.815, time stamp: 0x9fa806f2
Exception code: 0xc000027b
Fault offset: 0x0000000000713530
Faulting process id: 0x61ec
Faulting application start time: 0x01d63f71d635f57a
Faulting application path: C:\Program Files\WindowsApps\77cee5e8-8481-4e69-92bc-31cfb2a6bc2b_1.610.1.0_x64__4tcmghg6akv1r\Brainf_ckSharp.Uwp.exe
Faulting module path: C:\Windows\System32\Windows.UI.Xaml.dll
Report Id: 22e98e81-c491-492c-9b5c-e0806edd6751
Faulting package full name: 77cee5e8-8481-4e69-92bc-31cfb2a6bc2b_1.610.1.0_x64__4tcmghg6akv1r
Faulting package-relative application ID: App

Sergio has shared a screenshot (from DevCenter?) that further implicates the issue is in Windows.UI.Xaml in the crash

image

I'm surprised that Windows.UI.Xaml is being used instead of Microsoft.UI.Xaml, but I'm not well versed in how the WinUI sets the types under the covers. This could well be the expected output for a crash on a WinUI ContentPresenter.

LanceMcCarthy avatar Jun 10 '20 22:06 LanceMcCarthy

Hey Lance, just so that I understand, how did you manage to get past the initial startup crash in Release mode? I mean, if you got to repro the crash with the context menu in Release mode I take it you found a way to avoid that initial crash somehow? 🤔

Sergio0694 avatar Jun 10 '20 22:06 Sergio0694

I have not been able to see the initial crash, but I did fix this crash.

The controls library was not merging the resources, update Themes/Generic.xaml to:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <ResourceDictionary.MergedDictionaries>
        <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
        <ResourceDictionary Source="Brainf_ckEditBox/Brainf_ckEditBox.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

LanceMcCarthy avatar Jun 10 '20 22:06 LanceMcCarthy

Also, app.xaml in the main project should be merging the Themes dictionary:

<Application
    x:Class="Brainf_ckSharp.Uwp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    RequestedTheme="Dark">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls"/>
                <ResourceDictionary Source="Resources/Dictionaries/Brushes.xaml"/>
                <ResourceDictionary Source="Resources/Dictionaries/IconPaths.xaml"/>
                <ResourceDictionary Source="Resources/Dictionaries/MDL2Icons.xaml"/>
                <ResourceDictionary Source="Styles/ButtonsStyles.xaml"/>
                <ResourceDictionary Source="Styles/ClassicStyles.xaml"/>
                <ResourceDictionary Source="Styles/MiscStyles.xaml"/>
                <ResourceDictionary Source="Styles/TextStyles.xaml"/>
                <ResourceDictionary Source="Brainf_ckSharp.Uwp.Controls.Ide/Themes/Generic.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

LanceMcCarthy avatar Jun 10 '20 22:06 LanceMcCarthy

Nevermind about that last one, when I merge themes in App.xaml, it now crashes at launch

LanceMcCarthy avatar Jun 10 '20 22:06 LanceMcCarthy

Oh, so I was merging the resource dictionaries wrong in the class library! 🤦‍♂️

If the startup crash you're getting is this:

Exception "The text associated with this error code could not be found. Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source' because the type 'Windows.Foundation.String' cannot be assigned to the type 'Windows.Foundation.Uri'. [Line: 21 Position: 37]"} System.Exception {Windows.UI.Xaml.Markup.XamlParseException}

Apparently this is fixed by leaving ms-app:/// as the URI scheme for the resources in the class library. Looks like for some reason it can't parse the URI if you don't use this format 🤔

Which is weird, because apparently the app can do that just fine instead 🤷‍♂️

Sergio0694 avatar Jun 10 '20 22:06 Sergio0694

I have two side questions:

  1. Why was the crash only exhibiting with .NET Native, and specifically in Release mode?
  2. Since I now have those WinUI XamlControlsResources dictionaries in both the app and the class library, should I be worried that those styles would be instantiated twice? Or would the XAML parser be able to recognize them and only create those styles once to save resources? In case that matters at all, that is, not sure how merged resources work exactly in this scenario 😅

Sergio0694 avatar Jun 10 '20 22:06 Sergio0694

Ah, let me try that again for the Editbox styles (thanks Resharper).

About the resources, you can't always rely on the downstream library consumer. You would think that having a style in App.xaml would make it available for everything, no matter where it comes from, but the external project is not just some XAML files that get compiled into the main project.

Rather, the library gets compiled first (only aware of Windows.UI.Xaml), while the main project is compiling for Microsoft.UI.Xaml.

I'm just making educated guesses at this point, based on the nature of the exceptions and the fact that it's .NET Native compilation stripping out the unreferenced parts.

LanceMcCarthy avatar Jun 10 '20 22:06 LanceMcCarthy

Launch is fixed, but the context menu exception is still happening. Not sure why it stopped for a while, needing to compile with WinUI types is a red herring.

LanceMcCarthy avatar Jun 10 '20 22:06 LanceMcCarthy

Just to double check, have you see my message in the other issue here? I mean, with "launch is fixed" you mean before or after that?

Sergio0694 avatar Jun 10 '20 22:06 Sergio0694

Launch has always worked for me with Release builds. Installed msix after publishing as side-loadable package)

The only reason it stopped working was because of the Uri.

Here's the packaging setting: image

LanceMcCarthy avatar Jun 10 '20 22:06 LanceMcCarthy

I see. This is weird. It makes me think there's this crash in the context menu on one end which we can investigate, and the one I'm seeing at startup which might be an SDK bug instead?

I mean, you and I are running the same exact code, and for me the app is just exploding at startup, the only difference I can see is that I'm on 19041. Which would also explain why my other app Legere is exhibiting the same exact behavior - it explodes at startup in Release as well 🤦‍♂️

Sergio0694 avatar Jun 10 '20 23:06 Sergio0694

@LanceMcCarthy By the way, we have a ton of WinUI devs in our UWP Discord server, if you wanted to join we could talk there as well! We'd be happy to have you 😊

Right now there's Steven Brix (from the WinUI team) that's asking about this issue and I'm just relaying info from here, you could talk to him directly in there if you wanted.

Sergio0694 avatar Jun 10 '20 23:06 Sergio0694

I'm going to take another stab at this in the morning, have to sign off. I'll have a 19041 VM spun up and will be able to hopefully be a little more helpful.

[update] just got your latest message halfway through typing, lol.

The two crashes probably do have two separate causes, but these kinds of things happen at the same time tend to make me suspect larger issue.

LanceMcCarthy avatar Jun 10 '20 23:06 LanceMcCarthy

Sounds great, that's awesome! No worries, I'll be looking forward for an update once you have the 19041 VM setup then, that'll definitely be useful! Thanks again 😊

And yeah I definitely agree with you that all these weird issues at once are very suspicious.

Sergio0694 avatar Jun 10 '20 23:06 Sergio0694

I'm so tired that I completely missing ITemsPresenter and ContentPresenter, lol.

in IdeView.xaml change:

    <!--IDE-->
    <ide:Brainf_ckIde x:Name="CodeEditor">
        <ide:Brainf_ckIde.ContextMenuSecondaryContent>
            <ItemsControl
                ItemsSource="{x:Bind constants:CodeSnippets.All}"
                ItemTemplate="{x:Bind CodeSnippetTemplate}">
                <ItemsControl.ItemContainerStyle>
                    <Style TargetType="ItemsPresenter">
                        <Setter Property="HorizontalAlignment" Value="Stretch"/>
                    </Style>
                </ItemsControl.ItemContainerStyle>
            </ItemsControl>
        </ide:Brainf_ckIde.ContextMenuSecondaryContent>
    </ide:Brainf_ckIde>
</UserControl>

To

    <!--IDE-->
    <ide:Brainf_ckIde x:Name="CodeEditor">
        <ide:Brainf_ckIde.ContextMenuSecondaryContent>
            <ItemsControl
                ItemsSource="{x:Bind constants:CodeSnippets.All}"
                ItemTemplate="{x:Bind CodeSnippetTemplate}">
                <ItemsControl.ItemContainerStyle>
                    <Style TargetType="ContentPresenter">
                        <Setter Property="HorizontalAlignment" Value="Stretch"/>
                    </Style>
                </ItemsControl.ItemContainerStyle>
            </ItemsControl>
        </ide:Brainf_ckIde.ContextMenuSecondaryContent>
    </ide:Brainf_ckIde>
</UserControl>

LanceMcCarthy avatar Jun 10 '20 23:06 LanceMcCarthy

I feel so stupid, I mixed those two up 🤣

Thank you so much for spotting this! Commented in the other issue 👍

Sergio0694 avatar Jun 11 '20 00:06 Sergio0694

Closing this as I think the issue is fixed now (can right click and it works just fine). Thank you again for the help! 😄

Sergio0694 avatar Jan 06 '23 19:01 Sergio0694