在Xamarin.Forms应用程序中单击导航按钮时,系统引发异常

时间:2019-04-10 03:06:36

标签: xamarin xamarin.forms

在单击导航按钮时,系统在我的xamarin.Form应用程序中引发异常。我正在尝试获取email文本值并显示在Home页面中。

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));
    }
}

以下是我的Home.xaml.cs,在这里我需要显示MainPage.xaml.cs中的文本电子邮件值;

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

下面的App.xaml.cs详细信息;

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>

1 个答案:

答案 0 :(得分:1)

好吧,首先,您必须在App.xaml.cs中创建NavigationPage。另外,您需要将TabbedPage初始化移动到单独的xaml文件中或发生NavigationsClicked事件中。

  1. 编辑您的App.xaml.cs:
public App()
    {
        InitializeComponent();
        MainPage = new NavigationPage(new MainPage());
    }
  1. 有2种方法:

NavigationClicked事件中创建TabbedPage:

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);
}

或为TabbedPage创建新的XAML文件:

<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>