如何在WPF中的页面之间传递变量数据

时间:2019-06-18 13:54:01

标签: c# wpf xaml

我正在尝试将x和y坐标从一个页面传递到wpf中的另一页面。第一页使用x和y创建一个矩形。现在,我想使用该数据绘制线条。

我尝试调用第一页的新实例,然后从新实例中引用变量,但是在运行它时网格上没有显示任何行。

从第1页:

private double xval;

    public double xcoord
    {
        get => xval;
        set
        {
            xval = value;
            CreateARectangle();
        }
    }

    private double yval;

    public double ycoord
    {
        get => yval;
        set
        {
            yval = value;
            CreateARectangle();
        }
    }
    Rectangle rect = null;
    public void CreateARectangle(){
        if (rect == null)
        {
            // Creates a Rectangle  
            rect = new Rectangle();
            rect.Height = ycoord;
            rect.Width = xcoord;
            // Create a Brush  
            SolidColorBrush colorbrush = new SolidColorBrush();
            colorbrush.Color = Colors.Red;
            colorbrush.Opacity = .3;
            SolidColorBrush blackBrush = new SolidColorBrush();
            blackBrush.Color = Colors.Black;
            // Set Rectangle's width and color  
            rect.StrokeThickness = 1;
            rect.Stroke = blackBrush;
            // Fill rectangle with color  
            rect.Fill = colorbrush;
            // Add Rectangle to the Grid.  
            can.Children.Add(rect);
        }
        else
        {
            rect.Height = ycoord;
            rect.Width = xcoord;
        }
    }

从第2页开始:

public void drawLine()
     {
        ///top line in rectangle
        Line results = new Line();
        ///will be the bottom line in the new rectangle
        Line bottom = new Line();
        ///left connecting line
        Line left = new Line();
        ///right connecting line
        Line right = new Line();

        double test;
        FEModel fe = new FEModel();
        test=fe.xcoord;

        ///top line code
        results.Stroke = System.Windows.Media.Brushes.Red;
        results.Opacity =.3;
        results.X1 = test;
        results.X2 = test;
        results.Y1 = test;
        results.Y2 = test;
        results.HorizontalAlignment = HorizontalAlignment.Left;
        results.VerticalAlignment = VerticalAlignment.Center;
        results.StrokeThickness = 2;
        resultscan.Children.Add(results);

我只是在X1,X2,Y1和Y2中使用了测试变量,只是为了查看它是否确实在工作……那不是。

它可以编译,但是屏幕上没有显示任何行。当从第1页构建矩形时,Xcoord和Ycoord变量可以很好地工作。

0 个答案:

没有答案