问题描述
我有一个UWP应用程序,它在Windows :: UI :: Controls :: Canvas上绘制各种形状(椭圆,多边形,矩形)。
我想创建一个继承自UIElement的自定义DrawingObject类(我想)。我想为这个 DrawingObject添加一些自定义字段,例如名称,描述等的字符串。
I have a UWP app that draws various shapes (Ellipse, Polygon, Rectangle) on a Windows::UI::Controls::Canvas.
I would like to create a custom DrawingObject class that inherits from UIElement (I think). I would like to add some custom fields to this DrawingObject such as a Strings for name, description, etc.
我试图创建一个C ++ / CX类
I have tried to create a C++/CX class
public ref class DrawingObject sealed : UIElement
{
public:
DrawingObject();
private:
~DrawingObject();
};
但我收到错误
But I get the error
'Windows :: UI :: Xaml :: UIElement :: UIElement':无法访问类'Windows :: UI :: Xaml :: UIElement'中声明的私有成员
'Windows::UI::Xaml::UIElement::UIElement': cannot access private member declared in class 'Windows::UI::Xaml::UIElement'
第一个问题是如何解决此错误?第二个问题是如何在这个DrawingObject中定义一个形状,以便它在画布上呈现?
First question is how to resolve this error? The second question is how do I define a shape within this DrawingObject so that it is rendered on the canvas?
推荐答案
请检查
UIElement doc 首先你会发现以下内容:
Please check the UIElement doc first and you will find the following:
UIElement不公开公共构造函数。通常,您不直接从UIElement或FrameworkElement派生类。派生自定义类的更常用基类是这些类:
UIElement does not expose a public constructor. Typically, you don't derive classes from either UIElement or FrameworkElement directly. More typically used base classes for derived custom classes are these classes:
- 未密封的特定控件(例如,TextBox)
- 控制基类(Control,ContentControl,UserControl)
- 导航元素(页面,框架)
- 面板类(基类Panel,或特定的非密封实现,如Grid)
对于您的特定方案,请尝试创建
UserControl 。
For your specific scenario, please try create a UserControl instead.
祝你好运,
Barry
Barry
这篇关于[UWP] [C ++]创建自定义UIElement以添加到Canvas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!