[英]How to open a Xamarin Forms page from a Xamarin Droid Activity from a onclick listener
[英]How to Reset a Xamarin forms Navigation stack from a Xamarin droid Assist activity
Helo,我正在為我的應用程序添加輔助功能,當用戶按下主頁按鈕時,會打開一個帶有卡片的android活動,用戶可以單擊該卡片,然后打開有問題的xamarin表單頁面,例如當數學卡片是單擊MathPage xamarin表單頁面會打開此功能,但如果我在后台運行xamarin表單應用程序,則無論單擊什么按鈕,它都會加載主屏幕布局。 如果我關閉了多任務處理的xamarin表單應用程序,並按住主頁按鈕並單擊數學卡,它將打開MathPage。
這是我的android活動代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Xamarin.Forms.Platform.Android;
namespace Appname.Droid
{
[Activity(LaunchMode = LaunchMode.SingleInstance, Theme = "@style/Theme.Transparent")]
[IntentFilter(new[] { Intent.ActionAssist }, Categories = new[] { Intent.CategoryDefault })]
public class ToolBelt : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.ToolBelt);
}
//Code That Opens The Math Part!
[Java.Interop.Export("math")] // The value found in android:onClick attribute.
public void btnOneClick4(View v) // Does not need to match value in above attribute.
{
var intent = new Intent(ApplicationContext, typeof(MainActivity));
intent.PutExtra("page", "Math");
StartActivity(intent);
}
//Code That Opens The Science Part!
[Java.Interop.Export("science")] // The value found in android:onClick attribute.
public void btnOneClick9(View v) // Does not need to match value in above attribute.
{
var intent = new Intent(ApplicationContext, typeof(MainActivity));
intent.PutExtra("page", "Science");
StartActivity(intent);
}
//Code That Opens The Handwriting Part!
[Java.Interop.Export("english")] // The value found in android:onClick attribute.
public void btnOneClick10(View v) // Does not need to match value in above attribute.
{
var intent = new Intent(ApplicationContext, typeof(MainActivity));
intent.PutExtra("page", "Handwriten");
StartActivity(intent);
}
//Code That Opens The Flascards Part!
[Java.Interop.Export("flashcard")] // The value found in android:onClick attribute.
public void btnOneClick11(View v) // Does not need to match value in above attribute.
{
var intent = new Intent(ApplicationContext, typeof(MainActivity));
intent.PutExtra("page", "Flashcards");
StartActivity(intent);
}
//Code That Opens The Internet App!
[Java.Interop.Export("web")] // The value found in android:onClick attribute.
public void btnOneClick8(View v) // Does not need to match value in above attribute.
{
var uri = Android.Net.Uri.Parse("http://www.google.com");
var intent = new Intent(Intent.ActionView, uri);
StartActivity(intent);
}
//Code That Opens The Gmail App!
[Java.Interop.Export("email")] // The value found in android:onClick attribute.
public void btnOneClick3(View v) // Does not need to match value in above attribute.
{
var intent = PackageManager.GetLaunchIntentForPackage("com.google.android.gm");
StartActivity(intent);
}
//Code That Opens The Books App!
[Java.Interop.Export("books")] // The value found in android:onClick attribute.
public void btnOneClick5(View v) // Does not need to match value in above attribute.
{
var intent = PackageManager.GetLaunchIntentForPackage("com.google.android.apps.books");
StartActivity(intent);
}
}
}
這是MainActivity機器人代碼:
using System;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using AppName.Droid;
using AppName.Data;
[assembly: Xamarin.Forms.Dependency(typeof(MainActivity))]
namespace AppName.Droid
{
[Activity(Label = "AppName", Icon = "@drawable/ic_launcher", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity, SchoolTools.PackageInterface
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
var page = Intent.GetStringExtra("page");
LoadApplication(new App(page));
}
public IList<Apps> GetInstalledApps()
{
IList<Apps> apps = new List<Apps>();
var pkgInfos = global::Xamarin.Forms.Forms.Context.PackageManager.GetInstalledPackages(PackageInfoFlags.Activities);
foreach(var pi in pkgInfos)
{
// skip system packages
if (pi.ApplicationInfo.DataDir.StartsWith("/data/user/"))
{
Apps app = new Apps(pi.ApplicationInfo.LoadLabel(global::Xamarin.Forms.Forms.Context.PackageManager).ToString(), pi.PackageName);
if (!apps.Contains(app))
{
apps.Add(app);
}
}
}
return apps;
}
public bool Launch(string package)
{
var intent = Application.Context.PackageManager.GetLaunchIntentForPackage(package);
bool retVal = IsIntentAvailable(intent);
if (retVal)
{
intent.AddFlags(ActivityFlags.NewTask);
Application.Context.StartActivity(intent);
}
return retVal;
}
private static bool IsIntentAvailable(Intent intent)
{
return intent != null && Application.Context.PackageManager.QueryIntentActivities(intent, 0).Count != 0;
}
}
}
這是我的app.cs代碼:
using System;
using Xamarin.Forms;
namespace AppName
{
public class App : Application
{
public App(string pageName = "AppNameHome")
{
switch (pageName)
{
case "Math":
MainPage = new NavigationPage(new MathPage());
break;
case "Science":
MainPage = new NavigationPage(new Science.ScienceToolsPage());
break;
case "Handwriten":
MainPage = new NavigationPage(new Handwriting.HandwritingToolsPage());
break;
case "Flashcards":
MainPage = new NavigationPage(new Flashcards.FlashCardHome());
break;
default:
MainPage = new NavigationPage(new AppNameHome());
break;
}
}
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
}
}
}
任何幫助都將是驚人的!
提前致謝 :)
我發現我也需要將SingleInstance添加到主要活動中的問題是nohistory = true,這是解決此問題所需的代碼:
[Activity(LaunchMode = LaunchMode.SingleInstance, NoHistory = true,
Label = "AppName", Icon = "@drawable/ic_launcher", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.