我正在开发一个带有书籍按钮的Xamarin.Forms应用程序。我想要它打开iBooks,如果操作系统是iOS和谷歌玩书,如果它是Android
我将iBooks Part放下,但如何从Android应用程序版本中的按钮点击打开Google Play书?
继承我的xaml代码:
<StackLayout Orientation="Horizontal" MinimumHeightRequest="30" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<Button Image="books.png" HorizontalOptions="CenterAndExpand" BackgroundColor="Transparent" Clicked="OpenBooks" />
<Button Image="settings.png" HorizontalOptions="EndAndExpand" BorderColor="Transparent" BackgroundColor="Transparent" Clicked="gotosettings" />
</StackLayout>
Heres My C#代码:
public void OpenBooks(object sender, EventArgs e)
{
switch (Device.OS)
{
case TargetPlatform.iOS:
Device.OpenUri(new Uri("itms-books"));
break;
case TargetPlatform.Android:
//open google play books code here
break;
}
}
任何帮助都会很棒!
提前感谢!
答案 0 :(得分:1)
在Android平台上,你应该知道google play books的包名,并检查它是否已经安装。
在PCL部分,您无法通过Device.OpenUri("packagename");
您应该使用相关服务打开Google Play图书应用,并且Google Play图书应用包名称为com.google.android.apps.books
:
在PCL端定义界面:
namespace OpenBooks_Demo
{
public interface OpenBookInterface
{
void openBooks();
}
}
在andorid端实现界面:
[assembly: Xamarin.Forms.Dependency(typeof(OpenBookImp))]
namespace OpenBooks_Demo.Droid
{
public class OpenBookImp : Java.Lang.Object, OpenBookInterface
{
public OpenBookImp() { }
public void openBooks()
{
var ctx = Forms.Context;
Intent launchIntent = new Intent();
launchIntent = ctx.PackageManager.GetLaunchIntentForPackage("com.google.android.apps.books");
if (launchIntent != null)
{
ctx.StartActivity(launchIntent);//null pointer check in case package name was not found
}
else
{
try
{
ctx.StartActivity(new Intent(Intent.ActionView, Android.Net.Uri.Parse("market://details?id=" + "com.google.android.apps.books")));
}
catch (Exception e)
{
ctx.StartActivity(new Intent(Intent.ActionView, Android.Net.Uri.Parse("https://play.google.com/store/apps/details?id=" + "com.google.android.apps.books")));
}
}
}
}
}
然后在PCL端调用openBooks方法:
public void OpenBooks(object sender, EventArgs e)
{
switch (Device.OS)
{
case TargetPlatform.iOS:
Device.OpenUri(new Uri("itms-books"));
break;
case TargetPlatform.Android:
DependencyService.Get<OpenBookInterface>().openBooks();
break;
}
}