janusw / Camera.Maui

A CameraView Control for .NET Maui
MIT License
17 stars 3 forks source link

System.ObjectDisposedException: 'Cannot access a closed Stream.' when showing a ContentPage with cv:BarcodeImage a second time #18

Open DerMoonbreaker opened 3 months ago

DerMoonbreaker commented 3 months ago

Hello

a View should show a QR-Code for transfering some informations to an other device

the first await Shell.Current.GoToAsync(nameof(SettingsView), true); works perfect

the second call after going back to the MainView with await Shell.Current.GoToAsync(".."); ends in a System.ObjectDisposedException: 'Cannot access a closed Stream.'

After removing the cv:BarcodeImage no problems comes up.

Changing Settings from Singelton to Transitions do not change the behaviour

here some Codesnippets out of the Project

MauiProgram.cs

            builder.Services.AddSingleton<MainViewModel>();
            builder.Services.AddSingleton<MainView>();

            builder.Services.AddSingleton<SettingsViewModel>();
            builder.Services.AddSingleton<SettingsView>();

MainViewModel.cs

[RelayCommand]
public async Task GoToSettings()
{
        await Shell.Current.GoToAsync(nameof(SettingsView), true);
}

SettingsView.xaml

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:cv="clr-namespace:Camera.MAUI;assembly=Camera.MAUI"
             x:Class="AnteRoomAppointmentsApp.Views.SettingsView"
             xmlns:models="clr-namespace:VirtualWaitLib.Models;assembly=VirtualWaitLib"
             xmlns:viewmodel="clr-namespace:AnteRoomAppointmentsApp.ViewModels"
             x:DataType="viewmodel:SettingsViewModel"
             Title="SettingsView">
    <VerticalStackLayout>
        <Grid RowDefinitions="*">
            <Grid RowDefinitions="50,500,30,50,*" ColumnDefinitions="*,*">

<!-- some other stuff ->

                <cv:BarcodeImage Grid.Row="1" Grid.Column="0" x:Name="barcodeImage" Aspect="AspectFit"
                    WidthRequest="400" HeightRequest="400" 
                    BarcodeWidth="380" BarcodeHeight="380" BarcodeMargin="5"
                    BarcodeBackground="White" BarcodeForeground="Black"
                    BarcodeFormat="QR_CODE" Barcode="{Binding BarCode}" /> 
            </Grid>
        </Grid>

    </VerticalStackLayout>
</ContentPage>

SettingsViewModel.cs

  [ObservableProperty]
  string barCode = string.Empty;

 public SettingsViewModel(VwzService vwzService)
 {
     _vwzService = vwzService;
     Title = "Einstellungen";
     //initialize data
    Barcode tempBarCode = new Barcode();

     tempBarCode.Id = "a Gui Id saved in SecureStore"; 
     tempBarCode.Text = "some text";
     tempBarCode.Name = "a Name";

    BarCode = JsonConvert.SerializeObject(tempBarCode);
 }

  [RelayCommand]
  async Task GoToPreviousView()
  {
      await Shell.Current.GoToAsync(".."); // back to MainPage
  }

I hope this was all relevant stuff does anyone have an idear maybe I'm doing some wrong and I hope my english is not to bad

LBoullosa commented 3 months ago

I guess it´s related because registering views as singleton when navigating to another location the view was disposed. To understand what is happening I recommend you adding some OnAppearing, OnDisapearing events to know the life cycle. This is nothing related to BarcodeImage. If Transient doesn´t change the behaviour is because it´s accessing in some way the destroyed instance (the old one), that´s the reason why it seems doesn´t change. Try to compare the instances.

DerMoonbreaker commented 3 months ago

Hello LBoullosa

Thank you for your reply. I try to add your hints. one information about "This is nothing related to BarcodeImage": disabling die barcode on the view eliminate the error. All other elements on the view do not have such a problem. but let me first try whats happend when I add your hints.

best regards from germany