我有一個應用程式可以無緣無故地更改視窗大小。它只有一個按鈕和一個 400x300 像素的影像。當按下按鈕時,會創建一個黑色影像(400x300px)并加載到 Image 控制元件,這會導致視窗增加到 1800x1400px。對我來說這似乎是一個奇怪的錯誤。知道為什么會這樣嗎?
主視窗.xaml:
<Window x:Class="WindowSizeTest.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"
mc:Ignorable="d"
Title="MainWindow" SizeToContent="WidthAndHeight">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button Content="Press me" Click="Button_Click"/>
<Image x:Name="_img" Grid.Row="1" MinWidth="400" MinHeight="300"/>
</Grid>
</Window>
主視窗.xaml.cs
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace WindowSizeTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
void Button_Click(object sender, RoutedEventArgs e)
{
_img.Source = BitmapSource.Create(
400,
300,
96.0,
96.0,
PixelFormats.Gray8,
null,
new byte[400 * 300],
400);
}
}
}
uj5u.com熱心網友回復:
將 Image 的Stretch屬性設定None為 以確保 Image 元素使用其Source位圖的確切大小。
<Image x:Name="_img" MinWidth="400" MinHeight="300" Stretch="None"/>
uj5u.com熱心網友回復:
處理您的視窗“已加載”事件以解決此問題:
void Window_Loaded(object sender, RoutedEventArgs e)
{
this.SizeToContent = SizeToContent.Manual;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/337960.html
