[英]How to efficiently scale all elements in a WinUI 3 desktop app when Window size changes?
I'd like to have everything in my WinUI 3
(v1.2) desktop app UI ( MainWindow
) scale up in size as the MainWindow
changes in size.我想让我的WinUI 3
(v1.2) 桌面应用程序 UI ( MainWindow
) 中的所有内容随着MainWindow
大小的变化而扩大。 I'd also like to maintain the relative positioning of everything in the MainWindow
.我还想保持MainWindow
中所有内容的相对定位。 The purpose is to allow elderly users a quick and easy way to "enlarge" content by simply expanding the size of the window.目的是让老年用户通过简单地扩大窗口的大小来快速简便地“放大”内容。
For example, take a vanilla Template Studio
WinUI 3
desktop app with menu bar style navigation and a single page (call it MainPage
).例如,使用带有菜单栏样式导航和单个页面(称为MainPage
)的普通Template Studio
WinUI 3
桌面应用程序。 Replace MainPage.xaml
with将MainPage.xaml
替换为
<Page
x:Class="TestScale.Views.MainPage"
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"
mc:Ignorable="d">
<Grid Grid.Row="0" Grid.RowSpan="4">
<Border x:Name="Border" Width="300" Height="200" BorderThickness="2" BorderBrush="Red">
<TextBlock Text="The quick brown fox." HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</Grid>
</Page>
which just draws a red rectangle with a TextBlock
in the middle of the page (ignoring space used by ShellPage.xaml
).它只是在页面中间绘制一个带有TextBlock
的红色矩形(忽略ShellPage.xaml
使用的空间)。
I'd like a straightforward way to scale everything, if possible, so that the rectangle and text increase in size ( x
and y
) in the same proportions ( dx
and dy
) that MainPage
does when MainWindow
changes, but also maintain their relative positions in the xy
plane.如果可能的话,我想要一种直接缩放所有内容的方法,以便矩形和文本的大小( x
和y
)以与MainPage
在MainWindow
更改时相同的比例( dx
和dy
)增加,但也保持它们的相对位置在xy
平面上。 I've tried using Scale
and various Translation
s, but it always seems certain elements respond differently or only one dimension responds while the other stays fixed.我试过使用Scale
和各种Translation
,但似乎某些元素的响应不同,或者只有一个维度响应而另一个维度保持固定。
For example, adding例如,添加
Scale="{x:Bind ((app:App)app:App.Current).ScaleToInitialSize, Mode=OneWay}"
to the definition of ShellPage.xaml
almost does the trick except the position of the rectangle changes because the actual size of MainPage
changes to reflect pixels used but the rectangle size reflects "scaled" sizes. ShellPage.xaml
的定义几乎起到了作用,只是矩形的位置发生了变化,因为MainPage
的实际大小发生了变化以反映使用的像素,但矩形大小反映了“缩放”的大小。
Is there a good discussion of scaling in WinUI 3
or UWP
I've missed?是否有我错过的关于在WinUI 3
或UWP
中缩放的很好的讨论? I currently achieve this by binding everything to a Ratio
property ( Vector2
or Size
) in my App
class that changes whenever MainWindow.SizeChanged
fires.我目前通过将所有内容绑定到我的App
类中的Ratio
属性( Vector2
或Size
)来实现这一点,该属性在MainWindow.SizeChanged
触发时发生变化。 Unfortunately, this means I also have to bind things like XAML Thickness
, FontSize
, and even CornerRadius
properties to maintain the look and layout of the UI.不幸的是,这意味着我还必须绑定 XAML Thickness
、 FontSize
甚至CornerRadius
属性,以维护 UI 的外观和布局。
I expect this is a noob question but I can't seem to find any discussion anywhere.我希望这是一个菜鸟问题,但我似乎无法在任何地方找到任何讨论。 Any help gratefully appreciated.任何帮助感激不尽。
You can use the ViewBox .您可以使用ViewBox 。 The ViewBox scales its content automatically. ViewBox 自动缩放其内容。
<Viewbox>
<Grid
Grid.Row="0"
Grid.RowSpan="4">
<Border
x:Name="Border"
Width="300"
Height="200"
BorderBrush="Red"
BorderThickness="2">
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="The quick brown fox." />
</Border>
</Grid>
</Viewbox>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.