2014-10-10 108 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(); 
    }