棱镜库 UWP 显示应用的多个视图



我正在尝试为棱镜库 UWP 的应用程序显示多个视图。

我得到了System.NullReferenceExceptionframe.Navigate(typeof(ScreenCapture));如下所示:

async void ExecuteNewWindow()
{
CoreApplicationView newView = CoreApplication.CreateNewView();
int newViewId = 0;
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Frame frame = new Frame();
frame.Navigate(typeof(ScreenCapture));
Window.Current.Content = frame;
// You have to activate the window in order to show it later.
Window.Current.Activate();
newViewId = ApplicationView.GetForCurrentView().Id;
});
bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
}

如何在 uwp 的棱镜库中实现多视图。

你的项目是 Xamarin.Forms,但frame.Navigate(typeof(ScreenCapture));的参数是 uwp 页面类型。我检查了您的代码,ScreenCapture是表单ContentPage.根据你的要求,可以使用依赖项服务调用ExecuteNewWindow并将ExecuteNewWindow放在 UWP 项目中。

接口

public interface INewPageService
{
void  CreateNewPage();
}

实现

[assembly: Dependency(typeof(INewPageService))]
namespace BlankApp1.UWP
{
public class INewPageServiceImplement : INewPageService
{
public async void CreateNewPage()
{
CoreApplicationView newView = CoreApplication.CreateNewView();
int newViewId = 0;
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Frame frame = new Frame();
frame.Navigate(typeof(NewPage)); // uwp page
Window.Current.Content = frame;
// You have to activate the window in order to show it later.
Window.Current.Activate();
newViewId = ApplicationView.GetForCurrentView().Id;
});
bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
}
}
}

用法

DependencyService.Get<INewPageService>(DependencyFetchTarget.NewInstance).CreateNewPage();

请不要忘记在uwp app.xaml.cs文件中注册它。

if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame();
rootFrame.NavigationFailed += OnNavigationFailed;
Xamarin.Forms.Forms.Init(e);
DependencyService.Register<INewPageService, INewPageServiceImplement>();
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application
}
// Place the frame in the current Window
Window.Current.Content = rootFrame;
}

相关内容

最新更新