如何在WPF中管理两个单独的显示器?

时间:2019-04-18 22:25:12

标签: wpf visual-studio caliburn.micro

我正在编写一个应用程序,它将允许用户从触摸屏显示器上的一系列地图中进行选择,然后这些地图也将显示在更大的壁挂式屏幕上。用户将能够在地图上平移/缩放/旋转,我希望壁挂式屏幕能够显示与触摸屏同步的更改。

管理两个显示器的好方法是什么?

当前,我已将应用程序设置为MVVM格式,并且正在使用Caliburn.Micro。

每个地图都在其自己的UserControl中,并使用ShellViewModel中的Conductor和ActivateItem在ShellView的ContentControl中将其激活。我希望活动项目也显示在单独的窗口中(在壁挂式屏幕上)。

这是到目前为止的代码:

ShellView.xaml:

    <Grid>
        <!--The Content control shows which MapView is currently active-->
        <ContentControl x:Name="ActiveItem"/>
            <StackPanel>
                <TextBlock Text="Select a map.">
                <ComboBox>
                    <Button x:Name="LoadMap1">Map1</Button>
                    <Button x:Name="LoadMap2">Map2</Button>
                    <Button x:Name="LoadMap3">Map3</Button>
                </ComboBox>
            </StackPanel>
    </Grid>

ShellViewModel.cs:

    public class ShellViewModel : Conductor<object>
    {
        public ShellViewModel()
        {

        }

        public void LoadMap1()
        {
            ActivateItem(new MapOneViewModel());
        }

        public void LoadMap2()
        {
            ActivateItem(new MapTwoViewModel());
        }

        public void LoadMap3()
        {
            ActivateItem(new MapThreeViewModel());
        }
    }

我不知道这是否是设置此设置的最佳方法,但它非常适合在ShellView上加载地图。我真的只需要在壁挂式显示器的另一个窗口中显示相同的东西

感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

假设两个监视器都连接到同一设备,则可以使用Forms.Screen获取每个监视器的边界。然后,将窗口设置为相同的边界,添加一个Loaded事件处理函数以最大化它们并调用Show()

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        var primaryScreen = System.Windows.Forms.Screen.PrimaryScreen;
        this.MainWindow = new Window();
        this.MainWindow.Content = new TextBlock { Text = "This is the primary display." };
        this.MainWindow.Left = primaryScreen.Bounds.Left;
        this.MainWindow.Top = primaryScreen.Bounds.Top;
        this.MainWindow.Width = primaryScreen.Bounds.Width;
        this.MainWindow.Height = primaryScreen.Bounds.Height;
        this.MainWindow.WindowState = WindowState.Normal;
        this.MainWindow.Loaded += (_s, _e) => this.MainWindow.WindowState = WindowState.Maximized;
        this.MainWindow.Show();

        var secondaryScreen = System.Windows.Forms.Screen.AllScreens.First(screen => screen != primaryScreen);
        var secondaryWindow = new Window();
        secondaryWindow.Content = new TextBlock { Text = "This is the secondary display." };
        secondaryWindow.Left = secondaryScreen.Bounds.Left;
        secondaryWindow.Top = secondaryScreen.Bounds.Top;
        secondaryWindow.Width = secondaryScreen.Bounds.Width;
        secondaryWindow.Height = secondaryScreen.Bounds.Height;
        secondaryWindow.WindowState = WindowState.Normal;
        secondaryWindow.Loaded += (_s, _e) => secondaryWindow.WindowState = WindowState.Maximized;
        secondaryWindow.Show();

    }
}
相关问题