navigation errors

Eduardo Gomez Romero 1,175 Reputation points
2024-11-25T21:16:53.8533333+00:00

I have my services

 public ObservableCollection<TurbinePin> TurbinePins { get; set; } = [];
  public TurbinesService(FirestoreService firestoreService, BlobServiceClient blobServiceClient)
  {
      _firestoreService = firestoreService;
      _blobServiceClient = blobServiceClient;
      InitializeAsync();
  }
  private async void InitializeAsync()
  {
      await _firestoreService.InitializeFirestoreAsync();
      _firestoreDb = _firestoreService.GetFirestoreDb();
      if (_firestoreDb != null)
      {
          await LoadOrInitializeTurbineAsync();
          //InitializeTimer();
      }
  }
  async Task LoadOrInitializeTurbineAsync()
  {
      var turbinesRef = _firestoreDb!.Collection(collectionName);
      var snapshot = await turbinesRef.GetSnapshotAsync();
      if (snapshot.Count == 0)
      {
          var turbine = new Turbine
          {
              Id = "EC-G-SB",
              Country = "Ecuador",
              Name = "Estación Ciudadela Simón Bolívar",
              Address = "Av. de las Américas, Guayaquil 090513, Ecuador",
              Latitude = -2.151993,
              Longitude = -79.886109,
              InstalationDateTime = new DateTime(
                  2024, 8, 2, 0, 0, 0, DateTimeKind.Utc),
              ImagesURLs = [],
          };
          turbine.RemovedCo2Kilograms = Math.Round(
              turbine.EnergyProduced * turbine.Co2EmissionOffset, 5);
          await AddTurbineImages(turbine);
          var turbineDocRef = turbinesRef.Document(turbine.Id);
          await turbineDocRef.SetAsync(turbine);
          AddToObservable(turbine);
      }
      else
      {
          foreach (var document in snapshot.Documents)
          {
              var turbine = document.ConvertTo<Turbine>();
              turbine.Id = document.Id;
              await AddTurbineImages(turbine);
              AddToObservable(turbine);
          }
      }
  }
  private async Task AddTurbineImages(Turbine turbine)
  {
      turbine.ImagesURLs!.Clear();
      var containerClient = _blobServiceClient.GetBlobContainerClient(turbine.Country!.ToLower());
      await foreach (var item in containerClient.GetBlobsAsync())
      {
          var blobClient = containerClient.GetBlobClient(item.Name);
          turbine.ImagesURLs!.Add(blobClient.Uri.ToString());
      }
  }
  private void AddToObservable(Turbine turbine)
  {
      TurbinePins.Add(new TurbinePin { Turbine = turbine });
  }
  public ObservableCollection<TurbinePin> GetTurbinePinsForUI(ICommand pinClickedCommand)
  {
      foreach (var pin in TurbinePins.OrderBy(t => t.Turbine?.InstalationDateTime))
      {
          pin.PinClickedCommand = pinClickedCommand;
      }
      return TurbinePins;
  }


and a homepage, to get my list and initialize

        public Command<TurbinePin> OnPinMarkerClickedCommand { get; set; }
        protected readonly TurbinesService _turbinesService;
        public ObservableCollection<TurbinePin> Turbines => _turbinesService.TurbinePins;
        public HomePageViewModel(HttpService service, DeviceLanguageService
            deviceLanguage, TurbinesService turbinesService, IServiceProvider serviceProvider)
        {
            deviceLanguageService = deviceLanguage;
            httpService = service;
            LoadNews();
            provider = serviceProvider;
            _turbinesService = turbinesService;
            OnPinMarkerClickedCommand = new Command<TurbinePin>(OnPinMarkerClicked);
            _turbinesService.GetTurbinePinsForUI(OnPinMarkerClickedCommand);
        }
        public ObservableCollection<Article>? NewsList { get; set; } =
            [];
        async void LoadNews()
        {
            var language = deviceLanguageService.GetDeviceCultureInfo();
            var newsUrl = AppConstants.GetNewsUrl(language.TwoLetterISOLanguageName);
            NewsList!.Clear();
            var newsObj = await httpService.GetAsync<News>(newsUrl);
            foreach (var item in newsObj!.Articles!)
            {
                NewsList.Add(item);
            }
        }
        [RelayCommand]
        protected void ShowNewsDetail(Article article)
        {
            if (article != null)
            {
                Shell.Current.GoToAsync($"{nameof(ArticleDetailsPage)}",
                    true,
                    new Dictionary<string, object> {
                        { "articleObj", article }
                    });
            }
        }
        void OnPinMarkerClicked(TurbinePin turbine)
        {
            if (turbine != null)
            {
#if ANDROID || IOS
                Shell.Current.GoToAsync($"{nameof(TurbineDetailPage)}",
                     true,
                     new Dictionary<string, object> {
                    { "SelectedTurbine", turbine }
                });
#elif WINDOWS || MACCATALYST
                var viewModel = provider.GetRequiredService<TurbineDetailPageViewModel>();
                viewModel.SelectedTurbine = turbine;
                var page = provider.GetRequiredService<TurbineDetailPage>();
                page.BindingContext = viewModel;
                var secondWindow = new Window(page);
                var existingWindow = Application.Current!.Windows.FirstOrDefault(
                    w => w.Page is TurbineDetailPage detailPage);
                if (existingWindow != null)
                {
                    Application.Current?.ActivateWindow(existingWindow);
                }
                else
                {
                    Application.Current!.OpenWindow(secondWindow);
                }
#endif

and I have a custom control, to help me navigate

using System.Collections;

namespace METROWIND.Controls
{
    public partial class CustomMapPin: Pin
    {

        public static readonly BindableProperty MarkerClickedCommandProperty =
            BindableProperty.Create(nameof(MarkerClickedCommand), typeof(ICommand),
                typeof(CustomMapPin));

        public ICommand? MarkerClickedCommand
        {
            get => (ICommand)GetValue(MarkerClickedCommandProperty);
            set => SetValue(MarkerClickedCommandProperty, value);
        }

        public static readonly BindableProperty MarkerClickedCommandParameterProperty = BindableProperty.Create(
            nameof(MarkerClickedCommandParameter), typeof(TurbinePin), typeof(CustomMapPin));

        public TurbinePin MarkerClickedCommandParameter
        {
            get => (TurbinePin)GetValue(MarkerClickedCommandParameterProperty);
            set => SetValue(MarkerClickedCommandParameterProperty, value);
        }

        public static readonly BindableProperty CustomPinLabelProperty = BindableProperty.Create(
            nameof(CustomPinLabel), typeof(string), typeof(CustomMapPin));

        public string CustomPinLabel
        {
            get => (string)GetValue(CustomPinLabelProperty);
            set => SetValue(CustomPinLabelProperty, value);
        }

        public static readonly BindableProperty CustomPinAddressProperty = BindableProperty.Create(
            nameof(CustomPinAddress), typeof(string), typeof(CustomMapPin));

        public string CustomPinAddress
        {
            get => (string)GetValue(CustomPinAddressProperty);
            set => SetValue(CustomPinAddressProperty, value);

        }


        public static readonly BindableProperty ImagesProperty = BindableProperty.Create(
            nameof(Images), typeof(IEnumerable), typeof(CustomMapPin));

        public string Images
        {
            get => (string)GetValue(ImagesProperty);
            set => SetValue(ImagesProperty, value);
        }


        public CustomMapPin()
        {
            MarkerClicked += CustomPin_MarkerClicked;
        }

        private void CustomPin_MarkerClicked(object? sender, PinClickedEventArgs e)
        {
            // Ensure info window is shown
            e.HideInfoWindow = true;

            // Execute command if any
            if (MarkerClickedCommand?.CanExecute(MarkerClickedCommandParameter) == true)
            {
                MarkerClickedCommand.Execute(MarkerClickedCommandParameter);
            }
        }
    }
}


     <maps:Map
         x:Name="ChargingStationMap"
         ItemsSource="{x:Binding Turbines}"
         VerticalOptions="FillAndExpand">
         <maps:Map.ItemTemplate>
             <DataTemplate x:DataType="model:TurbinePin">
                 <controls:CustomMapPin
                     Address="{x:Binding Turbine.Address}"
                     Images="{x:Binding Turbine.ImagesURLs}"
                     Label="{x:Binding Turbine.Name}"
                     Location="{x:Binding Turbine.Location}"
                     MarkerClickedCommand="{x:Binding PinClickedCommand}"
                     MarkerClickedCommandParameter="{x:Binding}" />
             </DataTemplate>
         </maps:Map.ItemTemplate>
     </maps:Map>

but the problem i, that it will navvigatee somtimes

damo

https://reccloud.com/u/m2z2s0w

this error only occurs on mobile

.Net 9

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,729 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.