UWP C++;通过EventHandler自删除画布 我有一个UWP C++应用程序,用户可以在画布中添加形状。我希望他们能够删除他们使用橡皮擦表面笔

UWP C++;通过EventHandler自删除画布 我有一个UWP C++应用程序,用户可以在画布中添加形状。我希望他们能够删除他们使用橡皮擦表面笔,uwp,c++-cx,Uwp,C++ Cx,项目已成功添加到画布,如下所示 if (e->Pointer->PointerDeviceType == Windows::Devices::Input::PointerDeviceType::Pen) { auto Translate = ref new TranslateTransform(); auto ellipse = ref new Ellipse(); PointerPoint^ p = e->GetCurrentPoint(imgCa

项目已成功添加到画布,如下所示

if (e->Pointer->PointerDeviceType == Windows::Devices::Input::PointerDeviceType::Pen) {

    auto Translate = ref new TranslateTransform();
    auto ellipse = ref new Ellipse();

    PointerPoint^ p = e->GetCurrentPoint(imgCanvas);

    Translate->X = p->Position.X - Radius/2;
    Translate->Y = p->Position.Y - Radius/2;

    ellipse->Fill = ref new SolidColorBrush(ColorHelper::FromArgb(127, 255, 255, 0));
    ellipse->Width = Radius;
    ellipse->Height = Radius;

    ellipse->Stroke = ref new SolidColorBrush(Colors::Black);
    ellipse->StrokeThickness = 1;

    ellipse->RenderTransform = Translate;
    ellipse->PointerPressed += ref new Windows::UI::Xaml::Input::PointerEventHandler(this, &MainPage::onEllipseTouched);

    Canvas->Children->Append(ellipse);
}
我有下面的函数,在触摸时成功调用

MainPage::onEllipseTouched(Platform::Object^ sender, Windows::UI::Xaml::Input::PointerRoutedEventArgs^ e)
{
    //check for eraser
    PointerPoint^ p = e->GetCurrentPoint(Canvas);
    if (p->Properties->IsEraser) {
        //NEED TO REMOVE ITSELF FROM
        //Canvas->Children->RemoveAt(i);
        //but obviously I don't know I...
    }
}
如何使椭圆从子列表中删除自身?
另一种方法是循环遍历画布中的所有子对象,并计算最接近的子对象。这听起来不像使用EventHandler那么干净

编辑

我发现这个例子也包含了一个删除的例子,就像我试图做的那样,但它在C#中,函数

GridCanvas.Children.Remove(ellipse);
存在。但在C++中,我找不到和等价的。

但是,在C++中,我找不到和等价的。 <>看起来,C++中当前不支持<代码>删除()/<代码>方法。你应该能够像你尝试的那样使用。但是您可能不知道当前椭圆的索引,在这种情况下,您需要首先调用方法来获取索引,然后可以使用

RemoveAt
。示例代码如下:

//Get current tapped ellipse.
Ellipse^ currentellipse = dynamic_cast<Ellipse^>(sender);
unsigned int currentindex;   
canvas->Children->IndexOf(currentellipse, &currentindex);
canvas->Children->RemoveAt(currentindex);
//获取当前分接椭圆。
椭圆^currentellipse=动态投影(发送方);
无符号整数当前索引;
画布->子对象->索引of(currentellipse和currentindex);
画布->子对象->移除(当前索引);

有什么逻辑原因,为什么C++中没有所有的方法可用?谢谢你的回答,现在我需要弄清楚我是如何打破椭圆->PointerPressed+=ref new Windows::UI::Xaml::Input::PointerEventHandler()的。。。。它不再工作了。。。唉。信息技术只是从未。结束时,@ OLVIE9523,为此,您可能需要研究C++和C语言之间的区别。如果您有新问题,可以打开一个新线程。谢谢你的理解。