如何解决从 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();
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。