简体   繁体   中英

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

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.

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.

Thanks in Advance

Change it to 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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