У нас есть изображение, которое нужно показать как заставку. Это изображение должно отображаться в его реальном размере, если ширина или высота не превышает определенный максимум. В этом случае изображение должно быть масштабировано, чтобы соответствовать окну.
То, что у нас сейчас есть, работает нормально, за исключением случаев, когда превышен MaxWidth
(или MaxHeight
). В этом случае он просто обрезает изображение, а не масштабирует его.
Я обнаружил похожую проблему , но это не совсем то же самое. Есть ли способ преодолеть это?
[XML]$Xaml = @"
<Window
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='Window'
WindowStartupLocation='CenterScreen' ResizeMode='NoResize'
SizeToContent='WidthAndHeight' MaxWidth='600' MaxHeight='400'
Title='Splash' WindowStyle='None'
>
<Grid>
<!-- Use a grid with auto height and width to set the window to the size of the image -->
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<!-- Use a grid in the button to set the size of the counter and the pause/resume label -->
<Image Source='$Path'/>
<Button Name='TimerButton' VerticalAlignment='Top' HorizontalAlignment='Right' Margin='0,3,3,0'>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="15"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6"/>
<ColumnDefinition Width="26"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="0" Name='ResumePauseLabel' Padding='0' HorizontalContentAlignment='Left' Content='II'/>
<Label Grid.Column="1" Grid.Row="0" Name='TimerLabel' Padding='0' HorizontalContentAlignment='Center' Content='$Seconds'/>
</Grid>
</Button>
</Grid>
</Window>
"@
Даже при попытке этого изображение все равно обрезается:
<Image Source='$Path' Stretch='None' MaxHeight='400' MaxWidth='1800'/>
Окончательное решение благодаря комментариям @ mm8 и @Клеменс внес изменения в Grid
и установил дополнительные свойства для изображения:
[XML]$Xaml = @"
<Window
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' x:Name='Window'
WindowStartupLocation='CenterScreen' ResizeMode='NoResize'
SizeToContent='WidthAndHeight' MaxWidth='1600 ' MaxHeight='1600'
>
<Grid>
<Image Stretch="Uniform" StretchDirection="DownOnly" Source='$Path'/>
<Button Name='TimerButton' VerticalAlignment='Top' HorizontalAlignment='Right' Margin='0,3,3,0'>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="15"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="6"/>
<ColumnDefinition Width="26"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="0" Name='ResumePauseLabel' Padding='0' HorizontalContentAlignment='Left' Content='II'/>
<Label Grid.Column="1" Grid.Row="0" Name='TimerLabel' Padding='0' HorizontalContentAlignment='Center' Content='$Seconds'/>
</Grid>
</Button>
</Grid>
</Window>
"@