LayoutRoot名称在当前上下文中不存在

时间:2014-08-23 12:59:26

标签: c# wpf-controls

我收到这个erorr代码:"当前上下文中没有名称LayoutRoot"。

我还没有在Xaml文件中写任何控件,因为我无法找到控件" LayoutRoot"在toolBox中。我该如何解决这个问题?

这是我的代码:

using(SkeletonFrame frame = e.OpenSkeletonFrame())
{
if(frame != null)
{
Polyline figure;
Brush userBrush;
Skeleton skeleton;
**LayoutRoot.Children.Clear();**
frame.CopySkeletonDataTo(this._FrameSkeletons);
for(int i = 0; i < this._FrameSkeletons.Length; i++)
{
skeleton = this._FrameSkeletons[i];
if(skeleton.TrackingState == SkeletonTrackingState.Tracked)
{
userBrush = this._SkeletonBrushes[i % this._SkeletonBrushes.Length];
//Draws the skeleton’s head and torso
joints = new [] { JointType.Head, JointType.ShoulderCenter,
JointType.ShoulderLeft, JointType.Spine,
JointType.ShoulderRight, JointType.ShoulderCenter,
JointType.HipCenter, JointType.HipLeft,
JointType.Spine, JointType.HipRight,
JointType.HipCenter });
**LayoutRoot.Children.Add**(CreateFigure(skeleton, userBrush, joints));
//Draws the skeleton’s left leg
joints = new [] { JointType.HipLeft, JointType.KneeLeft,
JointType.AnkleLeft, JointType.FootLeft };
**LayoutRoot.Children.Add**(CreateFigure(skeleton, userBrush, joints));
//Draws the skeleton’s right leg
joints = new [] { JointType.HipRight, JointType.KneeRight,
JointType.AnkleRight, JointType.FootRight };
LayoutRoot.Children.Add(CreateFigure(skeleton, userBrush, joints));
//Draws the skeleton’s left arm
joints = new [] { JointType.ShoulderLeft, JointType.ElbowLeft,
JointType.WristLeft, JointType.HandLeft };
LayoutRoot.Children.Add(CreateFigure(skeleton, userBrush, joints));
//Draws the skeleton’s right arm
joints = new [] { JointType.ShoulderRight, JointType.ElbowRight,
JointType.WristRight, JointType.HandRight };
LayoutRoot.Children.Add(CreateFigure(skeleton, userBrush, joints));

1 个答案:

答案 0 :(得分:3)

您可以在xaml或代码本身中创建一些类似控件的面板

xaml示例

<Window x:Class="CSharpWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        WindowStartupLocation="CenterScreen">
    <Canvas x:Name="LayoutRoot" >
    </Canvas>
</Windows>

或代码

Canvas LayoutRoot = new Canvas();
using(SkeletonFrame frame = e.OpenSkeletonFrame())
{
    ...
}
//add LayoutRoot to the parent control etc.

我使用Canvas作为面板,假设您正在添加一些数字。您可以根据需要选择适当的控件。