2014-10-10 42 views
5

我使用xamarin表单创建新的移动应用程序。我必须创建两个页面登录屏幕和主屏幕。 我从Here得到样品Xamarin.form setpage第二次没有工作

但是,当我创建相同的我不能去第二页。它始终只保留登录页面。

在我的Android MainActivity代码调用第二次,但页面内容

public class MainActivity : AndroidActivity, LoginManager 
{ 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     Xamarin.Forms.Forms.Init(this, bundle); 

     SetPage(App.GetLoginPage(this)); 
     // SetPage(App.GetLoginPage(this)); 


    } 
    #region ILoginManager implementation 
    public void ShowMainPage() 
    { 
     SetPage(App.GetHomePage(this)); 
    } 

    public void Logout() 
    { 
     SetPage(App.GetLoginPage(this)); 
    } 
    #endregion 
} 

setPage方法不会被替换。请帮助任何一个

+0

的Xamarin.Forms框架处理导航为您服务。您应该使用Xamarin.Forms.Navigation类来管理导航堆栈,并避免在Android项目中使用SetPage, – 2016-12-28 18:47:19

回答

0

这是一个解决Android的第二套页面问题。创建第二个界面,而不是使用现有活动的设置页面启动新活动的OnCreate,其中新活动的OnCreate调用设置页面并完成当前活动。

App.cs

public static ILoginManager LoginManager; 
    public static IAppNavigation SplashManger;  


    public static Page GetLoginPage(ILoginManager lmanager) 
    { 
     LoginManager = lmanager;   
     return new Page_Login(); 
    } 

    public static Page GetShowSplashPage(IAppNavigation iSplashNavigation) 
    { 
     SplashManger = iSplashNavigation; 
     return new Page_Splash(); 
    } 

的Android MainActivty

[Activity(Label = "", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] 
public class MainActivity : AndroidActivity, IAppNavigation 
{ 
    protected override void OnCreate(Bundle bundle) 
    { 
     //Window.RequestFeature(WindowFeatures.NoTitle); 
     base.OnCreate(bundle); 

     Xamarin.Forms.Forms.Init(this, bundle); 
     SetPage(App.GetShowSplashPage(this)); 
    } 



    public void GetLoginPage() 
    { 
     StartActivity(new Intent(this, typeof(LoginActivity))); 
     Finish(); 
    } 

的Android LoginActivity

[Activity(Label = "", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] 
public class LoginActivity : AndroidActivity, ILoginManager 
{ 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     Xamarin.Forms.Forms.Init(this, bundle); 
     SetPage(App.GetLoginPage(this)); 
    } 



    public void GetMainMenu() 
    { 
     StartActivity(new Intent(this, typeof(MainMenuActivity))); 
     Finish(); 
    } 

在你的iOS不需要做任何特别的事情,只需实现你将要在App委托中使用的所有必要的接口。

的iOS的AppDelegate

[Register("AppDelegate")] 
public partial class AppDelegate : UIApplicationDelegate, ILoginManager, IAppNavigation 
{ 
    // class-level declarations 
    UIWindow window; 


    public override bool FinishedLaunching(UIApplication app, NSDictionary options) 
    { 
     Forms.Init(); 

     window = new UIWindow(UIScreen.MainScreen.Bounds); 

     window.RootViewController = App.GetShowSplashPage(this).CreateViewController(); 

     window.MakeKeyAndVisible(); 

     return true; 
    } 

    public void GetMainMenu() 
    { 
     window.RootViewController = App.GetMainMenu().CreateViewController(); 
     window.MakeKeyAndVisible(); 
    } 

    public void GetLoginPage() 
    { 
     window.RootViewController = App.GetLoginPage(this).CreateViewController(); 
     window.MakeKeyAndVisible(); 
    }