【问题标题】:UWP C++ self removal from canvas via EventHandler通过 EventHandler 从画布中删除 UWP C++
【发布时间】:2017-05-12 17:53:36
【问题描述】:

我有一个 UWP C++ 应用程序,用户可以在其中向画布添加形状。我希望他们能够使用 Surface Pen 上的橡皮擦将其删除。

像这样成功地将项目添加到画布...

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 那样干净。

编辑

我找到了这个示例Adding an ellipse to mouse position?,它还包含一个完全按照我尝试的方式删除的示例,但它在 C# 中的函数

GridCanvas.Children.Remove(ellipse);

存在。但是我在 C++ 中找不到和等价物。

【问题讨论】:

    标签: uwp c++-cx


    【解决方案1】:

    但是我在 C++ 中找不到和等价物。

    C++ 目前似乎不支持Remove() 方法。你应该可以使用RemoveAt(UInt32),就像你尝试的那样。但是你可能不知道当前椭圆的索引,那么你需要先调用IndexOf(UIElement, UInt32)方法来获取索引,然后你可以使用RemoveAt。示例代码如下:

    //Get current tapped ellipse.
    Ellipse^ currentellipse = dynamic_cast<Ellipse^>(sender);
    unsigned int currentindex;   
    canvas->Children->IndexOf(currentellipse, &currentindex);
    canvas->Children->RemoveAt(currentindex);
    

    【讨论】:

    • C++ 中并非所有方法都可用有什么合乎逻辑的原因吗?感谢您的回答,现在我需要弄清楚我是如何打破椭圆的->PointerPressed += ref new Windows::UI::Xaml::Input::PointerEventHandler()....它不再工作...叹息.它。只是。绝不。结束。
    • @Oliver9523 为此,您可能需要调查 c++ 和 c# 之间的差异。 msdn.microsoft.com/en-us/library/yyaad03b(v=vs.71).aspx 。如果您有新问题,您可以打开一个新线程。感谢理解。
    猜你喜欢
    • 1970-01-01
    • 2016-07-05
    • 2021-12-25
    • 2020-10-19
    • 2013-08-24
    • 2015-07-29
    • 2021-04-30
    • 2014-04-13
    • 2020-10-01
    相关资源
    最近更新 更多