【问题标题】:How to get C# WPF MVVM Screensaver use dual monitor?如何让 C# WPF MVVM 屏幕保护程序使用双显示器?
【发布时间】:2019-02-16 12:05:04
【问题描述】:

我正在寻找如何在双显示器和更多显示器上运行 C#-WPF-MVVM-Screensaver-View 的答案。我在这里阅读了一些关于网页的教程和答案。但是,编码示例从未在 wpf 上工作过。 有人有适用于 wpf 和 Model View ViewModel Pattern 的代码示例吗?

感谢您的帮助并感谢您。

【问题讨论】:

    标签: c# wpf mvvm monitor


    【解决方案1】:

    谢谢。我是在 Windows 10 上完成的。

    1. 为 C# 创建新的 WPF 窗口项目
    2. 从 app.xaml 中删除 startupuri / startuplocation。
    3. 向 app.xaml 添加启动方法。
    4. 4.

    <Application x:Class="SeveralDisplays.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:local="clr-namespace:SeveralDisplays"
                 Startup="OnStartup">
        <Application.Resources>
             
        </Application.Resources>
    </Application>
    using System.Drawing;
    using System.Windows;
    using System.Windows.Forms;
    using Application = System.Windows.Application;
    
    namespace SeveralDisplays
    {
    
        /// <summary>
        /// Interaction logic for App.xaml
        /// </summary>
        public partial class App : Application
        {
            private void OnStartup(object sender, StartupEventArgs e)
            {
                Window mainWindow1 = new MainWindow();
                Window mainWindow2 = new MainWindow2();
    
                Screen s1 = Screen.AllScreens[0];
                Screen s2 = Screen.AllScreens[1];
    
                Rectangle r1 = s1.WorkingArea;
                Rectangle r2 = s2.WorkingArea;
    
                mainWindow1.Top = r1.Top;
                mainWindow1.Left = r1.Left;
    
                mainWindow2.Top = r2.Top;
                mainWindow2.Left = r2.Left;
    
                mainWindow1.Show();
                mainWindow2.Show();
    
                mainWindow2.Owner = mainWindow1;
            }
        }
    } 
    
    1. 添加两个类并确保它们是窗口类。
    2. 第一/主视图,这里不要改动太多

    <Window x:Class="SeveralDisplays.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:SeveralDisplays"
            mc:Ignorable="d"
            Loaded="MainWindow_OnLoaded"
            
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            
        </Grid>
    </Window>
    1. 后面的第一个窗口代码

      使用 System.Windows;

      命名空间 {

      /// <summary>
      /// Interaction logic for MainWindow.xaml
      /// </summary>
      public partial class MainWindow : Window
      {
      
          public MainWindow()
          {
              InitializeComponent();
          }
      
          private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
          {
              WindowState = WindowState.Maximized;
              WindowStyle = WindowStyle.None;
              ShowInTaskbar = false;
          }
      }
      

      }

    2. 第二个 xaml 作为窗口

    <Window x:Class="SeveralDisplays.MainWindow2"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:SeveralDisplays"
            mc:Ignorable="d"
            Title="MainWindow2"
            WindowStartupLocation="Manual"
            WindowStyle="None"
            WindowState="Maximized"
            Height="300" Width="300">
        <Grid>
            
        </Grid>
    </Window>
    1. 后面的代码:

      使用 System.Windows;

      命名空间 {

      public partial class MainWindow2 : Window
      {
          public MainWindow2()
          {
              InitializeComponent();
          }
      }
      

      }

    【讨论】:

      【解决方案2】:

      您可以简单而智能地做到这一点。我只使用 app.xaml.cs 中的一个方法

      /// <summary>
      /// Shows the screensaver on every monitor. This is a multi monitor
      /// application.
      /// </summary>
      private void ShowScreenSaver()
      {
           ClockWindow ownerWindow = null;
      
           // Creates window on other screens.
           foreach (System.Windows.Forms.Screen screen in System.Windows.Forms.Screen.AllScreens)
           {
               ClockWindow window = new ClockWindow(screen.Bounds.Width,
                      screen.Bounds.Height);
      
               // Primary screen does not have WindowsStartupLocation.
               if (screen.Primary)
               {
      
                   // Maximizes screen.
                   window.WindowState = WindowState.Maximized;
      
                   ownerWindow = window;
               }
               else
               {
      
                   // Other screens need a WindowStartupLocation on manual.
                   window.WindowStartupLocation = WindowStartupLocation.Manual;
      
                   System.Drawing.Rectangle location = screen.Bounds;
                   window.Top = location.Top;
                   window.Left = location.Left - 480;
                   window.Width = location.Width;
                   window.Height = location.Height;
               }
      
               window.Show();
           }
      
           // Sets every other screen owned to prmary window.
           // It closes all windows at once.
           foreach (Window window in Current.Windows)
           {
               if (window != ownerWindow)
               {
                   window.Owner = ownerWindow;
               }
           }
       }
      

      在这里我添加了一个视图,我为几个显示器初始化了它。

      <Window x:Class="Clock_ScreenSaver.Views.ClockWindow"
              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
              xmlns:local="clr-namespace:Clock_ScreenSaver.Views"
              mc:Ignorable="d"
              Title="ClockWindow"
              Height="{Binding DisplayHeight}"
              Width="{Binding DisplayWidth}"
              AllowsTransparency="True"
              Background="Black"
              Cursor="None"
              ShowInTaskbar="False"
              KeyDown="ClockWindow_KeyDown"
              MouseMove="ClockWindow_MouseMove"
              MouseDown="ClockWindow_MouseDown"
              Closing="ClockWindowClosing"
              Loaded="ClockWindowLoaded"
              WindowStyle="None"
              ResizeMode="NoResize">
      
          <Grid Name="WindowGrid">
              <Grid.RowDefinitions>
                  <RowDefinition Height="*"/>
                  <RowDefinition Height="300"/>
                  <RowDefinition Height="*"/>
              </Grid.RowDefinitions>
              <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="*" />
                  <ColumnDefinition Width="300"/>
                  <ColumnDefinition Width="*"/>
              </Grid.ColumnDefinitions>
          <Border Grid.Row="1" Grid.Column="1" CornerRadius="360" BorderBrush="#FFDF66" BorderThickness="5" Background="#2D2D30">
              <StackPanel>
                  <Label Foreground="#FFDF66" Margin="0,15,0,0" FontSize="25" FontFamily="Arial" HorizontalAlignment="Center">DIGICLOCK</Label>
      
                  <StackPanel Background="#3F3F46" Margin="0,20,0,5" Width="280" Height="100">
                      <Label Content="{Binding ClockTime}"  Name="timelbl" Margin="0,20,0,0" Foreground="#FFDF66" FontSize="40" FontFamily="Arial" HorizontalAlignment="Center"></Label>
                  </StackPanel>
      
                  <StackPanel Background="#3F3F46" Margin="0,0,0,10"  Width="280" Height="50">
                      <Label Content="{Binding ClockDate}"  Name="datelbl" Margin="0,8,0,0" Foreground="#FFDF66" FontSize="20" FontFamily="Arial" HorizontalAlignment="Center"></Label>
                  </StackPanel>
      
                  <Button Width="60" Padding="5,5,5,5" Background="#FFDF66" FontSize="10" FontFamily="Arial" Foreground="#333333" BorderThickness="0" Name="QuitBtn" Click="QuitBtn_Click">
                      Quit
                  </Button>
              </StackPanel>
          </Border>
          </Grid>
      </Window>

      【讨论】:

        猜你喜欢
        • 2013-09-04
        • 1970-01-01
        • 1970-01-01
        • 2015-08-29
        • 1970-01-01
        • 2016-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多