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

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

Open
DerMoonbreaker opened this issue Mar 18, 2024 · 2 comments

Comments

@DerMoonbreaker
Copy link

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

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
Copy link
Author

DerMoonbreaker commented Apr 5, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants