当前上下文中不存在名称为LayoutRoot的内容

3

我遇到了这个错误代码:"The name LayoutRoot does not exist in the current context"。

因为我在工具箱中找不到控件“LayoutRoot”,所以我还没有在Xaml文件中编写任何控件。请问如何解决这个问题?

以下是我的代码:

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个回答

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作为面板,假设您要在其中添加一些图形。您可以根据需要选择适当的控件。


谢谢!现在没问题了,没有错误! - silviu oncioiu
太好了!愉快地编程 :) - pushpraj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接