2013-06-11 36 views
1

我想設計什麼是一個非常簡單的界面,顯示一些圖像,然後評級按鈕。這是XAML。爲什麼我的WPF窗口加載緩慢?

<Window x:Class="ASU_Evaluation2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local ="clr-namespace:ASU_Evaluation2" 
     Title="MainWindow" Height="686" Width="1154" Loaded="Window_Loaded" Closing="Window_Closing"> 

    <Grid Name="ReviewData" > 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="200*"/> 
      <ColumnDefinition Width="205"/> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="474*" /> 
      <RowDefinition Height="25*" /> 
     </Grid.RowDefinitions> 

     <StackPanel Grid.Column="1" Grid.RowSpan="2"> 

      <ToggleButton Content="Cell is good" Name="tbGoodCell" Height="30" Margin="1,1" IsChecked="{Binding Cell_Good}" Width="200"/> 
      <ToggleButton Content="Recon is good" Name="tbReconGood" Height="30" Margin="1,1" IsChecked="{Binding Recon_Good}" Width="200"/> 
      <ToggleButton Content="Stain is good" Name="tbStainCell" Height="30" Margin="1,1" IsChecked="{Binding GoodStain}" Width="200"/> 
      <ToggleButton Content="Recon Clipped" Name="tbReconClipped" Height="30" Margin="1,1" IsChecked="{Binding Clipping}" Width="200"/> 
      <ToggleButton Content="Interesting" Name="tbInteresting" Height="30" Margin="1,1" IsChecked="{Binding Interesting}" Width="200"/> 
      <ToggleButton Content="Background Noise" Name="tbNoisy" Height="30" Margin="1,1" IsChecked="{Binding Noisy}" Width="200"/> 
      <ToggleButton Content="Has Rings" Name="tbRings" Height="30" Margin="1,1" IsChecked="{Binding Rings}" Width="200"/> 
      <Label Content="Interfering Object" Margin="1,10,1,1"></Label> 

      <ListBox Name="tbInterferingObject" Height="70" SelectedIndex="{Binding InterferingObject}" Width="200"> 
      <ListBoxItem>None   </ListBoxItem> 
       <ListBoxItem>Close   </ListBoxItem> 
       <ListBoxItem>Far   </ListBoxItem> 
       <ListBoxItem>Side </ListBoxItem> 
      </ListBox> 

      <Label Content="Comments"/> 
      <TextBox Margin="1,5" Height="400" Name="Comments" Text="{Binding Comments}" Width="200"/> 

     </StackPanel> 

     <ComboBox Grid.Row="2" Grid.Column="0" x:Name="themes" Width="150" Height="20" SelectionChanged="themes_SelectionChanged" SelectedIndex="0" /> 
     <TabControl Grid.Column="0" Name="tabControl1" > 
      <TabItem Header="Recon View" Name="tabItem1"> 
       <Grid Height="612" Width="934"> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="225" /> 
         <ColumnDefinition Width="225" /> 
         <ColumnDefinition Width="225" /> 
         <ColumnDefinition Width="225*" /> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="291" /> 
         <RowDefinition Height="291" /> 
        </Grid.RowDefinitions> 
        <Image Grid.Column="0" Grid.Row="0" Margin="2,2" Name="MIP_Image" Height="210" Width="210"/> 
        <Image Grid.Column="1" Grid.Row="0" Margin="2,2" Name="Axial_Image" Source="{Binding Axial_Image}" Height="210" Width="210"/> 
        <Image Grid.Column="0" Grid.Row="1" Margin="2,2" Name="Sag_Image" Source="{Binding Sag_Image}" Height="210" Width="210"/> 
        <Image Grid.Column="1" Grid.Row="1" Margin="2,2" Name="Z_Image" Source="{Binding Z_Image}" Height="210" Width="210"/> 
        <Image Grid.Column="2" Grid.Row="0" Margin="2,2" Name="Fly_Through" Height="210" Width="210"/> 
        <Image Grid.Column="2" Grid.Row="1" Grid.ColumnSpan="2" Margin="2,2" Name="Background" Source="{Binding Background}" Height="250" Width="450"/> 
       </Grid> 
      </TabItem> 
      <TabItem Header="Alignment View" Name="tabItem2"> 
       <Grid > 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="460" /> 
         <ColumnDefinition Width="460" /> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="190" /> 
         <RowDefinition Height="190" /> 
         <RowDefinition Height="190" /> 
        </Grid.RowDefinitions> 
        <Image Grid.Column="0" Grid.Row="0" Margin="2,2" Name="PP1" Source="{Binding PP1}" Width="460" Height="190" /> 
        <Image Grid.Column="0" Grid.Row="1" Margin="2,2" Name="PP2" Source="{Binding PP2}" Width="460" Height="190" /> 
        <Image Grid.Column="0" Grid.Row="2" Margin="2,2" Name="PP3" Source="{Binding PP3}" Width="460" Height="190" /> 
        <Image Grid.Column="1" Grid.Row="0" Grid.RowSpan="3" Margin="2,2" Name="Centering" HorizontalAlignment="Center" VerticalAlignment="Center" Width="400" MinHeight="400" Source="{Binding Centering}"/> 

       </Grid> 
      </TabItem> 
      <TabItem Header="Stack View" Name="tabItem3" GotFocus="tabItem3_GotFocus"> 
       <Grid > 
        <Grid.RowDefinitions> 
         <RowDefinition Height="418*" /> 
         <RowDefinition Height="24*" /> 
        </Grid.RowDefinitions> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="581*" /> 
         <ColumnDefinition Width="207*" /> 
        </Grid.ColumnDefinitions> 
        <Image Name="Stack_Image" VerticalAlignment="Center" HorizontalAlignment="Center" MinHeight="100" MinWidth="100" Height="400" Margin="98,26,82,15" Width="400" /> 
        <ScrollBar Grid.Column="1" Height="353" HorizontalAlignment="Left" Margin="36,50,0,0" Name="StackZPosition" VerticalAlignment="Top" Width="17" ValueChanged="StackZPosition_ValueChanged" /> 
        <Label Content="Z Position" Grid.Column="1" Height="27" HorizontalAlignment="Left" Margin="13,17,0,0" Name="label1" VerticalAlignment="Top" Width="79" /> 
        <ProgressBar Grid.Row="1" Height="14" HorizontalAlignment="Left" Margin="95,4,0,0" Name="StackProgress" VerticalAlignment="Top" Width="404" /> 
       </Grid> 
      </TabItem> 
      <TabItem Header="Othogonal View" Name="tabItem4"> 
       <Grid > 
        <Grid.RowDefinitions> 
         <RowDefinition Height="418*" /> 
         <RowDefinition Height="24*" /> 
        </Grid.RowDefinitions> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="581*" /> 
         <ColumnDefinition Width="207*" /> 
        </Grid.ColumnDefinitions> 

       </Grid> 
      </TabItem> 
     </TabControl> 
    </Grid> 
</Window> 

綁定看起來像這樣

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Threading.Tasks; 
using System.Drawing; 

namespace ASU_Evaluation2 
{ 
class DatasetExample 
{ 
     public string Date { get { return "3/3/3"; } } 
    public ImageSource ExampleImage 
    { 
     get 
     { 
      string exampleImage = TopDirectory + @"\data\CrossSections_X___TIK.jpg"; 

      if (File.Exists(exampleImage)) 
       return BitmapFrame.Create(new Uri(exampleImage, UriKind.Absolute), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); 
      else 
       return null; 
     } 
    } 
    public bool Recon_Succeeded { get { return true; } } 
    public bool Cell_Good 
    { 
     get; 
     set; 
    } 
    public bool Recon_Good 
    { 
     get; 
     set; 
    } 
    public string DatasetName { get; private set; } 
    public string TopDirectory { get; private set; } 


    string VGFolder; 
    string StorageFolder; 
    string DehydrateFolder; 
    string BackupFolder; 
    string StackFolder; 
    string StackReportFilePath; 

    private void BuildPaths() 
    { 
     string dirName = Path.GetFileNameWithoutExtension(TopDirectory); 
     //return dirName; 
     string[] parts = dirName.Split('_'); 
     string Prefix = parts[0]; 
     string Year = parts[1].Substring(0, 4); 
     string month = parts[1].Substring(4, 2); 
     string day = parts[1].Substring(6, 2); 

     VGFolder = Path.Combine(@"y:\", Prefix + "\\" + Year + month + "\\" + day + "\\" + dirName); 
     StorageFolder = Path.Combine(@"z:\", Prefix + "\\" + Year + month + "\\" + day + "\\" + dirName); 
     DehydrateFolder = Path.Combine(@"e:\", Prefix + "\\" + Year + month + "\\" + day + "\\" + dirName); 
     BackupFolder = Path.Combine(@"V:\BackupCompleted\", Prefix + "\\" + Year + month + "\\" + day + "\\" + dirName); 

     if (Prefix.ToLower() == "cct001") 
     { 
      StackFolder = Path.Combine(@"V:\Raw PP\cct001\Absorption\", Year + month + "\\" + day + "\\" + dirName + "\\STACK\\000"); 
     } 
     else 
      StackFolder = Path.Combine(@"V:\Raw PP\", Prefix + "\\" + Year + month + "\\" + day + "\\" + dirName + "\\STACK\\000"); 

     StackReportFilePath = VGFolder + "\\FixedStackReport.xml"; 
    } 

    public DatasetExample(string name, string path) 
    { 
     DatasetName = name; 
     TopDirectory = path; 
     GoodStain = true; 
     Clipping = false; 
     Interesting = false; 
     Noisy = false; 
     Rings = false; 
     InterferingObject = 0; 
     Comments = " "; 
     Evaluator = "Brian"; 
     Cell_Good = true; 
     CellType = " "; 
     Recon_Good = true; 

    } 

    public string Evaluator { get; set; } 
    public string CellType { get; set; } 
    public bool GoodStain { get; set; } 
    public bool Clipping { get; set; } 
    public bool Interesting { get; set; } 
    public bool Noisy { get; set; } 
    public bool Rings { get; set; } 
    public int InterferingObject { get; set; } 
    public string Comments { get; set; } 



    public ImageSource Axial_Image 
    { 
     get 
     { 
      string exampleImage = TopDirectory + @"\data\CrossSections_X___TIK.jpg"; 

      if (File.Exists(exampleImage)) 
       return BitmapFrame.Create(new Uri(exampleImage, UriKind.Absolute), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); 
      else 
       return null; 
     } 
    } 
    public ImageSource Sag_Image 
    { 
     get 
     { 
      string exampleImage = TopDirectory + @"\data\CrossSections_Y___TIK.jpg"; 

      if (File.Exists(exampleImage)) 
       return BitmapFrame.Create(new Uri(exampleImage, UriKind.Absolute), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); 
      else 
       return null; 
     } 
    } 
    public ImageSource Z_Image 
    { 
     get 
     { 
      string exampleImage = TopDirectory + @"\data\CrossSections_Z___TIK.jpg"; 

      if (File.Exists(exampleImage)) 
       return BitmapFrame.Create(new Uri(exampleImage, UriKind.Absolute), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); 
      else 
       return null; 
     } 
    } 


    public void ClearMemory() 
    { 



    } 

    public ImageSource Background 
    { 
     get 
     { 
      string exampleImage = TopDirectory + @"\data\background.tif"; 

      if (File.Exists(exampleImage)) 
       return BitmapFrame.Create(new Uri(exampleImage, UriKind.Absolute), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); 
      else 
       return null; 
     } 
    } 
    public ImageSource PP1 
    { 
     get 
     { 
      string exampleImage = TopDirectory + @"\data\projection1.jpg"; 

      if (File.Exists(exampleImage)) 
       return BitmapFrame.Create(new Uri(exampleImage, UriKind.Absolute), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); 
      else 
       return null; 
     } 
    } 
    public ImageSource PP2 
    { 
     get 
     { 
      string exampleImage = TopDirectory + @"\data\projection2.jpg"; 

      if (File.Exists(exampleImage)) 
       return BitmapFrame.Create(new Uri(exampleImage, UriKind.Absolute), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); 
      else 
       return null; 
     } 
    } 
    public ImageSource PP3 
    { 
     get 
     { 
      string exampleImage = TopDirectory + @"\data\projection3.jpg"; 

      if (File.Exists(exampleImage)) 
       return BitmapFrame.Create(new Uri(exampleImage, UriKind.Absolute), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); 
      else 
       return null; 
     } 
    } 
    public string Centering 
    { 
     get 
     { 
      string exampleImage = TopDirectory + @"\data\centering.avi"; 

      if (File.Exists(exampleImage)) 
       return exampleImage; 
      else 
       return ""; 
     } 
    } 

    public ImageSource Stack_Image 
    { 
     get 
     { 
      string exampleImage = TopDirectory + @"\data\CrossSections_Y___TIK.jpg"; 

      if (File.Exists(exampleImage)) 
       return BitmapFrame.Create(new Uri(exampleImage, UriKind.Absolute), BitmapCreateOptions.None, BitmapCacheOption.OnLoad); 
      else 
       return null; 
     } 
    } 

} 

類}

這需要15-20秒來加載一個高端計算機上的接口。我試圖用windows SDK運行性能監視器工具,它只是崩潰。我究竟做錯了什麼?這在winforms中是一件令人尷尬的簡單任務。

+3

your're在UI線程上做了很多昂貴的東西(文件..等)上的IO操作),在不同的線程(而不是主線程UI線程)上執行所有操作,並通過PropertyChanged,事件,或者什麼時候完成處理。 –

+0

我認爲你應該使用另一個事件(顯示窗口後發射)。 – Kamil

+1

我做了一個測試,並採取了相同的數據集類,並用它來加載一個winform應用程序(更改位圖bitmapsource)https://dl.dropboxusercontent.com/u/63600049/Interface2.png winforms接口加載1- 3秒,而WPF界面需要45-60秒。 IO的處理方式不同嗎? – ochensati

回答

2

首先,你的屬性每次調用時都返回一個新實例ImageSource。你應該避免這一點。

  • 修復1:

    private ImageSource sag_Image; 
    public ImageSource Sag_Image 
    { 
        get 
        { 
         if (sag_Image != null) 
          return sag_Image; 
    
         //... Etc your code 
    
         sag_Image = //Assign the backing field to use it later. 
        } 
    } 
    

你應該Freeze your Freezables(如ImageSource)。

  • 修復2:

    public ImageSource Sag_Image 
    { 
        get 
        { 
         if (sag_Image != null) 
          return sag_Image; 
    
         //... Etc your code 
    
         sag_Image = //Assign the backing field to use it later. 
    
         sag_Image.Freeze(); //Notice the Freeze() method. 
        } 
    } 
    
  • 修復3:

    異步。所有需要超過10毫秒才能完成的操作是SHOULD是異步的。

三,你的XAML看起來就像是從Visual Studio的設計師得到的,也有這樣的ScrollBar.ValueChanged事件一些可疑的東西。那是幹什麼的?發佈相關的代碼,並可能是你需要的屏幕截圖。我相信有一個正確的方法可以做到這一點,WPF不涉及可怕的代碼隱藏winforms類型的黑客,並沒有顯示你在這裏描述的性能問題。

+0

我同意,但除了你的建議 - 也許他應該使用單獨的線程來做那個時間 - 昂貴的東西? – Kamil

+0

這是第一次加載後的結果。 https://dl.dropboxusercontent.com/u/63600049/Interface。png 慢動作負載在第一個負載(以及以下所有)上,所以我不是說你的建議對於新的負載適用於第一負載(我將用它來負載以下負載) 我試圖儘可能多的代碼到線程,但我仍然得到相同的可怕的表現(在左側的樹形視圖中) 我有一個winform和Java應用程序加載相同數量的圖像,並把它們放在屏幕在一小部分時間。只希望冷卻GUI – ochensati

+0

@ kamil編輯答案並補充說。謝謝 –