在MainPage.xaml中,我有2个画布控件(bd1和ball),代表2层。
<Canvas x:Name="ContentCanvas" Width="354" Height="669">
<Canvas.Background>
<RadialGradientBrush>
<GradientStop Color="#FF9D0A0A" Offset="0" />
<GradientStop Color="Black" Offset="1" />
</RadialGradientBrush>
</Canvas.Background>
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="bd1" Width="370" Height="480"
Clip="F1 M 0,0L 640,0L 640,480L 0,480L 0,0" MouseMove="Canvas_MouseMove" HorizontalAlignment="Left" VerticalAlignment="Top">
<Canvas.RenderTransform>
<TransformGroup>
<RotateTransform Angle="0" CenterX="0.5" CenterY="0.5" />
<TranslateTransform X="0" Y="0" />
<ScaleTransform ScaleX="1" ScaleY="1" />
<SkewTransform AngleX="0" AngleY="0"/>
</TransformGroup>
</Canvas.RenderTransform>
<Path....../>
........
</Canvas>
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="ball"
Width="54" Height="52.5" Clip="F1 M 0,0L 54,0L 54,52.5L 0,52.5L 0,0"
MouseMove="Canvas_MouseMove" HorizontalAlignment="Left" VerticalAlignment="Top">
<Canvas.RenderTransform>
<TransformGroup>
<RotateTransform Angle="0" CenterX="0.5" CenterY="0.5" />
<TranslateTransform X="0" Y="0" />
<ScaleTransform ScaleX="1" ScaleY="1" />
<SkewTransform AngleX="0" AngleY="0"/>
</TransformGroup>
</Canvas.RenderTransform>
<Canvas Width="640" Height="480.425" Canvas.Left="0" Canvas.Top="0">
<Ellipse x:Name="Ellipse" Width="54" Height="52.5" Canvas.Left="4.17233e-007" Canvas.Top="2.03252e-005" Stretch="Fill" StrokeLineJoin="Round" Stroke="#FFFF0000">
<Ellipse.Fill>
<LinearGradientBrush StartPoint="0.0633299,0.762378" EndPoint="0.93667,0.237621">
<LinearGradientBrush.GradientStops>
<GradientStop Color="#FF000000" Offset="0"/>
<GradientStop Color="#FFFFFFFF" Offset="1"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Ellipse.Fill>
</Ellipse>
</Canvas>
</Canvas>
为此,我创建了一个名为Layer的类,它具有Canvas属性:
public class Layer
{
public Layer() { }
public Layer(Canvas canvas, Tipologie tip)
{
ThisCanvas = canvas; Tip = tip;
}
public string Name { get; set; }
private Canvas canv;
public Canvas ThisCanvas
{
get { return canv; }
set
{
canv = value;
if (value != null)
Name = value.GetValue(Canvas.NameProperty) as string;
}
}
}
在MainPage
课程中,我有Layer
类型的全局集合。最初我把2个图层放入集合中。
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
Layers[0] = new Layer(); Layers[0].ThisCanvas = GetTemplateChild("bd1") as Canvas;
Layers[1] = new Layer(); Layers[1].ThisCanvas = ball;
LayerSelector.ItemsSource = Layers;
}
Layer[] Layers = new Layer[2];
问题是bd1和球形画布控件是null
。我也尝试将代码放在页面的构造函数中,但这是同样的问题。
我无法解释。
我做错了吗?提前谢谢!
答案 0 :(得分:0)
如果你在构造函数中传递Canvas,那么除非你把它重新分配给其他地方,否则它永远不会为null。
public class Layer {
public Layer(Canvas canvas) {
this.canvas = canvas;
}
private Canvas canvas;
public Canvas ThisCanvas {
get { return canvas; }
set { canvas = value; }
}
...
}
...
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) {
Layers[0] = new Layer(GetTemplateChild("bd1") as Canvas);
Layers[1] = new Layer(ball);
LayerSelector.ItemsSource = Layers;
}
Layer[] Layers = new Layer[2];