Hiding FlyoutItem based on user roles

ymq049740 1 Reputation point
2021-07-13T18:35:35.407+00:00

Hi there,
I recently watched a David Ortinau's video on youtube about hiding flyout/tabs using IsVisible property. I tried to modify the code, which is using tabs, to flyoutitem. However, for some reason I couldn't manage to trigger the IsVisible property from false to true after calling the messaging center.

Here is my code at AppShell.xaml

<Shell.BindingContext>
        <viewmodel:AppShellViewModel />
</Shell.BindingContext>


<FlyoutItem Title="News" Icon="icon_about.png">
        <ShellContent ContentTemplate="{DataTemplate userviews:NewsPage}" Route="News" />
    </FlyoutItem>

    <FlyoutItem Title="Add Station" Icon="icon_about.png">
        <ShellContent
            ContentTemplate="{DataTemplate adminviews:AddStationPage}"
            IsVisible="{Binding IsAdmin}"
            Route="AddStationPage" />
    </FlyoutItem>

    <FlyoutItem Title="Maps" Icon="icon_about.png">
        <ShellContent ContentTemplate="{DataTemplate userviews:MapPage}" Route="MapPage" />
    </FlyoutItem>

The task in LoginPageViewModel which triggers the MessagingCenter

public async Task Login()
        {
            if (user != null)
            {
                if (!string.IsNullOrWhiteSpace(User.Email) && !string.IsNullOrWhiteSpace(User.Password))
                {
                    bool canlogin = await UserService.ValidateLogin(User.Email, User.Password);
                    if (canlogin)
                    {
                        string userRole = App.globaluser.Role.ToLower();

                        MessagingCenter.Send<LoginPageViewModel>(this,
                            (userRole == "admin") ? "admin" : "user"
                            );

                        await Shell.Current.GoToAsync($"//{nameof(MapPage)}");
                    }
                    return;
                }
                else
                {
                    await Shell.Current.DisplayAlert("Error", "Empty fields!", "OK");
                }
            }
            else
            {
                await Shell.Current.DisplayAlert("Error", "Empty fields!", "OK");
            }
        }

And the receiving end of the messaging center that changes the binded property in AppShellViewModel

private bool isAdmin;

        public bool IsAdmin { get => isAdmin; set => SetProperty(ref isAdmin, value); }

        public AppShellViewModel()
        {
            MessagingCenter.Subscribe<LoginPageViewModel>(this, message: "admin", (sender) =>
            {
                IsAdmin = true;
            });

            MessagingCenter.Subscribe<LoginPageViewModel>(this, message: "user", (sender) =>
            {
                IsAdmin = false;
            });
        }

When debugging by placing breakpoints, everything was going well with both ends of messaging center sending and receiving message, even changed the IsAdmin value to true, but whenever it reaches the await Shell.Current.GoToAsync($"//{nameof(MapPage)}"); line, the current shell only renders the flyouts that does not have IsVisible binded.

I am suspecting some data binding issues causing this but can't seem to find it

Any help would be appreciated. Thanks in advance!

Developer technologies | .NET | Xamarin
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

1 answer

Sort by: Newest
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

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.