使用Acticity.SetContentView后,Xamarin.Forms内容未显示

时间:2017-07-21 13:50:11

标签: android-layout xamarin xamarin.forms

在使用Android布局this.SetContentView(Resource.Layout.Main)和LoadApplication(新的App())后,我看不到Xamarin.Forms MainPage.xaml。我试图删除线性布局,但它只是删除它们内部的项目,而不是表单本身。似乎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表单需要做些什么?
  • 为什么布局没有删除?

谢谢。

1 个答案:

答案 0 :(得分:0)

我使用StartActivity代替LoadApplication找到了我的解决方案。

通过创建第二个活动:

XamarinForms.cs 此活动会加载app()

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));
        }
    }
}

可能这不是最好的设计,因为现在我有两个活动。如果您知道更好的方法,请告诉我。谢谢。