Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
571 views
in Technique[技术] by (71.8m points)

xamarin.android - Start an Android activity in Xamarin Forms?

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);

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use DependencyService to implement this function:

INativePages in PCL:

 public interface INativePages
{
    void StartActivityInAndroid();
}

Implement the interface in 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);
        }

    }
}

Start an Android Activity in PCL :

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...