No installed components were detected, Element is already a child of another element

Vikas Kumar 0 Reputation points
2024-09-06T14:37:41.8133333+00:00

I am getting following issue when trying to Logout and Log back in to the Application in Winows the code works fine in Android here is my code this error comes up when after login I try to navigate to MainPage:
public LoginPage(OktaClient oktaClient)

{

    InitializeComponent();

    _oktaClient = oktaClient;

    _mainPageViewModel = new MainPageViewModel();

    WeakReferenceMessenger.Default.Register<LogoutMessage>(this, (r, m) =>

    {

        LogoutBtn_Clicked(null, null);

    });

#if WINDOWS

    _oktaClient.Browser = new WebViewBrowserAuthentication(WebViewInstace);

#endif

}

public class LogoutMessage

{

}

private async void LoginBtn_Clicked(object sender, EventArgs e)

{

    viewModel = BindingContext as AppShellViewModel;

    SpinnerPopup spinnerPopup = new();

    LoginBtn.IsVisible = false;

    try

    {

        var loginResult = await _oktaClient.LoginAsync();

        if (loginResult.IsError)

        {

            DialogHelper.DisplayOkDialog("Login Error", loginResult.Error);

            LoginBtn.IsVisible = true;

        }

        if (!loginResult.IsError)

        {

            Application.Current.MainPage.ShowPopup(spinnerPopup);

            _authenticationData = loginResult;

            var name = _authenticationData.User.Claims.FirstOrDefault(c => c.Type == "name")?.Value;

            var email = _authenticationData.User.Claims.FirstOrDefault(c => c.Type == "preferred_username")?.Value;

            await SecureStorageHelper.SaveKeyValuePair("email", email);

            await SecureStorageHelper.SaveKeyValuePair("id_token", _authenticationData.IdentityToken);

            await SecureStorageHelper.SaveKeyValuePair("InspertorName", name);

            var isApiLoginSuccess = await AuthenticationHelper.LoginToAPI();

            if (!isApiLoginSuccess)

            {

                DialogHelper.DisplayOkDialog("Login Error", "An error was encountered while logging in. \n" +

                                           "Please try again.");

                LoginBtn.IsVisible = true;

                return;

            }

            viewModel.IsUserLoggedIn = true;

            LoginStatus.Text = $"Logged in as {name}";

            Application.Current.Resources["AppShellViewModel"] = viewModel;

            LoginStatus.IsVisible = true;

            LogoutBtn.IsVisible = true;

            await Shell.Current.GoToAsync($"//MainPage");

        }

    }

    catch (Exception ex)

    {

        DialogHelper.DisplayOkDialog("Login Error", $"An error was encountered while logging in. {ex.Message} \n" +

            "Please try again.");

        LoginBtn.IsVisible = true;

    }

    finally

    {

        spinnerPopup.Close();

    }

}

private async void LogoutBtn_Clicked(object sender, EventArgs e)

{

    await _oktaClient.LogoutAsync(await SecureStorageHelper.GetValue("id_token"));

    DialogHelper.DisplayOkDialog("Logout Successful", "You have been logged out of the application.");

    SecureStorageHelper.RemoveKeyValue("access_token");

    SecureStorageHelper.RemoveKeyValue("email");

    SecureStorageHelper.RemoveKeyValue("id_token");

    SecureStorageHelper.RemoveKeyValue("refresh_token");

    SecureStorageHelper.RemoveKeyValue("InspertorName");

    LoginBtn.IsVisible = true;

    LoginStatus.IsVisible = false;

    LogoutBtn.IsVisible = false;

    Application.Current.Resources["AppShellViewModel"] = viewModel;

    Application.Current.MainPage = new AppShell();

    await Shell.Current.GoToAsync($"LoginPage", true);

}  

and here is the AppShell where I have the Following ShellContent :
<ShellContent Title="Login" Icon="login_icon.png" ContentTemplate="{DataTemplate views:LoginPage}" IsVisible="{Binding IsNotLoggedIn}" />

<ShellContent x:Name="MainpageName" Route="MainPage" Title="Home" Icon="home_icon.png" IsVisible="{Binding IsLoggedIn}" ContentTemplate="{DataTemplate views:MainPage}" />

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,026 questions
0 comments No comments
{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.