XAML .net maui在运行时将对象添加到画布

tsm1rwdh  于 2022-12-16  发布在  .NET
关注(0)|答案(2)|浏览(201)

假设我有一个非常简单的xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:drawable="clr-namespace:eden.Pages"
             x:Class="eden.Pages.Modeler">
    <ContentPage.Resources>
    <drawable:GraphicsDrawable x:Key="drawable" />
</ContentPage.Resources>
    <VerticalStackLayout>
        <GraphicsView x:Name="modelerArea"
            Drawable="{StaticResource drawable}">
            <FlyoutBase.ContextFlyout>
                <MenuFlyout>
                    <MenuFlyoutItem Text="Add rectangle"
                            Clicked="AddRectangle"> <!-- HERE I WANT TO ADD RECTANGLE ON CLICK -->
                    </MenuFlyoutItem>
                </MenuFlyout>
            </FlyoutBase.ContextFlyout>
        </GraphicsView>
    </VerticalStackLayout>
</ContentPage>

然后是非常简单的xaml.cs

public partial class Modeler : ContentPage
    {
        public Modeler()
        {
            InitializeComponent();
        }

        private void AddRectangle(object sender, EventArgs e)
        {
           // for example
           // var mySpecialRectangle = new specialRectangle();
           // canvas.Add(mySpecialRectangle);
        }
    }

    public class GraphicsDrawable : IDrawable
    {
        public void Draw(ICanvas canvas, RectF dirtyRect)
        {
            canvas.StrokeColor = Colors.Red;
            canvas.StrokeSize = 6;
            canvas.DrawLine(10, 10, 90, 100);
        }
    }
}

如何将该对象添加到canvas/graphicsview?

q7solyqu

q7solyqu1#

矩形是在graphicsview中绘制的,所以你可以创建一个继承自IDrawable的类,然后将其设置为GraphicsView示例的Drawable属性,最后你可以将GraphicsView示例添加到布局中,例如:
在页面中.xaml:

<VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center"
        x:Name="layout">
        <Button
                x:Name="CounterBtn"
                Text="Click me"
                Clicked="OnCounterClicked" />
    </VerticalStackLayout>

在页面.cs:

public partial class MainPage : ContentPage
{

      public MainPage()
      {
            InitializeComponent();
      }
    internal class RectangleDrawable : IDrawable
    {
        public void Draw(ICanvas canvas, RectF dirtyRect)
        {
            canvas.StrokeColor = Colors.DarkBlue;
            canvas.StrokeSize = 4;
            canvas.DrawRectangle(10, 10, 100, 50);
        }
    }
    private void OnCounterClicked(object sender, EventArgs e)
      {
            
            GraphicsView graphicsView = new GraphicsView();
            graphicsView.Drawable = new RectangleDrawable();
        graphicsView.HeightRequest= 200;
        graphicsView.WidthRequest = 400;
        layout.Children.Add(graphicsView);
      }
}

单击按钮时将绘制矩形。

7d7tgy0s

7d7tgy0s2#

你读过Draw graphical objects吗?
ICanvas方法中,您不需要“添加对象”,而是进行“绘制”调用。
modelerArea在您的xaml中是canvas,因此它将是以下各项的变体:

public void MyDrawLine(ICanvas canvas, ...)
{
    canvas.DrawLine(...);
}

...
MyDrawLine(modelerArea, ...);

警告:我自己从来没有这样做过;我是根据链接的文档来做的。
这意味着如果你想为以后“记住”一些“对象”,你必须自己去做。定义你自己的类来描述要绘制的对象。维护那些对象“模型”的列表,这样你就可以在以后绘制它们。

相关问题