簡體   English   中英

Xamarin 在同一解決方案中運行其他項目的活動

[英]Xamarin run activity from other project in same solution

我有一個帶有兩個 android 項目的 xamarin 解決方案。 我想在 project2 的 project1 中運行 android 活動。 我已經看到與此主題相關的線程(例如如何在另一個項目中調用活動?從另一個項目調用活動)並嘗試了許多建議的解決方案,但最后我總是得到“Android.Content.ActivityNotFoundException:'無法找到顯式活動 class {companyname.project2/companyname.project2.Droid.View.LoginView}; 您是否在 AndroidManifest.xml 中聲明了此活動?'”錯誤。 如果是的話,這在 xamarin 中是否可行? 難道我做錯了什么? 這是我的源代碼:

項目1:

namespace BigAppManager {

    [Activity (MainLauncher = true, Name = "com.companyname.bigappmanager.MainActivity", Label = " Logowanie", ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize, NoHistory = true, LaunchMode = LaunchMode.SingleInstance)]
    public class MainActivity : MvxActivity<MainActivityViewModel> {

        protected override void OnCreate (Bundle savedInstanceState) {
            base.OnCreate (savedInstanceState);
            Xamarin.Essentials.Platform.Init (this, savedInstanceState);
            
            SetContentView (Resource.Layout.activity_main);
            Button button = FindViewById<Button> (Resource.Id.button);

            button.Click += ClickButton;
        }

        private void ClickButton (object sender, EventArgs e) {
  
            //Intent intent = new Intent("companyname.project2.Droid.View.LoginView");
            //StartActivity(intent);
            Intent intent = new Intent (Intent.ActionMain);
            intent.AddCategory (Intent.CategoryLauncher);
            // intent.SetClassName("companyname.project2", "companyname.project2.Droid.View.LoginView");
            intent.SetComponent (new ComponentName ("companyname.project2", "companyname.project2.Droid.View.LoginView"));
            StartActivity (intent);

        }

        public override void OnRequestPermissionsResult (int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults) {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult (requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult (requestCode, permissions, grantResults);
        }
    }
}

項目2:

[Activity(Name = "companyname.project2.Droid.View.LoginView", Label = " Logowanie", ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.ScreenSize, NoHistory = true, LaunchMode = LaunchMode.SingleInstance)]
    public class LoginView : MvxActivity<LoginViewModel>
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.LoginView);

        }
    }

項目2 android清單意圖過濾器:

<activity android:name="companyname.project2.Droid.View.LoginView">
      <intent-filter>
        <action android:name="companyname.project2.View.LoginView"></action>
        <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
    </activity>

我不認為默認情況下會合並清單。 因此,如果您在構建時查看 obj/Debug/android 中生成的 AndroidManifest.xml,您可能會注意到缺少 LoginView 條目。

You can try adding the following NuGet package to your App project and see if that will merge the manifests: https://www.nuget.org/packages/Xamarin.Android.ManifestMerger/1.0.0-preview03

否則,您將需要在您的應用程序清單中為 LoginView 手動添加相同的條目。

編輯:

確保使用正確的意圖。 這對我來說很好:

var intent = new Intent(this, typeof(LoginView));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM