Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

How to use the same client between pages?

$
0
0

Hello, I'm developing an application in Xamarin Forms.

I'm creating a mqttClient everytime the user Log In and I wanted to use the same client through all App.

From the main page I create the client with the options I want, set the listener and set a loop to check every 5s if I'm still conected to the client, otherwise I reconnect.

When I'm navigating through pages I'm passing the mqttClient to other pages this way:
The navigation service:
await this._navigationService.NavigateToCreatePlace(Navigation, mqttClient);

       public async Task NavigateToCreatePlace(INavigation navigation, IMqttClient receivedMqttClient)
        {
            try
            {
                await navigation.PushAsync(new CreatePlacePage(receivedMqttClient));
            }
            catch(Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }

In the Page:

        public CreatePlacePage(IMqttClient receivedMqttClient)
        {
            InitializeComponent();
            this.BindingContext = new CreatePlaceViewModel(Navigation,receivedMqttClient);
        }

In the ViewModel I have:
public IMqttClient mqttClient;

        public CreatePlaceViewModel(INavigation navigation, IMqttClient receivedMqttClient)
        {
                mqttClient=receivedMqttClient;
        }

The problem is: the loop that is printing if my client is connected keeps printing that I'm connected, but sometimes if I try to use the mqttClient inside CreatePlacePage it says to me that the client is not connected.
So, I think the problem is the way I'm passing the values. It doesn't seems to me that the mqttClient inside the second page is the same as the one that I've created in the main page. Can anyone help me?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>