从 Xamarin.Forms 中的依赖项服务将 UWP 页面推送到 ContentPage

问题描述

我正在开发一个 xamarin.forms 应用程序。在其中我有一个依赖服务,我想从中打开本机 UWP 页面 (Windows.UI.Xaml.Controls.Page)。这就像将页面推送到 ContentPage 上,但我找不到合适的代码来做到这一点。请提出建议。

解决方法

从 Xamarin.Forms 中的依赖项服务将 UWP 页面推送到 ContentPage 上

当然,您可以使用 DependencyService 打开原生 UWP 页面,我在下面发布了实现。

界面

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

实施

[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();
            }
            
        }
    }
}

使用

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

返回 UWP 页面中的表单。

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

}