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));
}
}
}
3条答案
按热度按时间luaexgnf1#
我不认为WPF中有一种方法可以锁定它,你可以提供一个旋转布局转换到根容器。它实际上不是锁定方向,而是旋转你的应用程序。类似这样的东西,
请在此处阅读,
Layout Transformation
nhaq1z212#
对于任何人谁到达这个问题,并留下需要更多的细节,这里是一个完全详细的答案...
由于平板电脑的旋转支持是在单个供应商级别上完成的,因此Windows 7 / 8中除了检测旋转的变化之外,并没有太多的支持。屏幕仍然可以通过一些XAML技巧伪锁定为横向格式。这里是我昨晚在平板电脑上敲出并测试的一些示例代码...
主窗口.xaml
主窗口.xaml.cs
视图模型\主窗口视图模型.cs
下面是两张截图,显示了方向的变化。如果你全屏运行一个应用程序,你不会看到窗口镶边,你也不会意识到屏幕实际上已经旋转了。任何屏幕键盘在响应文本输入时打开,当然仍然会以纵向位置弹出。但是内容总是横向的事实应该给予操作者足够的提示来将图形输入板转过来进行输入。
我希望这能帮助到任何在这里着陆的人。
vecaoik13#
这对我很有效
在我的主窗口加载: