如何从Xamarin.forms中的按钮单击从一个页面移动到另一个页面

时间:2015-08-17 10:37:44

标签: c# ios xamarin.forms

我只是想知道如何从一个内容页面移动到另一个内容页面。请查看 elseif()代码。我必须在该块中写入以便我可以移动到另一个内容页面(命名为MainView.cs)..

 button.Clicked += (sender, e) =>
        {
            if (String.IsNullOrEmpty(username.Text) || String.IsNullOrEmpty(password.Text))
            {
                DisplayAlert("Oops!!Validation Error", "Username and Password are required", "Re-try");
            }

            else if (username.Text == "kanak" && password.Text == "1234")
            {
               // your code here                   
            }
            else
            {
                DisplayAlert("Failed", "Invalid User", "Login Again");
            }
        };

感谢任何帮助..

2 个答案:

答案 0 :(得分:5)

首先将contentPage包裹在NavigationPage中。从那里,您将使用PushAsync(new SomeNewPage());导航。

在你的情况下它应该是......

 else if (username.Text == "kanak" && password.Text == "1234")
            {
                Navigation.PushAsync(new <App-Root-name>.<folder-name>.<Class-Name>());
            }

示例示例(取自GitHub)...

using System;
using Xamarin.Forms;

namespace FormsGallery
{
    class TableViewMenuDemoPage : ContentPage
    {
        public TableViewMenuDemoPage()
        {
            Label header = new Label
            {
                Text = "TableView for a menu",
                Font = Font.SystemFontOfSize(30, FontAttributes.Bold),
                HorizontalOptions = LayoutOptions.Center
            };
            TableView tableView = new TableView
                {
                    Intent = TableIntent.Menu,
                    Root = new TableRoot
                    {
                        new TableSection("Views for Presentation")
                        {
                            new TextCell
                            {
                                Text = "Label",
                                Command = new Command(async () => 
                                    await Navigation.PushAsync(new LabelDemoPage()))
                            },
                            new TextCell
                            {
                                Text = "Image",
                                Command = new Command(async () => 
                                    await Navigation.PushAsync(new ImageDemoPage()))
                            },
                            new TextCell
                            {
                                Text = "BoxView",
                                Command = new Command(async () => 
                                    await Navigation.PushAsync(new BoxViewDemoPage()))
                            },
                            new TextCell
                            {
                                Text = "WebView",
                                Command = new Command(async () => 
                                    await Navigation.PushAsync(new WebViewDemoPage()))
                            },
                        }
                    }
                };

            // Build the page.
            this.Content = new StackLayout
            {
                Children = 
                {
                    header,
                    tableView
                }
            };
        }
    }
}

答案 1 :(得分:1)

else if (username.Text == "kanak" && password.Text == "1234")
        {
            Navigation.PushAsync(new <App-Root-name>.<folder-name>.<Class-Name>());
        }

在代码中。 <App-Root-name><folder-name>以及<Class-Name> ???

的含义是什么