Called multiple times

Eduardo Gomez Romero 1,235 Reputation points
2025-01-14T16:59:23.9566667+00:00

Minimal code example

AppShellViewModel

public partial class AppShellViewModel
{
    private readonly IConnectivity _connectivity;

    public AppShellViewModel(IConnectivity connectivity, NoInternetPopUp noInternetPopUp)
    {
    
        _connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;
    }


    private void Connectivity_ConnectivityChanged(object? sender, ConnectivityChangedEventArgs e)
    {
        Debug.WriteLine(_connectivity.NetworkAccess);

        HandleConnectivityChangeAsync();
    }

    private async void HandleConnectivityChangeAsync()
    {
        if (_connectivity.NetworkAccess == NetworkAccess.Internet)
        {
            _noInternetPopUp.IsOpen = false;
            try
            {
                if (TurbinePins.Count == 0 && !isInitializing)
                {
                    isInitializing = true;
                    await _turbineService.InitializeAsync();
                    isInitializing = false;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Initialization failed: {ex.Message}");
                isInitializing = false;
            }
        }
        else
        {
            TurbineService_NoInternet();
        }
    }


How this works

when the application is running and the internet gets cut off, a popup appears, and when the internet gets back on, the popup disappears

Posible problem

I just realize that I have 2 view Models, that inherit from the appshellviewmodel

TurbinesCollectionPageViewModel

    public partial class TurbinesCollectionPageViewModel(
        ITurbineService turbineService,
        IAppService appService,
        IServiceProvider serviceProvider,
        ICommandHandler commandHandler,
        IConnectivity connectivity,
        NoInternetPopUp noInternetPopUp):
        AppShellViewModel(turbineService, appService, serviceProvider,
            commandHandler, connectivity, noInternetPopUp)               

and changestationviewModel

    public partial class ChargingStationsMapPageViewModel: AppShellViewModel
    {
        public SfPopup? MapDialogPopUp;
        public Map? MapView;
        [ObservableProperty]
        bool isExpanded;
        public ChargingStationsMapPageViewModel(
            ITurbineService turbineService, IAppService
            appService, IServiceProvider serviceProvider,
            NoInternetPopUp noInternetPopUp,
            ICommandHandler commandHandler, IConnectivity connectivity)
            : base(turbineService, appService, serviceProvider, commandHandler, connectivity, noInternetPopUp)
        {
            MapDialogButtons();
        }


--------------------UPDATE--------------------------------------------------------

I created a static bool

 static bool _isConnectivityEventRegistered;



     if (!_isConnectivityEventRegistered)
     {
         _connectivity.ConnectivityChanged += Connectivity_ConnectivityChanged;
         _isConnectivityEventRegistered = true;
     }

and now is getting called twice

I wanted it to get called once per change

here is the app, if you please could take a quick look

https://github.com/eduardoagr/METROWIND/tree/MetroWind-1-2

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,879 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.