2017-07-31 81 views
-1

我有wpf解決方案,其中我創建了UserControl進行趨勢分析。這個UserControl在MainWindow中使用。使用SizeChanged事件時在主窗口中出現異常

趨勢的路徑繪製在當前的class ChartControl的showData()方法上。但是因爲我想要獲得與主窗口大小相關的路徑的實際圖片,所以我在該showData()方法被調用的地方添加了0​​事件。

我對事件這裏代碼:

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:ChartControl的 xaml內容主的窗口

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

    </Grid> 

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> 

程序啓動後,一切工作正常,並根據期望,但我在視覺工作室的視圖設計師,這是消除任何設計更改異常。

exception

+0

Thx for downvote,但有些提示,爲什麼它的壞問題將是有益的。 –

+0

我猜stacktrace是相當豐富的。 'chart.lib.ChartCOntrol.maxVal()'使用'Linq.Max',但它沒有元素 –

+0

很難說,看不到showData的代碼。 – gdir

回答

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>()你得到一個異常。

您在ShowData()的最開始處打電話給MaxVal(),據我所知,沒有真正可辨別的地方,您將任何ChartData添加到數據列表。

+0

好吧,我會盡量避免這種命名失敗,但仍然,我得到異常,不是在應用程序啓動後,只在視圖設計師,應用程序工作正常。這個數據在應用程序啓動後立即添加。我試圖刪除調用maxVal()方法,但沒有區別。 –

+0

堆棧跟蹤顯示它正在執行Resize-> showData-> maxVal;所以當設計師進入時,maxVal失敗,因爲數據收集是空的。 – Clint

+0

感謝我得到它,它幫助,但我有類似的方法來計算與列表的東西,所以我必須以不同的方式編碼。 –

1

OnResize事件處理程序中,您必須測試當前實例未處於設計模式。

System.ComponentModel.DesignerProperties.GetIsInDesignMode(this) 

如果你沒有數據然後返回...那麼沒有更多的設計異常!

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

這個方法應該使用什麼名字空間?我在使用這個時遇到錯誤。 –

+0

System.ComponentModel.DesignerProperties.GetIsInDesignMode(this) – GCamel

+0

謝謝,這也是一個很好的解決方案,但它只是我的壞方法的修復,更正確的可能是弄清楚如何在沒有數據的情況下不調用方法。 –

相關問題