XAML WPF Prism无法在辅助屏幕上最大化应用程序

eoigrqb6  于 2023-03-06  发布在  其他
关注(0)|答案(1)|浏览(220)

我正在写一个应用程序与WPF棱镜使用MVVM。我的应用程序使用1280 x800 px触摸屏,这是设置为次要屏幕,并有任务栏隐藏。我的主屏幕是1920 x1080 px,并设置为主要。我希望我的应用程序启动在次要屏幕上,并涵盖所有可见的空间,在这里我得到了奇怪的行为。我可以设置屏幕显示如下:MainWindow.xaml:

<Window x:Class="MyApp.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:prism="http://prismlibrary.com/"
    prism:ViewModelLocator.AutoWireViewModel="True"
    WindowState="{Binding WinState}"
    WindowStyle="{Binding WinStyle}"
    Left="{Binding PositionX}"
    Top="{Binding PositionY}"
    Height="{Binding Height}"
    Width="{Binding Width}"
    Title="{Binding Title}" >
<Grid>
    <ContentControl prism:RegionManager.RegionName="ContentRegion" />
</Grid>

MainWindowViewModel.cs:

using Prism.Mvvm;
using System.Windows;
using System.Windows.Forms;
using System.Linq;

namespace MyApp.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        public WindowState WinState
        {
            get { return windowState; }
            set { SetProperty(ref windowState, value); }
        }

        public WindowStyle WinStyle
        {
            get { return windowStyle; }
            set { SetProperty(ref windowStyle, value); }
        }

        public double PositionX
        {
            get { return positionX; }
            set { SetProperty(ref positionX, value); }
        }

        public double PositionY
        {
            get { return positionY; }
            set { SetProperty(ref positionY, value); }
        }

        public int Height
        {
            get { return height; }
            set { SetProperty(ref height, value); }
        }

        public int Width
        {
            get { return width; }
            set { SetProperty(ref width, value); }
        }

        #region Fields
        private string _title = "";
        private WindowState windowState;
        private WindowStyle windowStyle;
        private double positionX;
        private double positionY;
        private int height;
        private int width;
        #endregion

        public MainWindowViewModel()
        {
            InitScreen();
        }

        private void InitScreen()
        {
            var screens = Screen.AllScreens.ToList();
            var screen = Screen.AllScreens.Where(s => !s.Primary).First();
            WinState = WindowState.Normal;
            WinStyle = WindowStyle.SingleBorderWindow;
            if(screen != null) //Secondary screen detected
            {
                //Set second screen as app display screen
                this.PositionX = screen.WorkingArea.Left;
                this.PositionY = screen.WorkingArea.Top;
                this.Height = screen.WorkingArea.Height;
                this.Width = screen.WorkingArea.Width;
                this.WinState = WindowState.Maximized;
                this.WinStyle = WindowStyle.None;
            }
        }
    }
}

还尝试了代码隐藏方法,并设置了类似的复制粘贴此方法(修改为使用嵌入的WPF属性,而不是绑定到它们的我的属性)到MainWindow.xaml.cs,但它给了我同样的结果。我最初的问题是,我可以设置应用程序显示在第二个屏幕上,但当我试图以编程方式最大化它时,它却使应用程序显示(最大化)在我的主屏幕上(如果我这样做,如果与最大化按钮,它的工作如预期).在上述方法中,我设置WindowStyle为正常,因为这个answer,但它也没有帮助.
我已经使用了这样的解决方案,但仍然没有得到我所期望的。我已经隐藏了第二个屏幕上的任务栏,并设置了我的窗口的高度和宽度的第二个屏幕的高度和宽度,但由于某些原因,它不匹配的屏幕正确(我得到了酒吧左右的应用程序)。这很奇怪,因为如果我检查第二个屏幕的数据,我得到这样的信息:

{
    Bounds = {
        X = -1280 
        Y = 272 
        Width = 1280 
        Height = 800
    } 
    WorkingArea = {
        X = -1280 
        Y = 272 
        Width = 1280 
        Height = 800
    } 
    Primary = false 
    DeviceName = "\\\\.\\DISPLAY2"
}

为什么WorkingArea和Bounds中的Y都设置为272?
TL;DR:我可以将应用设置为在辅助显示器上显示,但当我最大化它时,它会显示在主显示器上。当我隐藏辅助显示器上的任务栏时,将WindowStyle设置为None(Borderless),将Window size设置为屏幕大小,它不会覆盖所有辅助屏幕(我在窗口的右侧和左侧找到了未覆盖的区域)。

p1tboqfb

p1tboqfb1#

似乎禁用控件大小调整解决了问题。

相关问题