【问题标题】:Use android package manager in Xamarin.Forms在 Xamarin.Forms 中使用 android 包管理器
【发布时间】:2019-08-20 04:26:37
【问题描述】:

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

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

【问题讨论】:

  • 我在网上看过,依赖注入可以用于这样的目的,但不知道怎么做。

标签: c# android xamarin.forms


【解决方案1】:

是的,您可以使用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);

【讨论】:

  • 可以在MainActivity中定义一个静态变量,例如:public static MainActivityInstance;然后在OnCreate方法中:Instance = this;可以使用MainActivity.Instance.PackageManager,也可以使用CurrentActivity Nuget 获取当前活动,然后调用 currentActivity.PackageManager
  • 我也会试试的。现在我使用 Android.App.Application.Context.PackageManager 在非活动类中获取包管理器。
猜你喜欢
  • 2014-09-21
  • 2012-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多