maui icon indicating copy to clipboard operation
maui copied to clipboard

Android app works in Debug but crashes in Release.

Open KevboSlice opened this issue 2 years ago • 11 comments

Description

when i run my app in debug everything works but when i change to release mode it crashes when you try to change the ObservableCollection. There must be an issue when it binds the ObservableCollection to the CollectionView control. I am at a loss as to why this is happening. I tried deploying it to my android phone and I get the same results as the emulator.

I am using Visual Studio 2022 Version 17.4.0 Preview 1.0 Pixel 5 Android Emulator 13 API 33

Steps to Reproduce

Download the sample app at https://github.com/KevboSlice/BindingProblem run the app in debug and the list will populate just fine. change to release mode and run it. It will crash.

Link to public reproduction project repository

https://github.com/KevboSlice/BindingProblem

Version with bug

7.0 Release Candidate 1

Last version that worked well

Unknown/Other

Affected platforms

Android, I was not able test on other platforms

Affected platform versions

android 13 API 33

Did you find any workaround?

none

Relevant log output

No response

KevboSlice avatar Nov 21 '22 20:11 KevboSlice

Hie, isn't it an issue with the Linker that is removing some packages that your application needs when you are not in debug? Check this resolved issue on https://stackoverflow.com/questions/72598734/entity-framework-core-in-net-maui-xamarin-forms

tanakamawere avatar Nov 22 '22 16:11 tanakamawere

Hi @KevboSlice. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar Nov 22 '22 19:11 ghost

@KevboSlice can you include a logcat? https://learn.microsoft.com/en-us/xamarin/android/deploy-test/debugging/android-debug-log?tabs=windows

PureWeen avatar Nov 22 '22 19:11 PureWeen

@KevboSlice can you include a logcat? https://learn.microsoft.com/en-us/xamarin/android/deploy-test/debugging/android-debug-log?tabs=windows

Note from @mattleibow Moved this to a gist so the comment list is easier to read.

Here is the entire log...

https://gist.github.com/mattleibow/0734b8cafa9f4904c7a53ada555cbfc4

Snippet that may be related:

 at Android.Views.ViewGroup.Layout(Int32 , Int32 , Int32 , Int32 )
   at Microsoft.Maui.ViewHandlerExtensions.PlatformArrangeHandler(IViewHandler viewHandler, Rect frame)
   at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.Controls.ReorderableItemsView, Microsoft.Maui.Controls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[AndroidX.RecyclerView.Widget.RecyclerView, Xamarin.AndroidX.RecyclerView, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].PlatformArrange(Rect frame)
   at Microsoft.Maui.Controls.VisualElement.ArrangeOverride(Rect bounds)
   at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IView.Arrange(Rect bounds)
   at Microsoft.Maui.Controls.Compatibility.Layout.LayoutChildIntoBoundingRegion(VisualElement child, Rect region)
   at Microsoft.Maui.Controls.TemplatedView.LayoutChildren(Double x, Double y, Double width, Double height)
   at Microsoft.Maui.Controls.Compatibility.Layout.UpdateChildrenLayout()
   at Microsoft.Maui.Controls.Compatibility.Layout.OnSizeAllocated(Double width, Double height)
   at Microsoft.Maui.Controls.VisualElement.SizeAllocated(Double width, Double height)
   at Microsoft.Maui.Controls.VisualElement.UpdateBoundsComponents(Rect bounds)
   at Microsoft.Maui.Controls.VisualElement.set_Frame(Rect value)
   at Microsoft.Maui.Controls.TemplatedView.ArrangeOverride(Rect bounds)
   at Microsoft.Maui.Controls.VisualElement.Microsoft.Maui.IView.Arrange(Rect bounds)
   at Microsoft.Maui.Controls.Compatibility.Layout.LayoutChildIntoBoundingRegion(VisualElement child, Rect region)
   at Microsoft.Maui.Controls.Page.LayoutChildren(Double x, Double y, Double width, Double height)
   at Microsoft.Maui.Controls.Page.UpdateChildrenLayout()
   at Microsoft.Maui.Controls.Page.OnSizeAllocated(Double width, Double height)
   at Microsoft.Maui.Controls.VisualElement.SizeAllocated(Double width, Double height)
   at Microsoft.Maui.Controls.VisualElement.UpdateBoundsComponents(Rect bounds)
   at Microsoft.Maui.Controls.VisualElement.set_Frame(Rect value)
   at Microsoft.Maui.Controls.ContentPage.Microsoft.Maui.IContentView.CrossPlatformArrange(Rect bounds)

KevboSlice avatar Nov 23 '22 14:11 KevboSlice

I had the same problem on Release and I solved it by simplifying the xaml code.

I also tried your code and saw that the project works by removing "Frame".

So from this:

    <RefreshView
      Grid.Row="0"
      Command="{Binding GetEmployeesCommand}"
      IsRefreshing="{Binding IsRefreshing}">
        <CollectionView
         ItemsSource="{Binding EmployeeList}"
         SelectionMode="None">
            <CollectionView.ItemTemplate>
                <DataTemplate x:DataType="model:EmployeeModel">
                    <Grid Padding="5">
                        <Frame Style="{StaticResource CardView}">
                        <VerticalStackLayout>
                            <Grid ColumnDefinitions="*,Auto" RowDefinitions="*,*,*">
                                <Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding Name}" Style="{DynamicResource BlackBrush}" FontSize="16" FontAttributes="Bold" />
                                <Label Grid.Row="1" Grid.Column="0" Text="{Binding Department}" Style="{DynamicResource Gray300Brush}" FontSize="12" />
                                <Label Grid.Row="1" Grid.Column="1" Text="{Binding Title}" Style="{DynamicResource Gray300Brush}" FontSize="12" HorizontalTextAlignment="End" />
                            </Grid>
                        </VerticalStackLayout>
                        </Frame>
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </RefreshView>

To this:

    <RefreshView
      Grid.Row="0"
      Command="{Binding GetEmployeesCommand}"
      IsRefreshing="{Binding IsRefreshing}">
        <CollectionView
         ItemsSource="{Binding EmployeeList}"
         SelectionMode="None">
            <CollectionView.ItemTemplate>
                <DataTemplate x:DataType="model:EmployeeModel">
                    <Grid Padding="5">
                        
                        <VerticalStackLayout>
                            <Grid ColumnDefinitions="*,Auto" RowDefinitions="*,*,*">
                                <Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding Name}" Style="{DynamicResource BlackBrush}" FontSize="16" FontAttributes="Bold" />
                                <Label Grid.Row="1" Grid.Column="0" Text="{Binding Department}" Style="{DynamicResource Gray300Brush}" FontSize="12" />
                                <Label Grid.Row="1" Grid.Column="1" Text="{Binding Title}" Style="{DynamicResource Gray300Brush}" FontSize="12" HorizontalTextAlignment="End" />
                            </Grid>
                        </VerticalStackLayout>
                       
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </RefreshView>

I'm just started learning Maui so i have no clue on why this is happening. Hope this may help.

D3bo avatar Nov 24 '22 15:11 D3bo

D3bo - you are right. By removing the frame everything works. Unfortunately I need the frame, I have a GestureRecognizer attached to the frame so i can tap it and pull up the details page. This is sort of a necessary element in the display. Hopefully someone can come up with a fix for this.

KevboSlice avatar Nov 24 '22 17:11 KevboSlice

Yeah, I was in your exact situation. I have replaced the gesture GestureRecognizer and replaced with some (horrible) code-behind like this: XAML

<CollectionView ItemsSource="{Binding Items}"
                                SelectionMode="Single"
                        VerticalOptions="Fill"
                          SelectionChanged="CollectionView_SelectionChanged">
            <CollectionView.ItemTemplate>
                <DataTemplate x:DataType="models:CustomersProfession">

                    <Grid Padding="5" ColumnDefinitions="*,*,100,100" >
                        <Label Text="{Binding Profession}" Grid.Column="0"/>
                        <Label Text="{Binding categoriaFidelity}" Grid.Column="1"/>
                        <CheckBox IsChecked="{Binding AvailableFoConsumers}" Grid.Column="2" IsEnabled="False"/>
                        <CheckBox IsChecked="{Binding AvailableForCompanies}" Grid.Column="3"  IsEnabled="False"/>
                    </Grid>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

CODE-BEHIND

private async void CollectionView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.CurrentSelection.FirstOrDefault() is not CustomersProfession item)
            return;

        await Shell.Current.GoToAsync(nameof(ProfessionDetailPage), true, new Dictionary<string, object>
            {
                { "Profession", item }
               
            });
    }

D3bo avatar Nov 24 '22 17:11 D3bo

The gist has things about layouts, and removing frame seems to work...

@KevboSlice can you try with a <Border> instead of a <Frame>? I wonder if this is some issue in the legacy layouts coming up...

mattleibow avatar Nov 24 '22 19:11 mattleibow

Hi @KevboSlice. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost avatar Nov 24 '22 19:11 ghost

The gist has things about layouts, and removing frame seems to work...

@KevboSlice can you try with a <Border> instead of a <Frame>? I wonder if this is some issue in the legacy layouts coming up...

@mattleibow I just tried and I can confirm that with a <Border> instead of a <Frame> it works.

D3bo avatar Nov 25 '22 02:11 D3bo

🤦 @mattleibow @KevboSlice Nope I think I got it now... It's the Style="{StaticResource CardView}" on the <Frame> that make the app crash. Keeping the frame and removing the style it works.

I just look up my code and i see that It crash becouse i have a style on a Button Style="{StaticResource ButtonOutline}". With the style the app crash every single time, without it works just fine.

We both learned from @jamesmontemagno examples but didn't copied all the styles..

Still... why it work on debug?

D3bo avatar Nov 25 '22 04:11 D3bo

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Nov 29 '22 19:11 ghost

Might be related to https://github.com/dotnet/maui/issues/11857 where XamlC is not setting all the correct properties from the resources.

Can you try in debug mode with <_MauiForceXamlCForDebug>true</_MauiForceXamlCForDebug> somewhere in your csproj?

mattleibow avatar Dec 08 '22 11:12 mattleibow

Probably related, because with <_MauiForceXamlCForDebug>true</_MauiForceXamlCForDebug> in debug mode it throw this exception:

Microsoft.Maui.Controls.Xaml.XamlParseException: 'Position 18:32. StaticResource not found for key CardView'

D3bo avatar Dec 09 '22 05:12 D3bo

Verified this issue with Visual Studio 17.7.0 Preview 1.0. Can repro on android platform with sample project. BindingProblem-master.zip 11534

Zhanglirong-Winnie avatar Jun 01 '23 06:06 Zhanglirong-Winnie