Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release build of sample app fails when startup page replaced with simple NavigationPage (Android) #7214

Closed
hbraasch opened this issue May 16, 2022 · 8 comments
Labels
area-navigation NavigationPage platform/android 🤖 s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working

Comments

@hbraasch
Copy link

Description

When generating a new MAUI app from scratch, the Android [Release] build version fails to run. This happens if the startup page is changed from:
MainPage = new AppShell();
to:
MainPage = new NavigationPage(new StartupPage()) { BarBackgroundColor = Colors.Grey, BarTextColor = Colors.White};
The [StartupPage.cs] is extremely basic and should not be the problem.
When building as [Debug] both the unmodied and modified apps works fine.
When building as [Release], the unmodified app works fine, the modified one fails.
A reproduction project is supplied:
https://github.com/hbraasch/PublishTester.git

All works fine under iOS.

Steps to Reproduce

  1. Load the reproduction app into VS.

  2. Ensure inside [App.xaml.cs], the modification is commented out:
    image

  3. Create a release build and run the app. Observe it runs fine.

  4. Ensure the modification is uncommented:

image

  1. Create a debug build and run the app. Observe it runs fine.
  2. Create a release build and run the app. Observe it starts to load, shows a purple splash screen, then abruptly closes.

Version with bug

Release Candidate 3 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 12 - API 31, Android 11 = API 30

Did you find any workaround?

None

Relevant log output

No response

@hbraasch hbraasch added s/needs-verification Indicates that this issue needs initial verification before further triage will happen t/bug Something isn't working labels May 16, 2022
@pijnappel
Copy link

pijnappel commented May 16, 2022

Can confirm, happens in my app too. I added some error handling. Here is the stack trace of my app:

Microsoft.Maui.Platform.ToPlatformException: Microsoft.Maui.Handlers.NavigationViewHandler found for Microsoft.Maui.Controls.NavigationPage is incompatible
 ---> System.InvalidProgramException: Invalid IL code in Microsoft.Maui.Handlers.NavigationViewHandler:CreatePlatformView (): IL_002a: ldnull    


   at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IStackNavigationView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Android.Views.View, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].OnCreatePlatformView()
   at Microsoft.Maui.Handlers.ViewHandler.OnCreatePlatformElement()
   at Microsoft.Maui.Handlers.ElementHandler.CreatePlatformElement()
   at Microsoft.Maui.Handlers.ElementHandler.SetVirtualView(IElement view)
   at Microsoft.Maui.Handlers.ViewHandler.SetVirtualView(IElement element)
   at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IStackNavigationView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Android.Views.View, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].SetVirtualView(IView view)
   at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IStackNavigationView, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Android.Views.View, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].SetVirtualView(IElement view)
   at Microsoft.Maui.Controls.Element.SetHandler(IElementHandler newHandler)
   at Microsoft.Maui.Controls.Element.set_Handler(IElementHandler value)
   at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IElement.set_Handler(IElementHandler value)
   at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
   Exception_EndOfInnerExceptionStack
   at Microsoft.Maui.Platform.ElementExtensions.ToHandler(IElement view, IMauiContext context)
   at Microsoft.Maui.Platform.ElementExtensions.ToPlatform(IElement view, IMauiContext context)
   at Microsoft.Maui.Platform.ContainerView.SetView(IElement view, Boolean forceRefresh)
   at Microsoft.Maui.Platform.ContainerView.set_CurrentView(IElement value)
   at Microsoft.Maui.Platform.ElementExtensions.ToContainerView(IElement view, IMauiContext context)
   at Microsoft.Maui.Platform.NavigationRootManager.Connect(IView view, IMauiContext mauiContext)
   at Microsoft.Maui.Handlers.WindowHandler.MapContent(IWindowHandler handler, IWindow window)
   at Microsoft.Maui.PropertyMapper`2.<>c__DisplayClass5_0[[Microsoft.Maui.IWindow, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Microsoft.Maui.Handlers.IWindowHandler, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<Add>b__0(IElementHandler h, IElement v)
   at Microsoft.Maui.PropertyMapper.UpdatePropertyCore(String key, IElementHandler viewHandler, IElement virtualView)
   at Microsoft.Maui.PropertyMapper.UpdateProperty(IElementHandler viewHandler, IElement virtualView, String property)
   at Microsoft.Maui.Handlers.ElementHandler.UpdateValue(String property)
   at Microsoft.Maui.Controls.Window.OnPropertyChanged(String propertyName)
   at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent)
   at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes)
   at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value, Boolean fromStyle, Boolean checkAccess)
   at Microsoft.Maui.Controls.BindableObject.SetValue(BindableProperty property, Object value)
   at Microsoft.Maui.Controls.Window.set_Page(Page value)
   at Microsoft.Maui.Controls.Application.set_MainPage(Page value)

@hbraasch
Copy link
Author

Great, thanks for checking.

@kristinx0211 kristinx0211 added s/verified Verified / Reproducible Issue ready for Engineering Triage and removed s/needs-verification Indicates that this issue needs initial verification before further triage will happen labels May 16, 2022
@XamlTest XamlTest added s/triaged Issue has been reviewed and removed s/triaged Issue has been reviewed labels May 16, 2022
@kristinx0211
Copy link

verified repro on android 11 using the above project with vs 17.3.0 Preview 2.0 [32515.5.main].

@Eilon Eilon added the area-navigation NavigationPage label May 16, 2022
@sgellock
Copy link

@kristinx0211 is there a workaround while waiting on an update?

@Michal-MK
Copy link

GA makes past the splash screen, but when I try to call PopAsync() it does not do anything... I have not checked logcat output yet due to time constratints... but maybe it has something to say.

@hbraasch
Copy link
Author

I just installed the latest release of Visual Studio [17.3.0 Preview 1.1] this morning, and it appears the issue demonstrated in my repro project [https://github.com/hbraasch/PublishTester.git] has been resolved!
The app now runs under [Release] build. I also checked it by running a more real-world app under[Release], and it also now runs successfully.
In my opinion, my issue has been resolved. Well done and thanks!
There are other developers also interested in this issue, so I suggest we leave this issue open until they have reported on ther experience. If only up to me, I would close this issue.

@Michal-MK
Copy link

Michal-MK commented May 24, 2022

Alright after an unholy amount of time spent trying to find the root cause I have found it along with a workaround... so here it goes.

Lets have a ContentPage

<?xml version="1.0" encoding="utf-8" ?>
<base:ContentPageBase xmlns:base="clr-namespace:ERecept.MAUI.Views.Base"
                      xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                      xmlns:vm="clr-namespace:ERecept.Core.ViewModel;assembly=ERecept.Core"
                      xmlns:uc="clr-namespace:ERecept.MAUI.Views.UserControls"
                      x:Class="ERecept.MAUI.Views.Pages.UserInfoPage"
                      x:DataType="vm:UserInfoPageVM">
    <Grid RowDefinitions="auto, *, auto">
        <uc:BackNav/>
        <uc:UserDetails Margin="8" BindingContext="{Binding UserDetails}" Grid.Row="1"/>
        <Button Grid.Row="2" Text="Log Out" Command="{Binding LogOutCommand}"
                BackgroundColor="{StaticResource DestructiveActionColor}"
                Margin="16,0,16,32"/>
    </Grid>
</base:ContentPageBase>

The base:ContentPageBase is just a wrapper that sets my BindingContext, not important. Note the x:DataType.

<uc:BackNav/>:

<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:vm="clr-namespace:ERecept.Core.ViewModel.Base;assembly=ERecept.Core"
             x:Class="ERecept.MAUI.Views.UserControls.BackNav"
             x:DataType="vm:MedicamentDetailVM">
    <Grid ColumnDefinitions="auto, *" RowDefinitions="auto" BackgroundColor="{StaticResource PrimaryColor}" Padding="8">
        <ImageButton Source="previous.png" Command="{Binding BackCommand}" HeightRequest="40" WidthRequest="40" Aspect="AspectFit"/>
        <Label Grid.Column="1" TextColor="White" VerticalTextAlignment="Center" FontSize="20"
               Text="{Binding NavText}"
               Padding="16,0,0,0"/>
    </Grid>
</ContentView>

Again here note the x:DataType.

The two classes are different but inherit from a common class ParametrizedBaseViewModel<T> Which defines the BackCommand property.

public class ParametrizedBaseViewModel<T> : INotifyPropertyChanged {
    public ICommand BackCommand { get; set; }
}

Why is the x:DataType different?

  • I assume that x:DataType is just a hint about what type my BindingContext will be so that I get IntelliSense in xaml... correct me if I am wrong. So I just put in any ViewModel I found since all contain the same property and XAML does not support generics afaik.

Now this does not work in Release Configuration, but runs fine in Debug Configuration.

When I change the x:DataType to the same type as in the parent ContentPage the navigation works in Release as well, but only for that specific ContentPage.

The workaround

Create an interface for the base class:

public interface IRedundantInterface {
	ICommand BackCommand { get; set; }
}

Make the ParametrizedBaseViewModel<T> implement IRedundantInterface and change the x:DataType in BackNav to that interface type.

Why?

The Binding to the BackCommand property does not happen in Release without the workaround. Why? The type specified contains the property of the same name. Is x:DataType doing something else than just type hints? Why do other bindings work => the NavText on Label?

@mattleibow
Copy link
Member

Just tested with the latest version of MAUI and this no longer happens. I think this happened around the time Google changed the ABI of some of the controls and broke things:

System.InvalidProgramException: Invalid IL code in Microsoft.Maui.Handlers.NavigationViewHandler:CreatePlatformView ():IL_002a: ldnull

@mattleibow mattleibow closed this as not planned Won't fix, can't repro, duplicate, stale Aug 30, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Sep 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-navigation NavigationPage platform/android 🤖 s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants