gpt4 book ai didi

c# - WPF 窗口可以包含一个 child 吗?

转载 作者:太空宇宙 更新时间:2023-11-03 21:33:18 27 4
gpt4 key购买 nike

我有一个 WPF 窗口:

<Window x:Class="Snapit.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" WindowState="Maximized"
ShowInTaskbar="True" AllowsTransparency="True" WindowStyle="None" Opacity="0.1"
BorderThickness="25" BorderBrush="Black" Name="myWindow">
<Grid Name="myGrid">

</Grid>
</Window>

我有一个长方形

System.Windows.Shapes.Rectangle myRect = new System.Windows.Shapes.Rectangle();
myRect = new System.Windows.Shapes.Rectangle();
myRect.Stroke = System.Windows.Media.Brushes.Yellow;
myRect.Fill = System.Windows.Media.Brushes.Black;
myRect.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
myRect.VerticalAlignment = VerticalAlignment.Center;
myRect.Height = 50;
myRect.Width = 50;
myRect.Opacity = 100;
myWindow.AddChild(myRect);

但我无法将矩形添加为子项,错误如下:

{"'The invocation of the constructor on type 'Snapit.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'."}

我不明白我哪里出错了。谢谢!!

最佳答案

你的窗口只能有一个 child ,这是你的网格......如果你想做,你应该改变

myWindow.AddChild(myRect);

myGrid.AddChild(myRect);

但是,为什么不使用 xaml 来编写矩形?读起来会更干净...

关于c# - WPF 窗口可以包含一个 child 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23103269/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com