首页 > 解决方案 > 在 Xamarin.Forms 中使用 android 包管理器

问题描述

尝试检查特定应用程序是否安装在 android 手机中。

在android studio中,我使用包管理器来获取应用程序的安装状态。但我需要使用 Xamarin.Forms 进行开发。

标签: c#androidxamarin.forms

解决方案


是的,您可以使用DependencyService来实现:

首先,定义一个接口

public interface IsInstallApplication
 {
     bool IsInstall(string packageName);
 }

然后在Droid.project创建一个实现接口的类:

[assembly: Dependency(typeof(AndroidIsInstallApplication))]// do not miss the line
namespace App18.Droid
{
   class AndroidIsInstallApplication : IsInstallApplication
     {
        public bool IsInstall(string packageName)
         {
            ... //here you could use Package manager to get the installation status of the application like in native android
            return true;
         }
     }
}

最后你可以在你的页面中调用它:

DependencyService.Get<IsInstallApplication>().IsInstall(packageName);

推荐阅读