Xamarin Forms:如何从另一个应用程序打开一个应用程序? [英] Xamarin Forms: How to open an app from another app?

查看:100
本文介绍了Xamarin Forms:如何从另一个应用程序打开一个应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个使用Xamarin.Forms开发的应用程序(A和B).我需要从应用程序B中打开应用程序A.

I have 2 applications ( A and B ) developed using Xamarin.Forms. I need to open app A from app B.

我按照以下 在视图中

var appname = @"otherappId";
var result = await DependencyService.Get<IAppHandler>().LaunchApp(appname);

界面

public interface IAppHandler
{
    Task<bool> LaunchApp(string uri);
}

Android

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

  1. 对于android系统,我正在运行 System.NullReferenceException:'对象引用未设置为对象的实例.'我不知道代码中的应用名称是什么?我已经尝试过使用包名称.

  1. For android, I am getting System.NullReferenceException: 'Object reference not set to an instance of an object.' when running the project. I don't know what is app name in the code? I have tried with the package name.

此外,如果设备上未安装android应用,则需要打开Play商店下载该应用.

Also if the android app is not installed on the device, it needs to open the Play Store to download the app.

我没有测试iOS部件,因为Mac不可用.上面的代码适用于iOS吗?另外,如果未在iPhone上安装该应用程序,则需要打开AppStore来下载该应用程序.

I didn't test the iOS part because Mac is not available. Is the above code work for iOS? Also if the app is not installed on the iPhone, need to open the AppStore to download the app.

此外,我想为UWP实现相同的功能.

Also, I like to implement the same for UWP.

参考: https://forums.xamarin.com/Discussion/92666/检测并打开设备上的另一个应用程序是否可以打开另一个xamarin.form在我的应用程序中使用该应用程序?

推荐答案

Xamarin.Forms.Forms.Context 从XF 2.5开始已过时.因此,您可以使用 Android.App.Application.Context 代替它,也可以使用插件

Xamarin.Forms.Forms.Context is out of date since XF 2.5 . So you could use Android.App.Application.Context instead of it or use the plugin Plugin.CurrentActivity

因此您可以像以下操作一样在Android中修改代码

So you could modify the code in Android like following

using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using App14;
using App14.Droid;
using Xamarin.Forms;

[assembly: Dependency(typeof(OpenAppAndroid))]
namespace App14.Droid
{
    public class OpenAppAndroid : IAppHandler
    {
        public Task<bool> LaunchApp(string packageName)
        {


            bool result = false;

            try
            {

                PackageManager pm = Android.App.Application.Context.PackageManager;

                if (IsAppInstalled(packageName))
                {
                    
                    Intent intent = pm.GetLaunchIntentForPackage(packageName);
                    if (intent != null)
                    {
                      
                        intent.SetFlags(ActivityFlags.NewTask);
                        Android.App.Application.Context.StartActivity(intent);
                    }
                }

                else
                {
                  
                    Intent intent = pm.GetLaunchIntentForPackage("the package name of play store on your device");
                    if (intent != null)
                    {

                        intent.SetFlags(ActivityFlags.NewTask);
                        Android.App.Application.Context.StartActivity(intent);
                    }
                }
            }
            catch (ActivityNotFoundException)
            {
                result = false;
            }

            return Task.FromResult(result);
        }


        private bool IsAppInstalled(string packageName)
        {
            PackageManager pm = Android.App.Application.Context.PackageManager;
            bool installed = false;
            try
            {
                pm.GetPackageInfo(packageName, PackageInfoFlags.Activities);
                installed = true;
            }
            catch (PackageManager.NameNotFoundException e)
            {
                installed = false;
            }

            return installed;
        }

    }
}

对于iOS,您可以参考从Xamarin Forms App启动另一个IOS应用.

For iOS you could refer Launch Another IOS App from Xamarin Forms App .

这篇关于Xamarin Forms:如何从另一个应用程序打开一个应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆