问题描述
我知道可以在Xamarin应用程序的import random
from PyQt5 import QtCore,QtGui,QtWidgets
class ArrowItem(QtWidgets.QGraphicsPathItem):
def __init__(self,parent=None):
super().__init__(parent)
self._length = -1
self._points = QtCore.QPointF(),QtCore.QPointF(),QtCore.QPointF()
self.length = 40.0
@property
def length(self):
return self._length
@length.setter
def length(self,l):
self._length = l
pos_top = QtCore.QPointF(0,l * 4 / 5)
pos_left = QtCore.QPointF(-l * 3 / 5,-l / 5)
pos_right = QtCore.QPointF(
l * 3 / 5,-l / 5,)
path = QtGui.QPainterPath()
path.moveto(pos_top)
path.lineto(pos_right)
path.lineto(pos_left)
self.setPath(path)
self._points = pos_top,pos_left,pos_right
def paint(self,painter,option,widget):
pos_top,pos_right = self._points
left_color = QtGui.QColor("#cc0000")
right_color = QtGui.QColor("#ff0000")
bottom_color = QtGui.QColor("#661900")
path_left = QtGui.QPainterPath()
path_left.lineto(pos_top)
path_left.lineto(pos_left)
path_right = QtGui.QPainterPath()
path_right.lineto(pos_top)
path_right.lineto(pos_right)
path_bottom = QtGui.QPainterPath()
path_bottom.lineto(pos_left)
path_bottom.lineto(pos_right)
painter.setPen(QtGui.QColor("black"))
painter.setBrush(left_color)
painter.drawPath(path_left)
painter.setBrush(right_color)
painter.drawPath(path_right)
painter.setBrush(bottom_color)
painter.drawPath(path_bottom)
def moveto(self,next_position,duration=100):
self._animation = QtCore.QVariantAnimation()
self._animation.setStartValue(self.pos())
self._animation.setEndValue(next_position)
self._animation.setDuration(duration)
self._animation.start(QtCore.QAbstractAnimation.DeleteWhenStopped)
self._animation.valueChanged.connect(self.setPos)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self,parent=None):
super().__init__(parent)
self.scene = QtWidgets.QGraphicsScene()
view = QtWidgets.QGraphicsView()
view.setRenderHints(QtGui.QPainter.Antialiasing)
view.setScene(self.scene)
view.scale(1,-1)
button = QtWidgets.QPushButton("Click me")
central_widget = QtWidgets.QWidget()
lay = QtWidgets.QVBoxLayout(central_widget)
lay.addWidget(view)
lay.addWidget(button)
self.setCentralWidget(central_widget)
self.resize(640,480)
self.arrow_item = ArrowItem()
self.scene.addItem(self.arrow_item)
button.clicked.connect(self.handle_clicked)
def handle_clicked(self):
x,y = random.sample(range(300),2)
print("next position:",x,y)
self.arrow_item.moveto(QtCore.QPointF(x,y))
if __name__ == "__main__":
app = QtWidgets.QApplication([])
w = MainWindow()
w.show()
app.exec_()
中放置SkiaSharp
画布。这是一些示例(工作)代码:
Grid
xmlns:skia="clr-namespace:SkiaSharp.Views.Forms;assembly=SkiaSharp.Views.Forms"
但是当我尝试在WPF应用中执行类似操作时:
<skia:SKCanvasView
x:Name="AUView"
Grid.Row="2"
PaintSurface="AUView_PaintSurface" />
xmlns:skia="clr-namespace:SkiaSharp;assembly=SkiaSharp"
我得到了错误:
<skia:SKCanvas
Name="AUView"
Grid.Row="2"
PaintSurface="AUView_PaintSurface" />
是否可以在WPF中将A value of type 'SKCanvas' cannot be added to a collection or dictionary of type 'UIElementCollection'.
放在网格中,还是整个页面都必须是SKCanvas
?
否则,您如何/如何在WPF中使用SKCanvas
?
解决方法
首先,您必须安装SkiaSharp.Views.WPF
软件包,该软件包也会自动安装SkiaSharp
。单独的SkiaSharp
包将不会包含所需的WPF控件,即SKElement
。
由于它是FrameworkElement
,因此可以将其放置在视图中的任何位置。 SKCanvas
不是WPF兼容组件。要在XAML中使用SKElement
,必须插入其XML名称空间。
xmlns:skia="clr-namespace:SkiaSharp.Views.WPF;assembly=SkiaSharp.Views.WPF"
然后将SKCanvas
控件放置在视图中的任何位置。
<skia:SKElement
Name="AUView"
Grid.Row="2"
PaintSurface="AUView_PaintSurface"/>