如何在两个不同的 Xamarin 表单页面之间切换?
How do you switch between two different Xamarin Form pages?
我正在尝试弄清楚如何在 Xamarin Forms 中的两个不同页面之间切换。
我不想使用NavigationPage
(它有自动显示的小后退箭头。
我有一个登录页面(ContentPage
),此人通过身份验证后,我需要导航到我的仪表板页面(TabbedPage
)。
例如
接下来,TabbedPage 中的 Tab 之一是登录用户的个人资料。因此,我需要注销它们。所以我将有一个按钮将它们注销,这意味着我需要将它们导航回登录页面(即 ContentPage
)。
我觉得我有两个 模式 用户可能会在。
- 未经授权。 (
ContentPage
)
- 授权。 (
TabbedPage
).
好像我需要将 App
的 MainPage
更改为这两个之一?
您可以通过在 Application 实例中设置 MainPage 照常进行导航。小样本。
namespace TestNavigation
{
public class App : Application
{
public App ()
{
// The root page of your application
MainPage = new MyPage1 ();
}
}
}
namespace TestNavigation
{
public class MyPage1 : ContentPage
{
public MyPage1 ()
{
var button = new Button () {
Text = "Click me"
};
button.Clicked += (object sender, EventArgs e) => {
Application.Current.MainPage = new MyPage2 ();
};
Content = new StackLayout {
Children = {
button
}
};
}
}
}
namespace TestNavigation
{
public class MyPage2 : ContentPage
{
public MyPage2 ()
{
Content = new StackLayout {
Children = {
new Label { Text = "Hello ContentPage" }
}
};
}
}
}
EDIT1
我提交了这个答案,但后来我意识到我可能不明白你的问题。
EDIT2
更新示例。
要将 MainPage 更改为另一个只需执行以下操作:
App.Current.MainPage = new NavigationPage(new MyContentPage());
或
App.Current.MainPage = new MyContentPage();
顺便说一句:您可以使用 NavigationPage 然后隐藏工具栏:
NavigationPage.SetHasNavigationBar(this, false);
如果你想在页面之间切换,那么你可以简单地做
Application.Current.MainPage=new YourPage();
How to Change MainPage in xamarin forms at runtime?
这就是我为获得结果所做的工作
对于登录页面,最佳做法是使用
await Navigation.PushModalAsync (new LoginPage());
进入登录页面后,验证通过后即可使用popModelAsync:
await Navigation.PopModalAsync ();
我正在尝试弄清楚如何在 Xamarin Forms 中的两个不同页面之间切换。
我不想使用NavigationPage
(它有自动显示的小后退箭头。
我有一个登录页面(ContentPage
),此人通过身份验证后,我需要导航到我的仪表板页面(TabbedPage
)。
例如
接下来,TabbedPage 中的 Tab 之一是登录用户的个人资料。因此,我需要注销它们。所以我将有一个按钮将它们注销,这意味着我需要将它们导航回登录页面(即 ContentPage
)。
我觉得我有两个 模式 用户可能会在。
- 未经授权。 (
ContentPage
) - 授权。 (
TabbedPage
).
好像我需要将 App
的 MainPage
更改为这两个之一?
您可以通过在 Application 实例中设置 MainPage 照常进行导航。小样本。
namespace TestNavigation
{
public class App : Application
{
public App ()
{
// The root page of your application
MainPage = new MyPage1 ();
}
}
}
namespace TestNavigation
{
public class MyPage1 : ContentPage
{
public MyPage1 ()
{
var button = new Button () {
Text = "Click me"
};
button.Clicked += (object sender, EventArgs e) => {
Application.Current.MainPage = new MyPage2 ();
};
Content = new StackLayout {
Children = {
button
}
};
}
}
}
namespace TestNavigation
{
public class MyPage2 : ContentPage
{
public MyPage2 ()
{
Content = new StackLayout {
Children = {
new Label { Text = "Hello ContentPage" }
}
};
}
}
}
EDIT1
我提交了这个答案,但后来我意识到我可能不明白你的问题。
EDIT2
更新示例。
要将 MainPage 更改为另一个只需执行以下操作:
App.Current.MainPage = new NavigationPage(new MyContentPage());
或
App.Current.MainPage = new MyContentPage();
顺便说一句:您可以使用 NavigationPage 然后隐藏工具栏:
NavigationPage.SetHasNavigationBar(this, false);
如果你想在页面之间切换,那么你可以简单地做
Application.Current.MainPage=new YourPage();
How to Change MainPage in xamarin forms at runtime?
这就是我为获得结果所做的工作
对于登录页面,最佳做法是使用
await Navigation.PushModalAsync (new LoginPage());
进入登录页面后,验证通过后即可使用popModelAsync:
await Navigation.PopModalAsync ();