繁体   English   中英

如何使用Xamarin.Forms通过单击按钮打开Goog​​le Play图书?

[英]How to open Google play books from a button click using Xamarin.Forms?

我正在Xamarin.Forms中开发一个具有用于书籍的按钮的应用程序。 如果操作系统是iOS,我希望它打开iBooks,如果是android,我希望它打开Goog​​le Play图书

我的iBooks部分已关闭,但是如何从应用程序的android版本中单击按钮打开Goog​​le 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>

这是我的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;
     }
}

任何帮助都将是惊人的!

预先感谢!

在android平台上,您应该知道google play book的软件包名称,并检查是否已安装。

在PCL部分中,无法通过Device.OpenUri("packagename");来实现Device.OpenUri("packagename");

您应该使用依赖项服务来打开Goog​​le 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;
            }
        }

在我的Android设备上,我已经安装了Google Play图书: 在此处输入图片说明

暂无
暂无

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

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