首页 文章

如何使用Xamarin.Forms从按钮单击打开Google Play书籍?

提问于
浏览
0

我正在开发一个带有书籍按钮的Xamarin.Forms应用程序 . 我想要它打开iBooks,如果操作系统是iOS,谷歌玩书,如果它是Android

我有iBooks部分下来,但如何从应用程序的Android版本中的按钮单击打开谷歌播放书籍?

继承我的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;
     }
}

任何帮助都会很棒!

预先感谢!

1 回答

  • 1

    在Android平台上,你应该知道google play books的包名,并检查它是否已经安装 .

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

    您应该使用依赖服务打开Google Play图书应用,并且Google Play图书应用包名称为 com.google.android.apps.books

    In the PCL side define the interface:

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

    In the andorid side implements the interface:

    [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:

    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书籍:
    enter image description here

相关问题