如何从 c Sharp 文件在 WPF 画布上显示矩形

发布于 2025-01-03 09:41:56 字数 142 浏览 2 评论 0原文

我知道如何在 WPF xaml 中创建一个矩形,然后在 c Sharp 文件中编辑它。我感兴趣的是在 c Sharp 文件中创建矩形,将其显示在 WPF 画布上,然后能够对其进行操作。我和我的朋友正在开发一款游戏,我们希望能够从源代码动态生成新对象。任何帮助都会很棒:)

I know how to create a rectangle in the WPF xaml, then edit it in the c sharp file. What I'm interested in is creating the rectangle in the c sharp file, displaying it on the WPF canvas, then being able to manipulate it. My friends and I are working on a game, and we'd like to be able to generate new objects from the source code on the fly. Any help would be great :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看轻我的陪伴 2025-01-10 09:41:56

这很容易。假设您有一个像此 XAML 中那样的 Canvas,

<Window ...>
    <Canvas Name="canvas" />
</Window>

您可以使用如下代码:

Rectangle rect = new Rectangle
{
    Fill = Brushes.Red,
    Width = 100,
    Height = 100
};

Canvas.SetLeft(rect, 100);
Canvas.SetTop(rect, 100);
canvas.Children.Add(rect);

It's easy. Provided that you have a Canvas like in this XAML

<Window ...>
    <Canvas Name="canvas" />
</Window>

you can play around with code like this:

Rectangle rect = new Rectangle
{
    Fill = Brushes.Red,
    Width = 100,
    Height = 100
};

Canvas.SetLeft(rect, 100);
Canvas.SetTop(rect, 100);
canvas.Children.Add(rect);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文