简体   繁体   English

多监视器和一个CPU的WPF应用程序

[英]WPF Application for Multi-Monitor and One CPU

I have developed WPF Application which is now going to be run Multi Monitor Env. 我已经开发了WPF应用程序,该应用程序现在将在Multi Monitor Env中运行。 But I don't know, How to set my application that can run in such Env. 但是我不知道,如何设置可以在这种Env中运行的应用程序。

在此处输入图片说明

My Case is like above image, One CPU and 3 Screen. 我的机箱就像上图,一个CPU和3个屏幕。 Now my client want to run same application in these screens. 现在,我的客户希望在这些屏幕中运行相同的应用程序。 And also for each screen he can operate different operation. 而且对于每个屏幕,他都可以进行不同的操作。

So i want to know is should i start 3 different instance of my application. 所以我想知道是否应该启动我的应用程序的3个不同实例。 but in that case on screen 1 user should not aware about other instance of Screen 2 and Screen 3 user. 但在那种情况下,在屏幕1上,用户应该不知道屏幕2和屏幕3的其他情况。

Or there is any alternate way. 或有其他替代方法。 As i don't know how to work with multiple screen.please help me 由于我不知道如何使用多个屏幕,请帮助我

Khusbhu, 库斯布

I am not sure if you really want to create 3 instances, as this scenario is for only single use who is working on single system, this is because the CPU have only one Keyboard and Mouse, so I don't understand why client what to run on 3 monitors same application. 我不确定您是否真的要创建3个实例,因为这种情况仅适用于在单个系统上工作的一次性用户,这是因为CPU只有一个键盘和鼠标,所以我不明白为什么客户端要在3个监视器上运行相同的应用程序。

If yes that is the case, then yes you will need 3 instances, its simple as 3 programs running on 3 different screens. 如果是的话,那么是的,您将需要3个实例,它很简单,因为3个程序在3个不同的屏幕上运行。

For more idea take a look on this http://www.pugetsystems.com/labs/articles/How-to-Set-Up-Multiple-Monitors-140/ 要了解更多信息,请查看此http://www.pugetsystems.com/labs/articles/How-to-Set-Up-Multiple-Monitors-140/

Thanks Nipun 谢谢Nipun

It seems a little overkill to fire up three instances of the same application if all you want to do is handle multiple monitors. 如果您只想处理多个监视器,则启动同一应用程序的三个实例似乎有点过头了。 It depends on the design of your GUI, but you can use Screen.AllScreens to find out about the screens that the system has and layout your windows accordingly. 这取决于您的GUI的设计,但是您可以使用Screen.AllScreens查找系统所具有的屏幕,并相应地布置窗口。 Screen has a few properties like PrimaryScreen and WorkingArea which will help you with this. 屏幕具有一些属性,例如PrimaryScreenWorkingArea ,它们将帮助您。 I know you are using WPF, so you will need a reference to System.Windows.Forms in your project but I have done this without issues in the past. 我知道您正在使用WPF,因此您将需要在项目中引用System.Windows.Forms ,但过去我这样做没有问题。

Your problem is very simple if you make a MVVM application. 如果创建MVVM应用程序,则问题非常简单。 For 1 ViewModel (the fonctionnality), you can attach many View (UI). 对于1个ViewModel(功能),您可以附加多个View(UI)。 In your case, on a single application, create one instance of a ViewModel and set the DataContext of each View to this viewmodel. 在您的情况下,在单个应用程序上,创建一个ViewModel实例,并将每个View的DataContext设置为此视图模型。 Each view can be on different monitor, sizing different, content different ... 每个视图可以在不同的监视器上,大小不同,内容不同...

Full Example 完整的例子

First, create a model ( = Data) 首先,建立模型(=资料)

using System;

namespace WpfApplication1
{
    public class MyModel
    {
        public String Text1 { get; set; }

        public Int32 Int1 { get; set; }

        public Int32 Int2 { get; set; }
    }
}

Then, a ViewModel (how the data interact and must live) 然后,创建一个ViewModel(数据如何交互以及必须如何生存)

using System;

namespace WpfApplication1
{
    public class MyViewModel
    {
        private MyModel myModel;

        public MyViewModel()
        {
            this.myModel = new MyModel() { Int1 = 1, Int2 = 12, Text1 = "toto" };
        }

        public String MyText
        {
            get { return this.myModel.Text1; }
            set { this.myModel.Text1 = value; }
        }

        public Int32 MyInt1
        {
            get { return this.myModel.Int1; }
            set { this.myModel.Int1 = value; }
        }

        public Int32 MyInt2
        {
            get { return this.myModel.Int2; }
            set { this.myModel.Int2 = value; }
        }
    }
}

Then, first view (UI, how the data must be shown) 然后,第一个视图(UI,必须如何显示数据)

<Window x:Class="WpfApplication1.View1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="View1" Height="300" Width="300"
        Background="{StaticResource {x:Static SystemColors.ControlBrushKey}}"
        xmlns:local="clr-namespace:WpfApplication1"
        >

    <Window.DataContext>
        <local:MyViewModel />
    </Window.DataContext>

    <Grid>
        <TextBox Text="{Binding MyText, UpdateSourceTrigger=PropertyChanged}" />
    </Grid>
</Window>

The code behind for this view (only UI code) 该视图后面的代码(仅UI代码)

using System.Windows;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for View1.xaml
    /// </summary>
    public partial class View1 : Window
    {
        public View1()
        {
            InitializeComponent();
        }

        public View1(MyViewModel viewModel)
            : this()
        {
            this.DataContext = viewModel;
        }
    }
}

The 2nd view (different of the first) 第二视图(与第一视图不同)

<Window x:Class="WpfApplication1.View2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="View2" Height="300" Width="300"
        Background="{StaticResource {x:Static SystemColors.ControlBrushKey}}"
        xmlns:local="clr-namespace:WpfApplication1"
        >

    <Window.DataContext>
        <local:MyViewModel />
    </Window.DataContext>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBox Text="{Binding MyText, UpdateSourceTrigger=PropertyChanged}" />
        <Slider Value="{Binding MyInt1, UpdateSourceTrigger=PropertyChanged}"  Grid.Row="1" />
    </Grid>
</Window>

using System.Windows;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for View2.xaml
    /// </summary>
    public partial class View2 : Window
    {
        public View2()
        {
            InitializeComponent();
        }

        public View2(MyViewModel viewModel)
            : this()
        {
            this.DataContext = viewModel;
        }
    }
}

The 3rd view (different of the first and the second) 第三视图(与第一视图和第二视图不同)

<Window x:Class="WpfApplication1.View3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="View2" Height="300" Width="300"
        Background="{StaticResource {x:Static SystemColors.ControlBrushKey}}"
        xmlns:local="clr-namespace:WpfApplication1"
        >

    <Window.DataContext>
        <local:MyViewModel />
    </Window.DataContext>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TextBox Text="{Binding MyText, UpdateSourceTrigger=PropertyChanged}" />
        <TextBox Text="{Binding MyInt1, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" />
        <TextBox Text="{Binding MyInt2, UpdateSourceTrigger=PropertyChanged}" Grid.Row="2" />

    </Grid>

</Window>

using System.Windows;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for View3.xaml
    /// </summary>
    public partial class View3 : Window
    {
        public View3()
        {
            InitializeComponent();
        }

        public View3(MyViewModel viewModel)
            : this()
        {
            this.DataContext = viewModel;
        }
    }
}

And to finish, your starting point, where all views are called : 最后,您将首先调用所有视图:

public MainWindow()
{
    MyViewModel myOnlyViewModel = new MyViewModel();
    View1 view1 = new View1(myOnlyViewModel);
    view1.Show();
    View2 view2 = new View2(myOnlyViewModel);
    view2.Show();
    View3 view3 = new View3(myOnlyViewModel);
    view3.Show();
}

As you can see, each change in a UI is shown in others UI (You don't need to launch 3 instances of your app, only 1 !). 如您所见,一个UI中的每个更改都显示在其他UI中(您无需启动应用程序的3个实例,只需启动1个!)。 For example, moving the slider will modify value of MyInt1 in other views. 例如,移动滑块将在其他视图中修改MyInt1的值。 All you have to do is to design all Views you want, and always think to separate how the data live and how the data are shown 您要做的就是设计所需的所有视图,并始终考虑将数据的生存方式和数据显示方式分开

If my understanding is correct. 如果我的理解是正确的。 You need to have one exe and multiple windows for each monitors/users. 每个监视器/用户需要一个exe文件和多个窗口。

Say for example if you have a Button1 and click event that create a 举例来说,假设您有一个Button1并点击事件创建了一个

Window1 obje = new Window1();Obje.Show(); Window1 obje = new Window1(); Obje.Show();

Create a New window instance on each click, and give them an id for each window and do your process. 每次单击都创建一个新窗口实例,并为每个窗口指定一个ID,然后进行处理。 If you want to persist each window instance in MainWindow keep them in List or Dictionary Have a class lever variables 如果要在MainWindow中保留每个窗口实例,请将其保留在“列表”或“字典”中。

Private Disctionary OpenWIndows = new Disctionary ();Private int Counter = 0;Window1 obje = new Window1();Counter ++;Obje.WIndowId = Counter;OpenWIndows.Add(Counter, obje);Obje.Show(); 私有词典OpenWIndows = new Disctionary();私有int计数器= 0; Window1 obje = new Window1(); Counter ++; Obje.WIndowId = Counter; OpenWIndows.Add(Counter,obje); Obje.Show();

So now OpenWIndows are available for you Inside Window1 class you can write a code for specific type of user based on your counter. 因此,现在可以在Windows1类中使用OpenWIndows了,您可以根据计数器为特定类型的用户编写代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM