为UWP app指定启动监视器

时间:2017-02-20 07:53:38

标签: c# uwp

在UWP桌面应用程序中,是否有办法强制应用程序在特定监视器上打开。 (在我的情况下,我有一台笔记本电脑和额外的屏幕连接到笔记本电脑,所以我想在代码中指定启动屏幕)

我在winforms中使用了以下代码:

Screen[] screens = Screen.AllScreens;

if (Screen.AllScreens.Length == 1)
            {
              Application.Run(new frmMain());
            }
else
{
    //select largest monitor and set new monitor
    Rectangle bounds = screens[LargestScreen].Bounds;
    frm.SetBounds(bounds.X, bounds.Y, bounds.Width, bounds.Height);
    frm.StartPosition = FormStartPosition.Manual;

    Application.Run(frm);
}

知道如何在UWP应用程序中实现上述功能吗?

1 个答案:

答案 0 :(得分:1)

您应该可以为应用创建multiple views,并使用ProjectionManager类和方法StartProjectingAsync在另一个屏幕上显示辅助视图。您可以在OnLaunched方法中执行此操作,然后在应用启动辅助视图后,您将在屏幕上显示所需内容。

protected override async void OnLaunched(LaunchActivatedEventArgs e)
{ 
    if (System.Diagnostics.Debugger.IsAttached)
    {
        this.DebugSettings.EnableFrameRateCounter = true;
    } 
    Frame rootFrame = Window.Current.Content as Frame;  
    if (rootFrame == null)
    {
        // Create a Frame to act as the navigation context and navigate to the first page
        rootFrame = new Frame();
        rootFrame.NavigationFailed += OnNavigationFailed;
        if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
        {
            //TODO: Load state from previously suspended application
        }
        // Place the frame in the current Window
        Window.Current.Content = rootFrame;
    }
   ///Get all the screens.
    String projectorSelectorQuery = ProjectionManager.GetDeviceSelector();
    var outputDevices = await DeviceInformation.FindAllAsync(projectorSelectorQuery);
    //if(outputDevices.Count==1)
    //{

    //}
    int thisViewId;
    int newViewId = 0;
    ///Choose one screen for display .
    DeviceInformation showDevice = outputDevices[1];
    thisViewId = ApplicationView.GetForCurrentView().Id;
    if (e.PrelaunchActivated == false)
    {
        if (rootFrame.Content == null)
        { 
        }          
        Window.Current.Activate();
    }
    ///Create a new view
    await CoreApplication.CreateNewView().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        Frame frame = new Frame();
        frame.Navigate(typeof(MainPage), null);
        Window.Current.Content = frame;          
        Window.Current.Activate();
        newViewId = ApplicationView.GetForCurrentView().Id;
    });
    await ProjectionManager.StartProjectingAsync(newViewId, thisViewId, showDevice); 

}

但似乎第一个视图无法直接显示在其他屏幕上,因为StartProjectingAsync方法需要新的视图ID。应用启动时创建的第一个视图称为主视图。你不创建这个视图;它是由应用程序创建的。主视图的线程充当应用程序的管理器,所有应用程序激活事件都在此线程上传递。并且主视图无法关闭,因此主要的第一个视图仍将保留在第一个屏幕上。

详情请参阅Projection official sample