cross-platform approach

Eduardo Gomez Romero 1,355 Reputation points
2024-11-23T11:36:16.55+00:00

I have his method

      void OnPinMarkerClicked(object turbine)
        {
            if (turbine != null)
            {
#if ANDROID || IOS
                Shell.Current.GoToAsync($"{nameof(TurbineDetailPage)}",
                     true,
                     new Dictionary<string, object> {
                    { "SelectedTurbine", turbine }
                });
#elif WINDOWS || MACCATALYST
                var viewModel = _serviceProvider.GetRequiredService<TurbineDetailPageViewModel>();
                var page = new TurbineDetailPage(viewModel);
                Application.Current!.OpenWindow(new Window(page));
#endif


And it navigates with my data on mobile

but on desktop I don't get anything

public partial class TurbineDetailPage : ContentPage
{
    public TurbineDetailPage(TurbineDetailPageViewModel turbineDetailVViewModel)
    {
        InitializeComponent();
        BindingContext = turbineDetailVViewModel;
    }
}

VM

    [QueryProperty(nameof(SelectedTurbine), "SelectedTurbine")]
    public partial class TurbineDetailPageViewModel : ObservableObject
    {
        [ObservableProperty]
        TurbinePin? selectedTurbine;
    }
}


i MADE THAT HAPPENED WITH THIS

  var viewModel = _serviceProvider.GetRequiredService
      <TurbineDetailPageViewModel>();
  viewModel.SelectedTurbine = (TurbinePin)turbine;
  var page = new TurbineDetailPage(viewModel);
  var secondWindow = new Window(page);
  var existingWindow = Application.Current!.Windows.FirstOrDefault(
      w => w.Page is TurbineDetailPage detailPage
      && detailPage.BindingContext == viewModel);
  if (existingWindow != null)
  {
      Application.Current?.ActivateWindow(existingWindow);
  }
  else
  {
      Application.Current!.OpenWindow(secondWindow);
  }
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,042 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 81,186 Reputation points Microsoft External Staff
    2024-11-26T07:33:16.15+00:00

    Hello,

    Answer from Eduardo Gomez Romero

    it was a singleton, I fixed it with transient.

    Please set TurbineDetailPageViewModel to the transient in MauiProgram.cs.

    var services = builder.Services;
    
    services.AddTransient<TurbineDetailPageViewModel>();
    
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

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.