コメント
コメントの投稿
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。
namespace AppNameA.Services
{
//DependencyServiceから利用する
public interface IApplicationService
{
void Start(string param);
event EventHandler ReturnParamSetted;
string GetReturnParam();
}
}
namespace AppNameB.Services
{
//DependencyServiceから利用する
public interface IApplicationService
{
//Intentを戻す機能
void ReturnApp(string param);
}
}
[assembly: Dependency(typeof(ApplicationService))]
namespace AppNameA.Droid.Services
{
public class ApplicationService : IApplicationService
{
public static ApplicationService Instance = null;
public event EventHandler ReturnParamSetted;
public static string ReturnParam= String.Empty;
public void Start(string param)
{
//現在のインスタンスを保持しておく
ApplicationService.Instance = this;
//アクティビティを取得する
var activity = (Activity)(Forms.Context);
//呼び出すインテントを作成する
Intent intent = new Intent(Intent.ActionGetContent);
intent.SetType("file/*");
if (!String.IsNullOrEmpty(param))
{
//パラメータをセットする
intent.PutExtra("parametername", param);
}
activity.StartActivityForResult(
Intent.CreateChooser(intent, "Select Backup File"), 12345); //数字は任意の番号。OnActivityResultで識別する番号
}
//戻りのパラメータを取得した際に呼び出す。
public void OnReturnParamSetted()
{
if (ApplicationService.Instance != null)
{
ApplicationService.Instance.ReturnParamSetted(null, new EventArgs());
}
}
//PCLからパラメータを取得する
public string GetReturnParam()
{
return ReturnParam;
}
}
}
protected override void OnActivityResult(int requestCode, Result resultCode, global::Android.Content.Intent data)
{
//アプリBからの戻り
if (requestCode == 12345) //インテントを呼び出した際につけた番号
{
if (resultCode == Result.Ok)
{
if (data.HasExtra("parametername"))
{
//戻り値を取得する
ApplicationService.ReturnParam = data.GetStringExtra("parametername");
ApplicationService.Instance.OnReturnParamSetted();
}
}
return;
}
}
[IntentFilter(new[] { Intent.ActionPick },
Categories = new[] { Intent.CategoryDefault },
DataScheme = "file"
)]
[IntentFilter(new[] { Intent.ActionGetContent },
Categories = new[] { Intent.CategoryDefault,
Intent.CategoryOpenable },
DataMimeType = "*/*"
)]
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);
//Intentを受け取る
Intent intent = this.Intent;
string action = intent.Action;
string param = string.Empty;
if (action != null &&
(action.Equals(Intent.ActionPick) || action.Equals(Intent.ActionGetContent)))
{
if (intent.Data != null)
{
param = intent.Data.ToString();
}
if (intent.HasExtra("parametername"))
{
//アプリAからのパラメータを取得する
param = intent.GetStringExtra("parametername");
}
}
//App.csのコンストラクタをオーバーロードし取得したパラメータを渡す。
LoadApplication(new App(param));
}
}
[assembly: Dependency(typeof(ApplicationService))]
namespace AppNameB.Droid.Services
{
public class ApplicationService : IApplicationService
{
//Intentを戻す機能
public void ReturnApp(string param)
{
//アクティビティを取得する
var activity = (Activity)(Forms.Context);
//アプリAのインテントを作成する
Intent intent = new Intent();
intent.PutExtra("parametername", param);
activity.SetResult(Result.Ok, intent);
activity.Finish();
}
}
}
using AppName.Services;
using Xamarin.Forms;
public class TestPage : ContentPage
{
OnButtonClick(object sender, EventArgs e)
{
//イベントの紐づけ
DependencyService.Get<IApplicationService>().ReturnParamSetted += this.OnParameterSetted;
//アプリBを起動してパラメータを受け渡す。
string param = "test";
DependencyService.Get<IApplicationService>().Start(param);
}
void OnParameterSetted(object sender, EventArgs e)
{
//アプリBでセットしたパラメータを取得する
string param = DependencyService.Get<IApplicationService>().GetReturnParam();
}
}
using AppName.Services;
using Xamarin.Forms;
public class TestPage : ContentPage
{
OnButtonClick(object sender, EventArgs e)
{
//アプリBを終了してアプリAにパラメータを受け渡す。
string param = "returnvalue";
DependencyService.Get<IApplicationService>().ReturnApp(param);
}
}
※名前とタイトルが入力されていないコメントでは他のコメントとの区別ができません。
入力されていないコメントには返信しませんのであらかじめご了承くださいませ。