在Xamarin.forms中如何通过按钮点击从一个页面跳转到另一个页面

3

我想知道如何从一个内容页移动到另一个内容页。请查看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个回答

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", Font = Font.SystemFontOfSize(30, FontAttributes.Bold), HorizontalOptions = LayoutOptions.Center }; TableView tableView = new TableView { Intent = TableIntent.Menu, Root = new TableRoot { new TableSection("用于演示的视图") { 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())) }, } } };
// 构建页面。 this.Content = new StackLayout { Children = { header, tableView } }; } } }

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

在代码中,“”、“”和“”是什么意思?

<App-Root-name> and <folder-name> are the namespace of the class. In the able answer, it is FormsGallery. And <Class-Name> is the name of the content page, whereas in this case it is TableViewMenuDemoPage. So for the above answer, it should be as follows. Navigation.PushAsync(new FormsGallery.TableViewMenuDemoPage()); - Curiosity

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接