【发布时间】:2020-12-15 01:20:00
【问题描述】:
我有 2 个使用 Xamarin.Forms 开发的应用程序(A 和 B)。我需要从应用 B 打开应用 A。
我已经按照thread进行了如下尝试:
在视图中
var appname = @"otherappId";
var result = await DependencyService.Get<IAppHandler>().LaunchApp(appname);
界面
public interface IAppHandler
{
Task<bool> LaunchApp(string uri);
}
安卓
[Activity(Label = "OpenAppAndroid")]
public class OpenAppAndroid : Activity, IAppHandler
{
public Task<bool> LaunchApp(string uri)
{
bool result = false;
try
{
var aUri = Android.Net.Uri.Parse(uri.ToString());
var intent = new Intent(Intent.ActionView, aUri);
Xamarin.Forms.Forms.Context.StartActivity(intent);
result = true;
}
catch (ActivityNotFoundException)
{
result = false;
}
return Task.FromResult(result);
}
}
IOS
public class OpenAppiOS : IAppHandler
{
public Task<bool> LaunchApp(string uri)
{
try
{
var canOpen = UIApplication.SharedApplication.CanOpenUrl(new NSUrl(uri));
if (!canOpen)
return Task.FromResult(false);
return Task.FromResult(UIApplication.SharedApplication.OpenUrl(new NSUrl(uri)));
}
catch (Exception ex)
{
return Task.FromResult(false);
}
}
-
对于 android,我在运行项目时收到
System.NullReferenceException: 'Object reference not set to an instance of an object.'。我不知道代码中的应用名称是什么?我已经尝试过使用包名。 -
另外如果设备上没有安装安卓应用,需要打开Play Store下载应用。
-
我没有测试 iOS 部分,因为 Mac 不可用。上面的代码适用于 iOS 吗?另外如果iPhone上没有安装应用,需要打开AppStore下载应用。
-
另外,我喜欢为 UWP 实现相同的功能。
参考: https://forums.xamarin.com/discussion/92666/detect-and-open-another-app-on-device Is it possible to open another app in my app with xamarin.form?
【问题讨论】:
标签: android ios xamarin.forms uwp