2017-07-21 76 views
0

使用Android佈局this.SetContentView(Resource.Layout.Main)和LoadApplication(新App())後,我看不到Xamarin.Forms MainPage.xaml。我試圖刪除線性佈局,但它只是刪除它們中的項目,而不是窗體本身。看起來像SetContentView阻塞Xamarin.Forms。所以,使用Acticity.SetContentView後不顯示Xamarin.Forms內容

這裏是MainActivity.cs

using Android.App; 
using Android.Content; 
using Android.Content.PM; 
using Android.OS; 
using Android.Text.Method; 
using Android.Widget; 
using Auth0.OidcClient; 
using IdentityModel.OidcClient; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Xamarin.Forms; 

namespace IndoFellowship.Droid { 
    // Define App icon and name 
    [Activity(
     Label = "Indo App", 
     MainLauncher = true, 
     Icon = "@drawable/icon", 
     LaunchMode = LaunchMode.SingleTask) 
    ] 
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity { 

     protected override void OnCreate(Bundle bundle) { 
      base.OnCreate(bundle); 
      global::Xamarin.Forms.Forms.Init(this, bundle); 

      // Showing layout that I set 
      SetContentView(Resource.Layout.Main); 

      // Do login 
      // Some codes 

      // Done processing on Android, I need to load Xamarin.Forms 
      LinearLayout mainLayout = (LinearLayout)FindViewById(Resource.Id.mainLayoutID); 
      mainLayout.RemoveAllViews(); 

      // Load App 
      LoadApplication(new App()); 
     } 
    } 
} 

Main.axml上的資源/值/

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayoutID" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    // Some layout here.. 

</LinearLayout> 

和我App.xaml.cs(Xamarin)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Xamarin.Forms; 

namespace IndoFellowship { 
    public partial class App : Application { 
     public static string AppName { get; set; } 

     public App() { 
      InitializeComponent(); 

      MainPage = new NavigationPage(new MainPage()); 
     } 

     protected override void OnStart() { 
      // Handle when your app starts 
     } 

     protected override void OnSleep() { 
      // Handle when your app sleeps 
     } 

     protected override void OnResume() { 
      // Handle when your app resumes 
     } 
    } 
} 

MainPage.xaml.cs中(Xamarin)

using Auth0.OidcClient; 
using IdentityModel.OidcClient; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Xamarin.Auth; 
using Xamarin.Forms; 

namespace IndoFellowship 
{ 
    public partial class MainPage : ContentPage { 
     public MainPage() 
     { 
      InitializeComponent(); 
      var browser = new WebView(); 

      browser.Source = "http://xamarin.com"; 

      Content = browser; 
     } 
    } 
} 

在這裏,我應該會看到xamarin.com webView的,但我的Android只是一片空白。這看起來像Xamarin.Forms沒有顯示,我仍然在Xamarin.Android。如果我使用SetContentView,我可以看到完美呈現的表單,但我無法處理在Android上需要做的事情。

  • 我需要做什麼才能正確加載MainActivity中的Xamarin表單?
  • 爲什麼佈局不被刪除?

謝謝。

回答

0

我通過使用StartActivity而不是LoadApplication找到了我的解決方案。

創建第二個活動:

XamarinForms.cs這個活動負載應用程序()

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 

namespace IndoFellowship.Droid { 
    [Activity(Label = "XamarinForm")] 
    public class XamarinForm : Xamarin.Forms.Platform.Android.FormsApplicationActivity { 
     protected override void OnCreate(Bundle bundle) { 
      base.OnCreate(bundle); 

      Xamarin.Forms.Forms.Init(this, bundle); 
      LoadApplication(new App()); 
     } 
    } 
} 

MainActivity.cs代替LoadApplication在這裏,我呼籲的第二項活動,而不是

using Android.App; 
using Android.Content; 
using Android.Content.PM; 
using Android.OS; 
using Android.Text.Method; 
using Android.Widget; 
using Auth0.OidcClient; 
using IdentityModel.OidcClient; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Xamarin.Forms; 

namespace IndoFellowship.Droid { 
    // Define App icon and name 
    [Activity(
     Label = "Indo App", 
     MainLauncher = true, 
     Icon = "@drawable/icon", 
     LaunchMode = LaunchMode.SingleTask) 
    ] 
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity { 

     protected override void OnCreate(Bundle bundle) { 
      base.OnCreate(bundle); 
      global::Xamarin.Forms.Forms.Init(this, bundle); 

      // Showing layout that I set 
      SetContentView(Resource.Layout.Main); 

      // Do login 
      // Some codes 

      // Done processing on Android, I need to load Xamarin.Forms 
      LinearLayout mainLayout = (LinearLayout)FindViewById(Resource.Id.mainLayoutID); 
      mainLayout.RemoveAllViews(); 

      // Load App 
      StartActivity(typeof(XamarinForms)); 
     } 
    } 
} 

可能這不是最好的設計,因爲沒有我有兩個活動。請讓我知道,如果你知道更好的方法來實現這一點。謝謝。