Mannu
Mannu

Reputation: 259

Push UWP Page over ContentPage from dependency service in Xamarin.Forms

I am working on a xamarin.forms application. In which I have a dependency service from where I want to open a native UWP page (Windows.UI.Xaml.Controls.Page). It will be like pushing a Page over ContentPage but I am not able to find a suitable code to do that. please suggest something.

Upvotes: 0

Views: 124

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32775

Push UWP Page over ContentPage from dependency service in Xamarin.Forms

Sure, you could use DependencyService to open native UWP page, I post the implementation below.

Interface

public interface IOpenNativeInterface
{
    void OpenNavtivePage();
    void BackToFormsPage();
}

Implementation

[assembly:Dependency(typeof(OpenNativeImplemention))]

namespace CallNativePage.UWP
{
    public class OpenNativeImplemention : IOpenNativeInterface
    {
        Windows.UI.Xaml.Controls.Frame rootFrame = Window.Current.Content as Windows.UI.Xaml.Controls.Frame;
        public void BackToFormsPage()
        {

            if (rootFrame != null)
            {
                if (rootFrame.CanGoBack)
                {
                    rootFrame.GoBack();
                }
            }
        }

        public void OpenNavtivePage()
        {
       

            if (rootFrame != null)
            {
                rootFrame.Navigate(typeof(TestPage));
            }
            else
            {
                Window.Current.Content = new TestPage();
            }
            
        }
    }
}

Usage

private void Button_Clicked(object sender, EventArgs e)
{
    DependencyService.Get<IOpenNativeInterface>().OpenNavtivePage();
}

Go back form in UWP Page.

private void Button_Click(object sender, RoutedEventArgs e)
{
    if (Frame.CanGoBack)
    {
        Frame.GoBack();
    }

}

Upvotes: 1

Related Questions