使用SizeChanged事件时主窗口中的异常

时间:2017-07-31 13:04:35

标签: c# wpf

我有wpf解决方案,我在其中创建了UserControl以进行趋势分析。这个UserControl在MainWindow中使用。

趋势路径绘制在当前class ChartControl的方法showData()上。但是因为我希望得到与主窗口大小相关的路径的实际图片,所以我添加了SizeChanged事件,其中调用了这个showData()方法。

我的活动代码:

private void OnResize(object sender, SizeChangedEventArgs e)
        {
            this.showData();
        }

编辑:

   private List<ChartData> data = new List<ChartData>();
   public void showData()
            {
                double maxVal = this.maxVal();
                double minVal = this.minVal();
                TimeSpan timeSpan = new TimeSpan();
                timeSpan = this.maxTime() - this.minTime();
                double stepSize = Area.ActualWidth / timeSpan.TotalSeconds;

                setLabels();

                Area.Children.Clear();

                for (int i = 1; i < this.data.Count; i++)
                {
                    Line lineHorizont = new Line();
                    lineHorizont.StrokeThickness = 2;
                    lineHorizont.Stroke = Brushes.Red;

                    lineHorizont.X1 = (this.data[i].X - this.minTime()).TotalSeconds * stepSize;
                    lineHorizont.Y1 = Math.Abs(((this.data[i - 1].Y - minVal) / (maxVal - minVal) * Area.ActualHeight) - Area.ActualHeight);

                    lineHorizont.X2 = lineHorizont.X1;
                    lineHorizont.Y2 = Math.Abs(((this.data[i].Y - minVal) / (maxVal - minVal) * Area.ActualHeight) - Area.ActualHeight);

                    Area.Children.Add(lineHorizont);

                    Line lineVertical = new Line();
                    lineVertical.StrokeThickness = 2;
                    lineVertical.Stroke = Brushes.Red;

                    lineVertical.X1 = (this.data[i - 1].X - this.minTime()).TotalSeconds * stepSize;
                    lineVertical.Y1 = Math.Abs(((this.data[i - 1].Y - minVal) / (maxVal - minVal) * Area.ActualHeight) - Area.ActualHeight);

                    lineVertical.X2 = (this.data[i].X - this.minTime()).TotalSeconds * stepSize;
                    lineVertical.Y2 = lineVertical.Y1;

                    Area.Children.Add(lineVertical);
                }

                //Draw cross coordinator
                coordX1.StrokeThickness = 1;
                coordX1.Stroke = Brushes.Black;
                coordX1.X1 = 0;
                coordX1.Y1 = Mouse.GetPosition(Area).Y;
                coordX1.X2 = Area.ActualWidth;
                coordX1.Y2 = coordX1.Y1;
                Area.Children.Add(coordX1);

                coordX2.StrokeThickness = 1;
                coordX2.Stroke = Brushes.Black;
                coordX2.X1 = Mouse.GetPosition(Area).X;
                coordX2.Y1 = 0;
                coordX2.X2 = coordX2.X1;
                coordX2.Y2 = Area.ActualHeight;
                Area.Children.Add(coordX2);
            }

public double maxVal()
        {
            List<double> data = new List<double>();
            for (int i = 0; i < this.data.Count; i++)
            {
                data.Add(this.data[i].Y);
            }
            return data.Max<double>();
        }

EDIT2: 主窗口的xaml内容

<Grid Margin="0">
        <lib:ChartControl x:Name="Trend" Margin="0" Width="Auto" Height="Auto"/>

    </Grid>
ChartControl的

xaml内容

<Grid Grid.Column="1" Margin="0" Grid.Row="1" Background="Black" Cursor="Cross" PreviewMouseMove="OnMouseMove">
            <Grid.RowDefinitions>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>
            <Rectangle Fill="#FFF1F1F1" Grid.RowSpan="10"/>
            <Rectangle Fill="#FFD4D4D4"/>
            <Rectangle Fill="#FFD4D4D4" Grid.Row="2"/>
            <Rectangle Fill="#FFD4D4D4" Grid.Row="4"/>
            <Rectangle Fill="#FFD4D4D4" Grid.Row="6"/>
            <Rectangle Fill="#FFD4D4D4" Grid.Row="8"/>
            <Canvas x:Name="Area" Grid.RowSpan="10"/>
        </Grid>

程序启动后一切正常并且符合预期,但我在Visual Studio的View Designer中得到了Exception,这消除了任何设计变更。

exception

2 个答案:

答案 0 :(得分:1)

OnResize事件处理程序中,您必须测试当前实例是否处于设计模式。

System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)

如果你没有数据然后返回...那么没有更多的设计异常!!

private void OnResize(object sender, SizeChangedEventArgs e) {
    if(!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
        this.showData();
}

答案 1 :(得分:0)

 List<double> data = new List<double>();
 for (int i = 0; i < this.data.Count; i++)
 {
   data.Add(this.data[i].Y);
 }
 return data.Max<double>();

请,请不要将您的本地变量命名为与您的外部变量相同。

至于为何失败,显而易见:

调用此方法时,

this.data为空,因此,当您尝试执行data.Max<double>()时会出现异常。

您在MaxVal()的最开始就打电话给ShowData(),据我所知,您没有真正可辨别的地方ChartData添加任何Calendar{ id:calendarioDX frameVisible :false width:dp(260) height:parent.height*0.26 anchors.right: parent.right; anchors.rightMargin: parent.width*0.03 anchors.verticalCenter: parent.verticalCenter } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> <link rel="stylesheet" type="text/css" href="stylesheet.css"> <link/> </head> <body onload="startGame()"> <h2> My Lost Game </h2> <p>Press the 'up', 'down', 'left' and 'right' keys on your keyboard to move. Avoid the obstacles and reach the goal.</p><br> <p></p> <script> var myObstacle = []; function startGame() { myGameArea.start(); myGameGoal = new component(75, 95, "player2.jpg", 710, 215, "image"); myGamePiece = new component(75, 95, "player.jpg", 10, 215, "image"); myObstacle2 = new component (110, 150, "obstacle2.jpg", 350, 0, "image"); myObstacle3 = new component (110, 150, "obstacle3.jpg", 530, 350, "image"); } var myGameArea = { canvas : document.createElement("canvas"), start : function() { this.canvas.width = 800; this.canvas.height = 500; this.context = this.canvas.getContext("2d"); document.body.insertBefore(this.canvas, document.body.childNodes[4]); this.frameNo = 0; this.interval = setInterval(updateGameArea, 20); window.addEventListener('keydown', function (e) { myGameArea.keys = (myGameArea.keys || []); myGameArea.keys[e.keyCode] = true; }) window.addEventListener('keyup', function (e) { myGameArea.keys[e.keyCode] = false; }) }, clear : function() { this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);}, stop : function() { clearInterval(this.interval); } } function component(width, height, color, x, y, type) { this.type = type; if (type == "image") { this.image = new Image(); this.image.src = color; } this.width = width; this.height = height; this.x = x; this.y = y; this.update = function() { ctx = myGameArea.context; if (type == "image") { ctx.drawImage(this.image, this.x, this.y, this.width, this.height); } else { ctx.fillStyle = color; ctx.fillRect(this.x, this.y, this.width, this.height); } } this.newPos = function() { this.x += this.speedX; this.y += this.speedY; } this.crashWith = function(otherobj) { var myleft = this.x; var myright = this.x + (this.width); var mytop = this.y; var mybottom = this.y + (this.height); var otherleft = otherobj.x; var otherright = otherobj.x + (otherobj.width); var othertop = otherobj.y; var otherbottom = otherobj.y + (otherobj.height); var crash = true; if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) { crash = false; } return crash; } } function updateGameArea() { var x, y; for (i = 0; i < myObstacle.length; i += 1) { if (myGamePiece.crashWith(myObstacle[i])) { myGameArea.stop(); return; } } myGameArea.clear(); myGameArea.frameNo += 1; if (myGameArea.frameNo == 1 || everyinterval(150)) { x = 200; y = 300; myObstacle.push(new component(110, 150, "obstacle1.jpg", x, y, "image")); } for (i = 0; i < myObstacle.length; i += 1) { myObstacle[i].y += -1; myObstacle[i].update(); } myGameArea.clear(); myGamePiece.speedX = 0; myGamePiece.speedY = 0; if (myGameArea.keys && myGameArea.keys[37]) {myGamePiece.speedX = -3; } if (myGameArea.keys && myGameArea.keys[39]) {myGamePiece.speedX = 3; } if (myGameArea.keys && myGameArea.keys[38]) {myGamePiece.speedY = -3; } if (myGameArea.keys && myGameArea.keys[40]) {myGamePiece.speedY = 3; } myObstacle2.update(); myObstacle3.update(); myGameGoal.update(); myGamePiece.newPos(); myGamePiece.update(); } function everyinterval(n) { if ((myGameArea.frameNo / n) % 1 == 0) {return true;} return false; } </script> </body> </html> 1}}到数据列表。