XAMARIN Android应用程序,如何在MVVM中获取已安装应用程序作为服务的列表? 我想在Android手机上加载已安装应用程序的列表 我正试图利用MVVM框架构建一个Xamarin表单应用程序 我正在使用Visual Studio 2019 Pro和Xamarin表单
我一直在寻找有关Android.App.Application.Context.PackageManager.GetInstalledApplications的文章,但是我还没有弄清楚如何获取对它的引用。您可以使用依赖项服务实现此功能。我将给出详细的信息,以便其他新加入Xamarin的人也能理解 首先,我们将创建我们的模型。您可以在共享目录中将其命名为XAMARIN Android应用程序,如何在MVVM中获取已安装应用程序作为服务的列表? 我想在Android手机上加载已安装应用程序的列表 我正试图利用MVVM框架构建一个Xamarin表单应用程序 我正在使用Visual Studio 2019 Pro和Xamarin表单,android,xamarin.forms,visual-studio-2019,Android,Xamarin.forms,Visual Studio 2019,我一直在寻找有关Android.App.Application.Context.PackageManager.GetInstalledApplications的文章,但是我还没有弄清楚如何获取对它的引用。您可以使用依赖项服务实现此功能。我将给出详细的信息,以便其他新加入Xamarin的人也能理解 首先,我们将创建我们的模型。您可以在共享目录中将其命名为InApp.cs public class InApp { public string AppName { get; set; }
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;
}
}
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;
}
现在我们可以在Android
文件夹中创建依赖项服务。将其命名为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;
}
}
public interface IAndroidService
{
List<InApp> GetIntalledApps();
}
IAndroidService
是在运行时访问Android
文件夹AndroidService
类的界面。我们将在共享目录中创建这个类。我们可以将其命名为IAndroidService.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;
}
}
public interface IAndroidService
{
List<InApp> GetIntalledApps();
}
上面,我们已经完成了依赖项注入,并将GetInstalledApps
方法返回的值添加到了Observable
列表中
现在在MainPage.Xaml.Cs
中绑定视图模型
BindingContext = new InstalledAppViewModel();
在主页.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>
在列表视图属性的
ItemSource
中,您正在绑定我们在视图模型类中创建的Observable
列表。我以前读过这个问题,但这个问题中还有两件事没有解释。1.我在哪里做什么,如何做?它给出了一个答案,但我需要更清楚。在Xamarin项目中似乎也没有这样做。2.你提到的问题/答案是为了当你知道你正在寻找的应用程序的名称时。你是对的,它没有进入我第一眼看到的细节。然而,使用DependencyService从表单访问特定于Android的实现的一般方法是正确的。