Narayana Dvl
Narayana Dvl

Reputation: 211

how to call platform specific page from xamarin.forms

I am new to the xamarin app development, I have some questions :

  1. Is it possible to call the platform specific page from the Xamarin.Form page?
  2. Can I do Page Navigation from view model?

Let me explain clearly.

I have a Page XFView.xaml , XFView.xaml.cs and XFViewModel.cs in PCL (Xamarin.Forms) project from XFView.xaml.cs or XFViewModel.cs i want to call MAActivity.cs page which is present in Xamarin.Andriod project

I tried a lot but got getting any idea.

Upvotes: 10

Views: 7209

Answers (1)

Alex Lau
Alex Lau

Reputation: 1697

Whenever you would like to call something from native (MAActivity.cs in your case), you have to use DependencyService.

For example:

  1. Setup dependency service

    // PCL Project
    public interface INativePages
    {
        void StartMA();
    }
    
    // MonoDroid Project
    [assembly: Dependency(typeof(NativePages))]
    
    public class NativePages : INativePages
    {
        public void StartMA()
        {
            var intent = new Intent(Forms.Context, typeof(MAActivity));
            Forms.Context.StartActivity(intent);
        }
    }
    
  2. Invoke in PCL

    // PCL Project
    DependencyService.Get<INativePages>().StartMA();
    

Upvotes: 11

Related Questions