如何从另一个 class 访问名称 canvas 到 MainPage
How to access the name canvas to MainPage from another class
我是 C# 新手。我想从 class CS_Line 访问 MainPage.xaml 中的 Canvas,但我做不到。如何在classCS_Line.cs.
中调用名字的canvas
MainPage.xaml:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Canvas x:Name="MyCanvas" HorizontalAlignment="Left" Height="573" Margin="154,76,0,0" VerticalAlignment="Top" Width="913" Background="White"/>
</Grid>
在CS_Line.cs中:
class CS_Line
{
//attribute
InkManager _inkKhaled = new Windows.UI.Input.Inking.InkManager();
private uint _penID;
private uint _touchID;
private Point _previousContactPt;
private Point currentContacPt;
private double x1;
private double y1;
private double x2;
private double y2;
//method
private void MyCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
{
PointerPoint pt = e.GetCurrentPoint(MyCanvas); *//====>>>>> can't access the MyCanvas*
_previousContactPt = pt.Position;
PointerDeviceType pointerDevType = e.Pointer.PointerDeviceType;
if (pointerDevType == PointerDeviceType.Pen ||
pointerDevType == PointerDeviceType.Mouse &&
pt.Properties.IsLeftButtonPressed)
{
_inkKhaled.ProcessPointerDown(pt);
_penID = pt.PointerId;
e.Handled = true;
}
else if (pointerDevType == PointerDeviceType.Touch)
{
}
}
您可以从 sender
得到 MyCanvas
。
private void MyCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
{
Canvas myCanvas = sender as Canvas; // Here's your MyCanvas object
PointerPoint pt = e.GetCurrentPoint(myCanvas);
_previousContactPt = pt.Position;
我是 C# 新手。我想从 class CS_Line 访问 MainPage.xaml 中的 Canvas,但我做不到。如何在classCS_Line.cs.
中调用名字的canvasMainPage.xaml:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Canvas x:Name="MyCanvas" HorizontalAlignment="Left" Height="573" Margin="154,76,0,0" VerticalAlignment="Top" Width="913" Background="White"/>
</Grid>
在CS_Line.cs中:
class CS_Line
{
//attribute
InkManager _inkKhaled = new Windows.UI.Input.Inking.InkManager();
private uint _penID;
private uint _touchID;
private Point _previousContactPt;
private Point currentContacPt;
private double x1;
private double y1;
private double x2;
private double y2;
//method
private void MyCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
{
PointerPoint pt = e.GetCurrentPoint(MyCanvas); *//====>>>>> can't access the MyCanvas*
_previousContactPt = pt.Position;
PointerDeviceType pointerDevType = e.Pointer.PointerDeviceType;
if (pointerDevType == PointerDeviceType.Pen ||
pointerDevType == PointerDeviceType.Mouse &&
pt.Properties.IsLeftButtonPressed)
{
_inkKhaled.ProcessPointerDown(pt);
_penID = pt.PointerId;
e.Handled = true;
}
else if (pointerDevType == PointerDeviceType.Touch)
{
}
}
您可以从 sender
得到 MyCanvas
。
private void MyCanvas_PointerPressed(object sender, PointerRoutedEventArgs e)
{
Canvas myCanvas = sender as Canvas; // Here's your MyCanvas object
PointerPoint pt = e.GetCurrentPoint(myCanvas);
_previousContactPt = pt.Position;