Why can't I access a variable from my event handler?

I am creating a project where I am creating an entire project (including a template) using C # in Visual Studio.

I created a template and just started working on events, I want to create a MouseLeftButtonDown event for the created canvas, this event will register the mouse position.

This is my code:

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private char tester;
        private Point down1;
        private Point up1;

        private void Grid1_Loaded(object sender, RoutedEventArgs e)
        {
            RowDefinition row1 = new RowDefinition();
            RowDefinition row2 = new RowDefinition();

            row1.Height = new GridLength(40);
            row2.Height = new GridLength(1, GridUnitType.Star);

            Grid1.RowDefinitions.Add(row1);
            Grid1.RowDefinitions.Add(row2);

            Canvas canvas1 = new Canvas();
            canvas1.Background = new SolidColorBrush(Colors.Beige);
            Grid.SetRow(canvas1, 1);
            Grid1.Children.Add(canvas1);
            canvas1.MouseLeftButtonDown += new RoutedEventHandler(canvas1_MouseLeftButtonDown);
            canvas1.MouseLeftButtonUp += new RoutedEventHandler(canvas1_MouseLeftButtonUp);


            Button btnRect = new Button();
            btnRect.Content = "Rectangulo";
            btnRect.Margin = new Thickness(5, 5, 5, 5);
            btnRect.HorizontalAlignment = HorizontalAlignment.Left;
            btnRect.VerticalAlignment = VerticalAlignment.Center;
            Grid.SetRow(btnRect, 0);
            Grid1.Children.Add(btnRect);
            //btnRect.Click += new RoutedEventHandler(btnRect_Click);

            Button btnEllip = new Button();
            btnEllip.Content = "Ellipse";
            btnEllip.Margin = new Thickness(75, 5, 5, 5);
            btnEllip.HorizontalAlignment = HorizontalAlignment.Left;
            btnEllip.VerticalAlignment = VerticalAlignment.Center;
            Grid.SetRow(btnEllip, 0);
            Grid1.Children.Add(btnEllip);
            //btnEllip.Click += new RoutedEventHandler(btnEllip_Click);

        }

And this is my event:

private void canvas1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                 down1 = e.GetPosition(canvas1); 
            }
        }
    }

I tried to do it down1 = e.GetPosition(canvas1);, but I get an error (canvas1); "The name canvas1 does not exist in the current context"

I did a similar project without having to code the template without any problems ..

+3
source share
5 answers

but I get an error (canvas1);

, canvas1 , canvas1_MouseLeftButtonDown().

:

 Canvas c = sender as Canvas;  // or Canvas c = (Canvas) sender;
 down1 = e.GetPosition(c); 
+2

canvas1 Grid1_Loaded. - , .

0

What about:

down1 = e.GetPosition((Canvas)sender); 
0
source

The problem is that canvas1 is a local variable in Grid1_Loaded. There is no way to access this variable outside of this method.

0
source

The problem is that canvas1it is not included in the volume. You could include a simple lambda expression like this:

canvas1.MouseLeftButtonDown += (sender, e) => down1 = e.GetPosition(canvas1);
0
source

Source: https://habr.com/ru/post/1784123/


All Articles