编程语言
首页 > 编程语言> > c#-名称LayoutRoot在当前上下文中不存在

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

作者:互联网

我收到以下erorr代码:“名称LayoutRoot在当前上下文中不存在”.

我还没有在Xaml文件中编写任何控件,因为在toolBox中找不到控件“ LayoutRoot”.我怎么解决这个问题?

这是我的代码:

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));

解决方法:

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

标签:wpf-controls,c
来源: https://codeday.me/bug/20191121/2051069.html