wpf 如何为通用 Windows 应用程序设置固定窗口大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36065512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set fixed window size for Universal Windows Application
提问by Jafuentes
I just started to develop an Universal Windows Application in VS2015 Community Edition. I have used the sample called PieCharts for Windows UWP.
我刚开始在 VS2015 社区版中开发一个通用的 Windows 应用程序。我使用了名为 PieCharts for Windows UWP 的示例。
My problem is that I whould like to fix the window size to 400x650 as I did in my old desktop applications. I have set Width and Height manually It does not work. My MainPage.xaml is something like the following code.
我的问题是我想像在旧桌面应用程序中那样将窗口大小修复为 400x650。我手动设置了宽度和高度它不起作用。我的 MainPage.xaml 类似于以下代码。
<Page x:Class="MyFistUWP.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyFistUWP"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
MaxHeight="650" MinHeight="650" MaxWidth="400" MinWidth="400" Height="650" Width="400" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="140" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Image Grid.Row="0" Source="Resources/my_logo.png" />
</Grid>
The window size is wrong and I can resize window.
窗口大小错误,我可以调整窗口大小。
Thanks,
谢谢,
回答by Mykyta Bondarenko
You can use this code for set minimal size of application:
ApplicationView.GetForCurrentView().SetPreferredMinSize(new Size(400, 650));
您可以使用应用程序的设置最小尺寸的代码:
ApplicationView.GetForCurrentView().SetPreferredMinSize(new Size(400, 650));
回答by tomelom
I found a solution. I think its not the best, but it works.
XAML:
我找到了解决办法。我认为它不是最好的,但它有效。
XAML:
<Grid Background="#4A9130" SizeChanged="FormName_SizeChanged">
c#
C#
private void FormName_SizeChanged(object sender, SizeChangedEventArgs e)
{
ApplicationView.GetForCurrentView().TryResizeView(new Size(900, 600));
}
Hope it helps
希望能帮助到你
回答by Weissu
I guess the only choice is use OnSizeChanged Method to control size of window. It's work's for me. Maybe not the best choice but really easy way to solve problem.
我想唯一的选择是使用 OnSizeChanged 方法来控制窗口的大小。这是给我的工作。也许不是最好的选择,但解决问题的方法很简单。
private void FormName_SizeChanged(object sender, SizeChangedEventArgs e)
{
if((this.Width != 400) & (this.Height != 650))
{
this.Width = 450;
this.Height = 650;
}
}

