在Xamarin Forms中启动Android活动? [英] Start an Android activity in Xamarin Forms?

查看:102
本文介绍了在Xamarin Forms中启动Android活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码在我的应用中使用默认的Android pdf查看器查看托管的PDF文件

I am trying to view a hosted PDF file with the default Android pdf viewer in my App with the following code

            var intent = new Intent(Intent.ActionView);

            intent.SetDataAndType(Uri.Parse("http://sample/url.pdf"), "application/pdf");
            intent.SetFlags(ActivityFlags.ClearTop);
            Android.Content.Context.StartActivity(intent);

我已经在Native项目中使用了此代码,因此我知道它可以在Android活动中使用.对于Xamarin Forms,我是否有办法从内容页面启动Android活动,反之亦然?

I have used this code in a Native project, so I know that it works within an Android activity. For Xamarin Forms, is there a way for me to start an Android activity from a content page, and vice versa?

推荐答案

您可以使用

You can use DependencyService to implement this function:

INativePages:

 public interface INativePages
{
    void StartActivityInAndroid();
}

Xamarin.Android中实现接口:

[assembly: Xamarin.Forms.Dependency(typeof(NativePages))]
namespace PivotView.Droid
{
    public class NativePages : INativePages
    {
        public NativePages()
        {
        }

        public void StartAc()
        {
            var intent = new Intent(Forms.Context, typeof(YourActivity));
            Forms.Context.StartActivity(intent);
        }

    }
}

在PCL中启动Android Activity:

Start an Android Activity in PCL :

    private void Button_Clicked(object sender, EventArgs e)
    {
        Xamarin.Forms.DependencyService.Register<INativePages>();
        DependencyService.Get<INativePages>().StartAc();
    }

这篇关于在Xamarin Forms中启动Android活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆