如何锁定WPF应用程序的方向

arknldoa  于 2022-11-26  发布在  其他
关注(0)|答案(3)|浏览(169)

是否有一个API来锁定我的WPF应用程序的方向,使其只能在横向模式下运行?
我可以找到可以在方向更改时触发事件,但是否有方法将其锁定
Microsoft.Win32.SystemEvents.DisplaySettingsChanged

luaexgnf

luaexgnf1#

我不认为WPF中有一种方法可以锁定它,你可以提供一个旋转布局转换到根容器。它实际上不是锁定方向,而是旋转你的应用程序。类似这样的东西,

<Grid.RenderTransform>
            <RotateTransform Angle="-90"/>
</Grid.RenderTransform>

请在此处阅读,
Layout Transformation

nhaq1z21

nhaq1z212#

对于任何人谁到达这个问题,并留下需要更多的细节,这里是一个完全详细的答案...
由于平板电脑的旋转支持是在单个供应商级别上完成的,因此Windows 7 / 8中除了检测旋转的变化之外,并没有太多的支持。屏幕仍然可以通过一些XAML技巧伪锁定为横向格式。这里是我昨晚在平板电脑上敲出并测试的一些示例代码...

主窗口.xaml

<Window x:Class="LockRotationWpf.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:LockRotationWpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid HorizontalAlignment="Center" 
          VerticalAlignment="Center" 
          RenderTransformOrigin="0.5,0.5" >
        <Grid.RenderTransform >
            <RotateTransform Angle="{Binding Angle}" />
        </Grid.RenderTransform>
        <StackPanel>
            <TextBlock FontSize="100" Text="Landscape"/>
            <TextBlock Text="{Binding Angle}" TextAlignment="Center"/>
        </StackPanel>
    </Grid>
</Window>

主窗口.xaml.cs

using System.Windows;
using LockRotationWpf.ViewModels;

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

            var vm = new MainWindowViewModel();
            DataContext = vm;
        }
    }
}

视图模型\主窗口视图模型.cs

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using LockRotationWpf.Annotations;

namespace LockRotationWpf.ViewModels
{
    // Omit the INotifyPropertyChanged implementation if already implemented on a base ViewModel class.
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        public double _angle;

        public MainWindowViewModel()
        {
            Microsoft.Win32.SystemEvents.DisplaySettingsChanged += DisplaySettingsChanged;
        }

        private void DisplaySettingsChanged(object sender, EventArgs e)
        {
            Angle = System.Windows.SystemParameters.PrimaryScreenHeight > System.Windows.SystemParameters.PrimaryScreenWidth ? -90 : 0;
        }

        public double Angle
        {
            get { return _angle; }
            set
            {
                _angle = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

下面是两张截图,显示了方向的变化。如果你全屏运行一个应用程序,你不会看到窗口镶边,你也不会意识到屏幕实际上已经旋转了。任何屏幕键盘在响应文本输入时打开,当然仍然会以纵向位置弹出。但是内容总是横向的事实应该给予操作者足够的提示来将图形输入板转过来进行输入。

我希望这能帮助到任何在这里着陆的人。

vecaoik1

vecaoik13#

这对我很有效

[System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SetDisplayAutoRotationPreferences")]
    private static extern UInt32 SetDisplayAutoRotationPreferences8(UInt32 orientation);

    [System.Runtime.InteropServices.DllImport("kernel", EntryPoint = "SetDisplayAutoRotationPreferences")]
    private static extern UInt32 SetDisplayAutoRotationPreferences7(UInt32 orientation);

    public MyWindow() 
    {
       InitializeComponent();
    }

在我的主窗口加载:

SetDisplayAutoRotationPreferences8(2);//Lanscape

相关问题