简体   繁体   中英

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.
  2. I am trying to build a Xamarin Forms application leveraging the MVVM framework.
  3. I am using Visual Studio 2019 Pro and 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.

You can implement this using dependency service. I will give in detail so others who are new to Xamarin also might understand.

First we will create our model. You can name this as InApp.cs in Shared directory.

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. Name this as 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.

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.

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

IAndroidService is the interface which will access the Android folder AndroidService class at runtime. We will create this class in Shared directory. We can name this as 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.

Since we are doing this in MVVM , we will create a view model now.

Create InstalledAppViewModel.cs in Shared directory.

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.

Now in your MainPage.Xaml.Cs bind the view model.

BindingContext = new InstalledAppViewModel();

Add the list view in your 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM