简体   繁体   English

XAMARIN Android 应用程序,如何在 MVVM 中获取已安装应用程序的列表作为服务?

[英]XAMARIN Android App, how do I get a list of installed apps as a service in MVVM?

  1. I want to load a list of installed applications on an Android phone.我想在 Android 手机上加载已安装应用程序的列表。
  2. I am trying to build a Xamarin Forms application leveraging the MVVM framework.我正在尝试利用 MVVM 框架构建一个 Xamarin Forms 应用程序。
  3. I am using Visual Studio 2019 Pro and Xamarin Forms我正在使用 Visual Studio 2019 Pro 和 Xamarin Forms

I keep finding articles talking about Android.App.Application.Context.PackageManager.GetInstalledApplications however I have not been able to figure out how to get get a reference to it.我一直在寻找有关Android.App.Application.Context.PackageManager.GetInstalledApplications的文章,但是我无法弄清楚如何获得对它的引用。

You can implement this using dependency service.您可以使用依赖服务来实现这一点。 I will give in detail so others who are new to Xamarin also might understand.我将详细介绍,以便其他不熟悉 Xamarin 的人也可以理解。

First we will create our model.首先,我们将创建 model。 You can name this as InApp.cs in Shared directory.您可以在 Shared 目录中将其命名为InApp.cs

public class InApp
{
    public string AppName { get; set; }
    public string PackageName { get; set; }

    public InApp(string appName, string packageName)
    {
        AppName = appName;
        PackageName = packageName;
    }
}

Now we can create our dependency service in Android Folder.现在我们可以在Android文件夹中创建我们的依赖服务。 Name this as AndroidService.cs .将此命名为AndroidService.cs

public class AndroidService : IAndroidService
{
    public List<InApp> GetIntalledApps()
    {
        List<InApp> inApps = new List<InApp>();
        IList<ApplicationInfo> apps = Android.App.Application.Context.PackageManager.GetInstalledApplications(PackageInfoFlags.MatchAll);
        for (int i = 0; i < apps.Count; i++)
        {
            inApps.Add(new InApp(apps[i].LoadLabel(Android.App.Application.Context.PackageManager), apps[i].PackageName));
        }
        return inApps;
    }
}

The above code will get the installed apps, then creates a List of the model which we've created above and returns it.上面的代码将获取已安装的应用程序,然后创建我们在上面创建的 model 的List并返回它。

At run time, Xamarin should know where to look for the dependency service, so for that we should add this above the name space of AndroidService class we've created above.在运行时,Xamarin 应该知道在哪里寻找依赖服务,因此我们应该将其添加到我们上面创建的AndroidService class 的名称空间之上。

[assembly: Xamarin.Forms.Dependency(typeof(AndroidService))]

IAndroidService is the interface which will access the Android folder AndroidService class at runtime. IAndroidService是在运行时访问Android文件夹AndroidService class 的接口。 We will create this class in Shared directory.我们将在 Shared 目录中创建这个 class。 We can name this as IAndroidService.cs .我们可以将其命名为IAndroidService.cs

public interface IAndroidService
{
    List<InApp> GetIntalledApps();
}

Now we have completed our dependency service implementation.现在我们已经完成了依赖服务的实现。 Next part is to create a ListView and add the returned installed apps list from our Android service.下一部分是创建一个ListView并添加从我们的 Android 服务返回的已安装应用程序列表。

Since we are doing this in MVVM , we will create a view model now.由于我们在MVVM中执行此操作,我们现在将创建一个视图 model。

Create InstalledAppViewModel.cs in Shared directory.在 Shared 目录中创建InstalledAppViewModel.cs

public class InstalledAppViewModel: INotifyPropertyChanged
{
    private ObservableCollection<InApp> installedApps;
    public ObservableCollection<InApp> InstalledApps
    {
        get { return installedApps; }
        set
        {

            installedApps = value;
        }
    }

    public InstalledAppViewModel()
    {
        List<InApp> listOfInstalledApps = DependencyService.Get<IAndroidService>().GetIntalledApps();
        InstalledApps = new ObservableCollection<InApp>(listOfInstalledApps);
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

Above, we have done the dependency injection and added the returned values from the GetInstalledApps method to our Observable list.上面,我们已经完成了依赖注入并将GetInstalledApps方法的返回值添加到我们的Observable列表中。

Now in your MainPage.Xaml.Cs bind the view model.现在在您的MainPage.Xaml.Cs绑定视图 model。

BindingContext = new InstalledAppViewModel();

Add the list view in your MainPage.Xaml .MainPage.Xaml中添加列表视图。

 <ListView ItemsSource="{Binding InstalledApps}">
<ListView.ItemTemplate>
    <DataTemplate>
        <ViewCell>
            <StackLayout Orientation="Vertical">
                <StackLayout Orientation="Horizontal" VerticalOptions="CenterAndExpand">
                    <Label Text="{Binding AppName}" FontSize="Medium" />
                </StackLayout>
            </StackLayout>
        </ViewCell>
    </DataTemplate>
</ListView.ItemTemplate>

In ItemSource of the list view attribute, you are binding the Observable list which we have created in the View Model class.在列表视图属性的ItemSource中,您正在绑定我们在视图 Model class 中创建的Observable列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Xamarin Android:使用图标获取已安装的应用列表 - Xamarin Android: Get installed app list with icons 如何获取 Android 设备上已安装应用程序的列表 - How to get a list of installed apps on Android device 如何获取 Android 11 中已安装应用程序的列表 - How to get a list of installed apps in Android 11 如何将模块安装为服务 - Android - How do I get a module installed as a service - Android 如何列出另一个应用程序中所有已安装的Android应用程序的权限 - How do you list the permissions for all installed Android Apps from another App 为什么在 Flutter 中无法获取 Android 的已安装应用程序列表? - Why can't I get a list of installed apps for Android in Flutter? Android:如何从已安装的用户应用程序中创建复选框列表 - Android: how to do a checkbox list from installed user apps 如何在android中获取具有大小的已安装应用程序列表 - How to get list of installed app with size in android 使用cordova如何获得Android设备上已安装的应用程序列表? - Using cordova how can i get list of installed apps on android device? Android:如何获取使用Internet访问的已安装应用程序列表-PackageManager - Android : How to get the List of Installed apps that use Internet Access - PackageManager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM