Search code examples
c#runtimecoordinateswin-universal-appshapes

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


Solution

  • 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>