2017-02-20 43 views
0

在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應用程序中實現上述內容?

回答

0

您應該可以爲應用創建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

+0

非常感謝您回答我的問題。 – user3385105