我有一個StackPanel
有2個孩子,我需要根據StackPanel
的大小調整大小。我在StackPanel
的SizeChanged
附加了一個事件處理程序,並在那裏調整元素的大小。問題是事件處理程序被一次又一次地觸發,直到應用程序崩潰。這是怎麼回事?調整兒童大小時在StackPanel中的UWP佈局週期
class BasicStatusTile : StackPanel {
private TextBlock TopText { get; set; }
private TextBlock BottomText { get; set; }
public BasicStatusTile() {
InitializeComponent();
}
private void InitializeComponent() {
TopText = new TextBlock() {
Text = "0",
HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch,
TextAlignment = Windows.UI.Xaml.TextAlignment.Center,
FontSize = 48
};
BottomText = new TextBlock() {
Text = "Speed (km/h)",
HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch,
VerticalAlignment = Windows.UI.Xaml.VerticalAlignment.Bottom,
TextAlignment = Windows.UI.Xaml.TextAlignment.Center
};
base.Children.Insert(0, TopText);
base.Children.Insert(1, BottomText);
base.Background = new SolidColorBrush(Colors.White);
base.SizeChanged += BasicStatusTile_SizeChanged;
}
private void BasicStatusTile_SizeChanged(object sender, Windows.UI.Xaml.SizeChangedEventArgs e) {
double third = base.ActualHeight/6;
TopText.FontSize = third * 4;
BottomText.FontSize = third;
}
}
'StackPanel'應該是一個主持人,你把所有的UI元素。你不應該這樣擴展它。這裏應該使用'UserControl'。 –