简体   繁体   中英

How can i link a canvas on a grid, to the mainwindow.xaml?

I have a canvas in a grid, i want to keep my canvas on that grid, because its the first window, that opens in my program.

In my MainWindow.xaml, i have a ContentPage , that always changes its content, the startup content is the authenticationPage. In this page i have a Canvas that shows my skeletal tracking, and is used for making a gesture. This gestureCanvas is on my authenticationPage . The code behind this gestureCanvas is on my MainWindow.xaml.cs.

I need to link the gestureCanvas with my MainWindow.xaml.cs, because the code is behind MainWindow , and it's going to be used there, because it's an Kinect application.

How to link these ?

partial class MainWindow
{
    void LoadCircleGestureDetector()
    {
        using (Stream recordStream = File.Open(circleKBPath, FileMode.OpenOrCreate))
        {
            circleGestureRecognizer.TraceTo(gesturesCanvas, Colors.Red);
        }
    }
 }

This is my authenticationPage

         <UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:smartHome2011"
xmlns:MyUserControl="clr-namespace:MyUserControl;assembly=MyUserControl"
mc:Ignorable="d"
x:Class="smartHome2011.AuthenticationPage"
x:Name="UserControl"
d:DesignWidth="640" d:DesignHeight="480">

<Grid x:Name="LayoutRoot">
         <Grid x:Name="kinectGrid" HorizontalAlignment="Left">
            <Viewbox Margin="204,220,430,220">
                <Grid ClipToBounds="True" Margin="204,220,430,220">
                    **<Canvas x:Name="gesturesCanvas" />**
                    <Canvas x:Name="kinectCanvas"></Canvas>
                </Grid>
            </Viewbox>
        </Grid>
    </Grid>

at your code behind MainWindow you can try following

var gesturesCanvas = YourContentPage.FindName("gesturesCanvas") as Canvas;
if (gesturesCanvas != null) {
  // do something
}

hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM