UWP C++ 通过 EventHandler 自 canvas 移除
UWP C++ self removal from canvas via EventHandler
我有一个 UWP C++ 应用程序,用户可以在其中向 canvas 添加形状。我希望他们能够使用 Surface 触控笔上的橡皮擦将其删除。
项目已成功添加到 canvas 这样...
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...
}
}
如何让椭圆从子列表中删除自身?
另一种方法是遍历 canvas 中的所有 Children 并计算最接近的。这听起来不像使用 EventHandler 那样干净。
编辑
我发现这个示例 Adding an ellipse to mouse position? 还包含一个完全按照我尝试执行的方式删除的示例,但它在 C# 中,其中函数
GridCanvas.Children.Remove(ellipse);
存在。但是我在 C++ 中找不到等价物。
However I can't find and equivalent in C++.
C++ 目前似乎不支持 Remove()
方法。你应该可以先使用RemoveAt(UInt32)
as you tried. But you may not know the index of current ellipse, in that case you need to invoke IndexOf(UIElement, UInt32)
方法获取索引,然后你可以使用RemoveAt
。示例代码如下:
//Get current tapped ellipse.
Ellipse^ currentellipse = dynamic_cast<Ellipse^>(sender);
unsigned int currentindex;
canvas->Children->IndexOf(currentellipse, ¤tindex);
canvas->Children->RemoveAt(currentindex);
我有一个 UWP C++ 应用程序,用户可以在其中向 canvas 添加形状。我希望他们能够使用 Surface 触控笔上的橡皮擦将其删除。
项目已成功添加到 canvas 这样...
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...
}
}
如何让椭圆从子列表中删除自身?
另一种方法是遍历 canvas 中的所有 Children 并计算最接近的。这听起来不像使用 EventHandler 那样干净。
编辑
我发现这个示例 Adding an ellipse to mouse position? 还包含一个完全按照我尝试执行的方式删除的示例,但它在 C# 中,其中函数
GridCanvas.Children.Remove(ellipse);
存在。但是我在 C++ 中找不到等价物。
However I can't find and equivalent in C++.
C++ 目前似乎不支持 Remove()
方法。你应该可以先使用RemoveAt(UInt32)
as you tried. But you may not know the index of current ellipse, in that case you need to invoke IndexOf(UIElement, UInt32)
方法获取索引,然后你可以使用RemoveAt
。示例代码如下:
//Get current tapped ellipse.
Ellipse^ currentellipse = dynamic_cast<Ellipse^>(sender);
unsigned int currentindex;
canvas->Children->IndexOf(currentellipse, ¤tindex);
canvas->Children->RemoveAt(currentindex);