简体   繁体   English

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

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

I am working on a app in Xamarin.Forms that has a button for books. 我正在Xamarin.Forms中开发一个具有用于书籍的按钮的应用程序。 I want it to open iBooks if the OS is iOS and Google play books if it is android 如果操作系统是iOS,我希望它打开iBooks,如果是android,我希望它打开Goog​​le Play图书

I have the iBooks Part down but How would I open google play books from the button click in the android version of the app? 我的iBooks部分已关闭,但是如何从应用程序的android版本中单击按钮打开Goog​​le Play图书?

Heres my xaml code: 这是我的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# code: 这是我的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;
     }
}

any help would be amazing! 任何帮助都将是惊人的!

Thank in advance! 预先感谢!

In the android platform you should know the package name of google play books and check if it is installed already. 在android平台上,您应该知道google play book的软件包名称,并检查是否已安装。

In the PCL part you can not achieve it by Device.OpenUri("packagename"); 在PCL部分中,无法通过Device.OpenUri("packagename");来实现Device.OpenUri("packagename");

You should use the dependency service to open the google play books app, and The google play books app package name is com.google.android.apps.books : 您应该使用依赖项服务来打开Goog​​le Play图书应用,而Google Play图书应用的软件包名称为com.google.android.apps.books

In the PCL side define the interface: 在PCL端定义接口:

namespace OpenBooks_Demo
{
    public interface OpenBookInterface
    {
        void openBooks();
    }
}

In the andorid side implements the interface: 在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")));
                }
            }
        }
    }
}

And then call the openBooks method in the PCL side: 然后在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;
            }
        }

In my android device I have installed the google play books: 在我的Android设备上,我已经安装了Google Play图书: 在此处输入图片说明

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

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