0

On click on the navigate button, system throws exception in my xamarin.Form app. I am trying to get the email text value and display in the Home page.

Exception: Unhandled Exception: System.InvalidOperationException: 
<Timeout exceeded getting exception details>`

MainPage.xaml.cs;

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        var emailText = emailEntry.Text;
        var passwordText = passwordEntry.Text;
    }
    int count = 0;

    public void Button_Clicked(object sender, System.EventArgs e)
    {
        string text = emailEntry.Text;
    }

    public async void NavigateButton_OnClicked(object sender, EventArgs e)
    {
        await Navigation.PushAsync(new Home(emailEntry.Text));
    }
}

Following is my Home.xaml.cs where I need to display the text email value from the MainPage.xaml.cs;

public partial class Home : ContentPage
{
    public Home(string parameter1)
    {
        InitializeComponent();
        HomeLabel.Text = parameter1;
    }
}

App.xaml.cs details below;

public partial class App : Application
{
    string parameter1;
    public App()
    {
        InitializeComponent();
        var tabbedPage = new TabbedPage();
        tabbedPage.Children.Add(new Home(parameter1));
        tabbedPage.Children.Add(new Map());
        tabbedPage.Children.Add(new Settings());
        MainPage = new MainPage();
        //MainPage = new TabbedPage();
        //MainPage = tabbedPage;
    }

    protected override void OnStart()
    {
        // Handle when your app starts
    }

    protected override void OnSleep()
    {
        // Handle when your app sleeps
    }

    protected override void OnResume()
    {
        // Handle when your app resumes
    }
}

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:soccerapp"
    x:Class="soccerapp.MainPage">

    <StackLayout Spacing="20" Padding="50">
        <Entry x:Name="emailEntry" Placeholder="Email Id"></Entry>
        <Entry x:Name="passwordEntry" Placeholder="Password" IsPassword="True"></Entry>
        <Button Text="Log In"  Clicked="Button_Clicked" TextColor="White" BackgroundColor="#404751"></Button>
        <Button Text="Navigate to Home"  Clicked="NavigateButton_OnClicked" TextColor="White" BackgroundColor="ForestGreen"></Button>

        <!-- Place new controls here -->
        <Label Text="Welcome Mate!" HorizontalOptions="Center" VerticalOptions="CenterAndExpand" />
        </StackLayout>

</ContentPage>

Home.xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="soccerapp.Home" BackgroundColor="GreenYellow" Title="Home">
    <ContentPage.Content>
        <StackLayout>
            <Label x:Name="HomeLabel"  Text="Home Page is here" TextColor="White"
            VerticalOptions="CenterAndExpand" 
            HorizontalOptions="CenterAndExpand"></Label>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>
7
  • Can you post your xaml Commented Apr 10, 2019 at 3:20
  • InvalidOperationException seems weird here. Can you check the stack trace to see the source of the exception? Commented Apr 10, 2019 at 3:27
  • Why do you use Navigation.PushAsync(new Home(...)) if you already have this page in your TabbedPage? Commented Apr 10, 2019 at 3:27
  • 3
    MainPage = new NavigationPage(new MainPage()); Commented Apr 10, 2019 at 3:32
  • 1
    @soccerway please see my answer, you use Navigation and TabbedPage in a wrong way. Commented Apr 10, 2019 at 3:50

1 Answer 1

1

Ok, first of all you have to create NavigationPage in you App.xaml.cs. Also, you need to move your TabbedPage initialization into separate xaml file or in NavigationsClicked event.

  1. Edit your App.xaml.cs:
public App()
    {
        InitializeComponent();
        MainPage = new NavigationPage(new MainPage());
    }
  1. There are 2 ways of doing it:

Create your TabbedPage in NavigationClicked event:

public async void NavigateButton_OnClicked(object sender, EventArgs e)
{
    var tabbedPage = new TabbedPage();
    tabbedPage.Children.Add(new Home(parameter1));
    tabbedPage.Children.Add(new Map());
    tabbedPage.Children.Add(new Settings());
    await Navigation.PushAsync(tabbedPage);
}

or create new XAML file for TabbedPage:

<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            x:Class="MyTabbedPage">
  <!--Pages can be added as references or inline-->
    <ContentPage Title="HomeTitle">
  <!--Content for Home Page-->
    </ContentPage>

    <ContentPage Title="MapTitle">
  <!--Content for Map Page-->
    </ContentPage>

    <ContentPage Title="SettingsTitle">
  <!--Content for Settings Page-->
    </ContentPage>
</TabbedPage>
Sign up to request clarification or add additional context in comments.

9 Comments

Sorry I didn't get that completely, where do I need add CurrentPage = Children[yourSpecificIndex];
@soccerway hmm, by the way, I'm wrong. You didn't use your tabbed page in an app. I'll edit my answer now, please wait.
Thanks, I saw that, but what do you mean by CurrentPage, in my case it should be MainPage right ?
I am confused now, why shouldn't i use the tabbed page ? Where do i make changes now ?
@soccerway Yes, you should. You should use TabbedPage, but in a proper way. You initialize it in App.xaml.cs, but never use. Please see my answer edit.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.