简体   繁体   English

c# Windows Foundation 为什么我不能画一个形状@坐标

[英]c# Windows Foundation why can't I draw a shape @ coordinates

W11, VS2022 W11, VS2022

I'm fairly new to UI design in c#. I'm trying to draw a control at runtime and I can't seem to find the way to specify its x & y properties or set it to a Point.我是 c# 中 UI 设计的新手。我试图在运行时绘制控件,但我似乎无法找到指定其 x 和 y 属性或将其设置为点的方法。

xaml xaml

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <Grid x:Name="layoutRoot"></Grid>
    </Grid>
</Page>

c# method

        public MainPage()
        {
            this.InitializeComponent();
            var ellipse1 = new Ellipse
            {
                Fill = new SolidColorBrush(Windows.UI.Colors.Pink),

                Width = 200,
                Height = 200                
            };

            layoutRoot.Children.Add(ellipse1);
        }

` `

Have tried setleft, setright and location but they are all missing.已经尝试过 setleft、setright 和 location,但它们都丢失了。

Thanks in Advance提前致谢

Change it to Canvas Canvas

var ellipse1 = new Ellipse
            {
                Fill = new SolidColorBrush(Colors.Pink),
                Width = 200,
                Height = 200                
            };

int left = 50, top = 50;

Canvas.SetLeft(ellipse1, left); // second option Canvas.SetRight()
Canvas.SetTop(ellipse1, top);  // second option Canvas.SetTop()
layoutRoot.Children.Add(ellipse1);

EDIT:编辑:

<Page
    x:Class="App1.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Canvas x:Name="layoutRoot">
    </Canvas>
</Page>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM