Share via

Issue with loading CarouselView tabs

Sreejith Sreenivasan 1,001 Reputation points
2024-01-31T10:15:32.6933333+00:00

My home page is a CarouselView having 5 tabs.

	<CarouselView 
		x:Name="carouselView"
		CurrentItemChanged="OnCurrentItemChanged"
		Loop="False">
		<CarouselView.ItemsSource>
			<x:Array Type="{x:Type ContentView}">
				<control:HomeTabPage/>
				<control:KeyPadPage/>
				<control:CallHistoryPage/>
				<control:ContactsPage/>
				<control:RequestTab/>
			</x:Array>
		</CarouselView.ItemsSource>
		<CarouselView.ItemTemplate>
			<DataTemplate>
				<ContentView Content="{Binding}"/>
			</DataTemplate>
		</CarouselView.ItemTemplate>
	</CarouselView>

I am loading the data on each tabs using the WeakReferenceMessenger. When swipe the tabs or click I send a message using WeakReferenceMessenger like below:

	void OnCurrentItemChanged(object sender, CurrentItemChangedEventArgs e)
	{
		int index = carouselView.Position;
		AddWeakReferenceMessenger(index);
	}
	
	private void AddWeakReferenceMessenger(int index)
	{
		if (index == 0)
		{
			WeakReferenceMessenger.Default.Send(new HomeTabChangeMessage("home_tab"));
		}
		else if (index == 1)
		{
			WeakReferenceMessenger.Default.Send(new HomeTabChangeMessage("keypad_tab"));
		}
		else if (index == 2)
		{
			WeakReferenceMessenger.Default.Send(new HomeTabChangeMessage("history_tab"));
		}
		else if (index == 3)
		{
			WeakReferenceMessenger.Default.Send(new HomeTabChangeMessage("contacts_tab"));
		}
		else if (index == 4)
		{
			WeakReferenceMessenger.Default.Send(new HomeTabChangeMessage("requests_tab"));
		}
	}

And I am subscribing it on child tabs like below and start loading data via API call:

	WeakReferenceMessenger.Default.Register<HomeTabChangeMessage>(this, (r, m) =>
	{
		if (m.Value == "keypad_tab")
		{
			Debug.WriteLine("isKeyPadLoaded:>>" + Utility.isKeyPadLoaded);
			if (!Utility.isKeyPadLoaded)
			{
				Utility.isKeyPadLoaded = true;
				//Start the API call from here
			}
		}
	});

Here I am using Boolean variables to check the tabs are already loaded or not. Its default value is false and after loading a tab I am setting its value as true. So If I view the same tab again it will start the API calls. The same way I subscribed the WeakReferenceMessenger on all the other child tabs. The feature was working fine with this approach. My Problem: If I load all the tabs, then log out from the app and log in again the tabs are not loading. I have created a screen recorder video and upload it here for the easy understanding. When I logging out and logging in I am resetting the value of all the Boolean variable to false. But When I view the tabs the data is not loading. I printed the value of Boolean variable when I logging out and logging in and it is showing false initially and true after that. That means the WeakReferenceMessenger invoking multiple times. User's image

I suspect the issue is due to the WeakReferenceMessenger's multiple invoking; because of that the code execution is not going inside of the if block of the bool value check. Do I need to unregister the WeakReferenceMessenger or something? I created a demo project based on this issue, but the issue was not replicating on this demo project. For reference, I upload Here

Developer technologies | .NET | .NET Multi-platform App UI

Answer accepted by question author

Anonymous
2024-02-01T06:28:02.1633333+00:00

Do you want to register this message, then execute code only once in the Register event when Tabbedpage showing?

If so, you need to unregister the WeakReferenceMessenger with WeakReferenceMessenger.Default.Unregister<HomeTabChangeMessage>(this);. If unregister method is not working. Please try to use following code. It works normally in my side.

if(!WeakReferenceMessenger.Default.IsRegistered<HomeTabChangeMessage>(this))
{
    WeakReferenceMessenger.Default.Register<HomeTabChangeMessage>(this, (r, m) =>
    {
        MainThread.BeginInvokeOnMainThread(async () =>
        {
            if (m.Value == "Tab1")
            {
                Debug.WriteLine("1=isTab1Loaded:>>" + Utility.isTab1Loaded);
                if (!Utility.isTab1Loaded)
                {
                    Utility.isTab1Loaded = true;
                    Debug.WriteLine("***Tab1Loaded***");
                }
            }
        });
        WeakReferenceMessenger.Default.Unregister<HomeTabChangeMessage>(this);
    });
}

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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