2015-11-12 457 views
1

我開發一個WPF應用程序,允許兩個端點在一個視頻聊天連接從WPF消失。我已經被設計在XAML設計的UI在過去的兩個月,一切正常,當我跑它沒有顯示一切等直到昨天,對於完全沒有任何明顯原因的應用程序。 UI的一些部分的代碼時在另一臺筆記本電腦上運行只是不出來了。我堅持事實上,它之前已經工作,現在不再工作了。我也沒有改變代碼中可能與UI的那部分有關的任何東西,也沒有在xaml中做任何改變。UI元素在運行時

我沒有檢查過,我從來沒有搞錯代碼隱藏的任何可見性參數,所以我沒有看到任何應用程序的行爲突然改變的原因。

有沒有人有一個想法,爲什麼會發生這種情況?

這裏是XAML:

<Window x:Class="RealSenseiConfFusion.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="1000" Width="1500" 
    WindowStartupLocation="Manual" 
    Left="0" Top="0" 
    Closing="Window_Closing" 
    Loaded="Window_Loaded" > 
<Grid Background="LightSteelBlue"> 
    <Rectangle Margin="10,10,498.6,0" Name="rectangle1" Stroke="Black" RadiusX="9" RadiusY="9" Fill="LightSlateGray" Height="142" VerticalAlignment="Top" /> 
    <Grid Name="callAndSyncGrid" Margin="26,24,1165,835" Background="LightGray"> 
     <StackPanel> 
      <TextBlock HorizontalAlignment="Center">ABOUT CALL</TextBlock> 
      <TextBlock Name="myIpTextBlock">Your ip is:</TextBlock> 
      <Grid Name="callGrid"> 
       <TextBox Height="23" HorizontalAlignment="Left" Name="txtIP" Background="GhostWhite" Foreground="Black" Width="221" /> 
       <Button Height="23" HorizontalAlignment="Right" Name="btnCall" Width="75" Click="btnCall_Click">Call</Button> 
      </Grid> 
      <Button Name="btnSync" Content="Start Synchronization" HorizontalAlignment="Left" Width="120" Height="25" Click="btnSynchronize_Click"/> 
      <TextBlock Name="currentConvText"></TextBlock> 
     </StackPanel> 
    </Grid> 
    <Grid Name="visSetupGrid" Margin="650,24,515,835" Background="LightGray"> 
     <StackPanel> 
      <TextBlock HorizontalAlignment="Center">VISUALIZATION SETUP</TextBlock> 
      <Grid> 
       <TextBlock HorizontalAlignment="Left"> Visualization block 1:</TextBlock> 
       <ComboBox Name="vis1Combo" HorizontalAlignment="Right"> 
        <ComboBoxItem IsSelected="True">Vis1UC1</ComboBoxItem> 
        <ComboBoxItem>Vis1UC1_2</ComboBoxItem> 
        <ComboBoxItem>Vis1UC2</ComboBoxItem> 
        <ComboBoxItem>Vis1UC2_2</ComboBoxItem> 
        <ComboBoxItem>Vis2UC1</ComboBoxItem> 
        <ComboBoxItem>Vis2UC2</ComboBoxItem> 
        <ComboBoxItem>Vis2UC3</ComboBoxItem> 
        <ComboBoxItem>Vis3UC1</ComboBoxItem> 
        <ComboBoxItem>Vis3UC2</ComboBoxItem> 
        <ComboBoxItem>Vis3UC3</ComboBoxItem> 
       </ComboBox> 
      </Grid> 
      <Grid> 
       <TextBlock HorizontalAlignment="Left"> Visualization block 2:</TextBlock> 
       <ComboBox Name="vis2Combo" HorizontalAlignment="Right"> 
        <ComboBoxItem>Vis1UC1</ComboBoxItem> 
        <ComboBoxItem>Vis1UC1_2</ComboBoxItem> 
        <ComboBoxItem>Vis1UC2</ComboBoxItem> 
        <ComboBoxItem>Vis1UC2_2</ComboBoxItem> 
        <ComboBoxItem>Vis2UC1</ComboBoxItem> 
        <ComboBoxItem IsSelected="True">Vis2UC2</ComboBoxItem> 
        <ComboBoxItem>Vis2UC3</ComboBoxItem> 
        <ComboBoxItem>Vis3UC1</ComboBoxItem> 
        <ComboBoxItem>Vis3UC2</ComboBoxItem> 
        <ComboBoxItem>Vis3UC3</ComboBoxItem> 
       </ComboBox> 
      </Grid> 
      <Grid> 
       <TextBlock HorizontalAlignment="Left"> Visualization block 3:</TextBlock> 
       <ComboBox Name="vis3Combo" HorizontalAlignment="Right"> 
        <ComboBoxItem>Vis1UC1</ComboBoxItem> 
        <ComboBoxItem>Vis1UC1_2</ComboBoxItem> 
        <ComboBoxItem IsSelected="True">Vis1UC2</ComboBoxItem> 
        <ComboBoxItem>Vis1UC2_2</ComboBoxItem> 
        <ComboBoxItem>Vis2UC1</ComboBoxItem> 
        <ComboBoxItem>Vis2UC2</ComboBoxItem> 
        <ComboBoxItem>Vis2UC3</ComboBoxItem> 
        <ComboBoxItem>Vis3UC1</ComboBoxItem> 
        <ComboBoxItem>Vis3UC2</ComboBoxItem> 
        <ComboBoxItem>Vis3UC3</ComboBoxItem> 
       </ComboBox> 
      </Grid> 
      <Button Name="btnVisApply" HorizontalAlignment="Center" Click="btnVisApply_Click">Apply!</Button> 
     </StackPanel> 
    </Grid> 
    <Grid Name="otherPeerVideo" HorizontalAlignment="Left" VerticalAlignment="Top" Width="1000" Height="700" Margin="0,83,0,0"> 
     <Grid Width="960" Height="540"> 
      <Border BorderBrush="DarkOrange" BorderThickness="4"> 
       <WindowsFormsHost Name="wfServer"/> 
      </Border> 
     </Grid> 
    </Grid> 
    <StackPanel Name="feedbackAndMyVideoStackPanel" Background="WhiteSmoke" Margin="1000,83,0,0" VerticalAlignment="Top" Width="454" Height="800"> 
     <Grid Name="vis1Grid" Height="160"></Grid> 
     <Grid Name="vis2Grid" Height="160"></Grid> 
     <Grid Name="vis3Grid" Height="160"></Grid> 
     <Grid Name="myVideoGrid" VerticalAlignment="Bottom" Height="320"> 
      <Grid HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="454" Height="300"> 
       <Border BorderBrush="Blue" BorderThickness="4"> 
        <WindowsFormsHost Name="myVideo" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="446" Height="292"/> 
       </Border> 
      </Grid> 
      <Button Name="myVideoHideButton" Click="myVideoHideButton_Click" HorizontalAlignment="Right" VerticalAlignment="Top" Width="80" Height="20">Hide/Unhide</Button> 
     </Grid> 
    </StackPanel> 
</Grid> 

這裏是它看起來像在設計師(在兩臺機器上)截圖: Designer look

下面是如何截圖它看起來當應用程序在我的機器上運行,如: enter image description here

在這裏,它的外觀時,應用程序在筆記本電腦上運行就像一個截圖: enter image description here

+2

你顯然有一個佈局的問題。所有的佈局都使用諸如「Margin =」26,24,1165,835「'這樣的東西不夠充分,可能會導致各種不良行爲。我建議您刪除所有邊距並使用正確的[WPF佈局](http://www.codeproject.com/Articles/30904/WPF-Layouts-A-Visual-QuickStart)技術。 –

+1

我同意Highcore。許多(All?)寬度,高度,邊距都是硬編碼的。如果兩臺電腦的窗戶大小不一樣,可能會變壞。嘗試使兩臺電腦上的兩個窗口具有相同的大小,並檢查您是否仍然看到相同的東西 –

+0

謝謝您的有用輸入。我對WPF世界很陌生;你能解釋一下使用邊距和使用Canvas的區別嗎?會使用畫布幫我嗎? – ArthurT

回答

1

我編譯/想你的代碼。 當窗口變小,許多組件消失。 由於利潤率。邊距更換

  • 之前:

隨着餘量用於放置

<Grid Name="callAndSyncGrid" Margin="26,24,1165,835" Background="LightGray"> 
    <!-- ... --> 
</Grid> 
<Grid Name="visSetupGrid" Margin="650,24,515,835" Background="LightGray"> 
     <!-- ... --> 
</Grid> 

「呼吸」的邊距
放置由兩列組成(ColumnDefinitions和Grid.Column =「1」;默認爲0)。
此處每列需要50%(50 *)的可用空間。
你也可以使用的大小 「自動」 或323(硬編碼的大小)

<Grid Name="parentGridForLayout" > 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="50*"/> 
     <ColumnDefinition Width="50*"/> 
    </Grid.ColumnDefinitions> 
    <Grid Name="callAndSyncGrid" Margin="10" Background="LightGray"> 
     <!-- ... --> 
    </Grid> 
    <Grid Name="visSetupGrid" Margin="10" Background="LightGray"> 
      <!-- ... --> 
    </Grid> 
</Grid> 

諮詢

使用網格+保證金或帆布是相當接近的方法。但是通過網格,您可以創建列和行來組織布局。你可以將網格(或其他面板,如StackPanels,DockPanels,WrapPanels)組成網格來組織布局。

免責聲明: 我單獨一段代碼是不夠的。
你需要組織你所有的GUI和許多面板。
我會從頭開始重新創建窗口:空白頁面,然後放置第一個網格(或任何合適的面板),將其與列或行分開。然後把從一個窗口輸入一些內容在電網水平,那麼走在孩子電網多放一些網格/進口含量

問候

+0

感謝您花時間幫助我,非常感謝。 但是,如果我使用Canvas'es或Grid + Margins,我想這是公平的,期望不同的元素不會隨窗口擴展,如果我把它放大,對吧? 我需要使用數據綁定來實現真正的縮放窗口嗎? – ArthurT

+1

不,你不需要DataBinding作爲你的基本情況 - 我只是編輯Grid/{Column Width或Row Height} –

0

由於@HighCore和@Emmanuel都靈指出,問題是,我用邊距定義佈局,如果我在不同的屏幕尺寸上運行應用程序,這會導致很大的麻煩。 爲了解決這個問題,我使用了Canvas'es來放置每個元素相對於它們的容器。 現在我可以在不同的屏幕上使用該應用程序,並且一切都按照我的需要顯示。

這裏更好的layouted UI以供參考:

<Window x:Class="RealSenseiConfFusion.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="860" Width="1500" 
    WindowStartupLocation="Manual" 
    Left="0" Top="0" 
    Closing="Window_Closing" 
    Loaded="Window_Loaded" > 
<Grid Background="LightSteelBlue"> 
    <Canvas> 
     <Canvas Canvas.Top="10" Canvas.Left="10" Width="980" Height="142"> 
      <Rectangle Width="980" Height="132" Name="rectangle1" Stroke="Black" RadiusX="9" RadiusY="9" Fill="LightSlateGray" VerticalAlignment="Top" /> 
      <Grid Name="callAndSyncGrid" Canvas.Top="15" Canvas.Left="15" Width="200" Background="LightGray"> 
       <StackPanel> 
        <TextBlock HorizontalAlignment="Center">ABOUT CALL</TextBlock> 
        <TextBlock Name="myIpTextBlock">Your ip is:</TextBlock> 
        <Grid Name="callGrid"> 
         <TextBox Height="23" HorizontalAlignment="Left" Name="txtIP" Background="GhostWhite" Foreground="Black" Width="221" /> 
         <Button Height="23" HorizontalAlignment="Right" Name="btnCall" Width="75" Click="btnCall_Click">Call</Button> 
        </Grid> 
        <Button Name="btnSync" Content="Start Synchronization" HorizontalAlignment="Left" Width="120" Height="25" Click="btnSynchronize_Click"/> 
        <TextBlock Name="currentConvText"></TextBlock> 
       </StackPanel> 
      </Grid> 
      <Grid Name="visSetupGrid" Canvas.Top="15" Canvas.Right="15" Width="200" Background="LightGray"> 
       <StackPanel> 
        <TextBlock HorizontalAlignment="Center">VISUALIZATION SETUP</TextBlock> 
        <Grid> 
         <TextBlock HorizontalAlignment="Left"> Visualization block 1:</TextBlock> 
         <ComboBox Name="vis1Combo" HorizontalAlignment="Right"> 
          <ComboBoxItem IsSelected="True">Vis1UC1</ComboBoxItem> 
          <ComboBoxItem>Vis1UC1_2</ComboBoxItem> 
          <ComboBoxItem>Vis1UC2</ComboBoxItem> 
          <ComboBoxItem>Vis1UC2_2</ComboBoxItem> 
          <ComboBoxItem>Vis2UC1</ComboBoxItem> 
          <ComboBoxItem>Vis2UC2</ComboBoxItem> 
          <ComboBoxItem>Vis2UC3</ComboBoxItem> 
          <ComboBoxItem>Vis3UC1</ComboBoxItem> 
          <ComboBoxItem>Vis3UC2</ComboBoxItem> 
          <ComboBoxItem>Vis3UC3</ComboBoxItem> 
         </ComboBox> 
        </Grid> 
        <Grid> 
         <TextBlock HorizontalAlignment="Left"> Visualization block 2:</TextBlock> 
         <ComboBox Name="vis2Combo" HorizontalAlignment="Right"> 
          <ComboBoxItem>Vis1UC1</ComboBoxItem> 
          <ComboBoxItem>Vis1UC1_2</ComboBoxItem> 
          <ComboBoxItem>Vis1UC2</ComboBoxItem> 
          <ComboBoxItem>Vis1UC2_2</ComboBoxItem> 
          <ComboBoxItem>Vis2UC1</ComboBoxItem> 
          <ComboBoxItem IsSelected="True">Vis2UC2</ComboBoxItem> 
          <ComboBoxItem>Vis2UC3</ComboBoxItem> 
          <ComboBoxItem>Vis3UC1</ComboBoxItem> 
          <ComboBoxItem>Vis3UC2</ComboBoxItem> 
          <ComboBoxItem>Vis3UC3</ComboBoxItem> 
         </ComboBox> 
        </Grid> 
        <Grid> 
         <TextBlock HorizontalAlignment="Left"> Visualization block 3:</TextBlock> 
         <ComboBox Name="vis3Combo" HorizontalAlignment="Right"> 
          <ComboBoxItem>Vis1UC1</ComboBoxItem> 
          <ComboBoxItem>Vis1UC1_2</ComboBoxItem> 
          <ComboBoxItem IsSelected="True">Vis1UC2</ComboBoxItem> 
          <ComboBoxItem>Vis1UC2_2</ComboBoxItem> 
          <ComboBoxItem>Vis2UC1</ComboBoxItem> 
          <ComboBoxItem>Vis2UC2</ComboBoxItem> 
          <ComboBoxItem>Vis2UC3</ComboBoxItem> 
          <ComboBoxItem>Vis3UC1</ComboBoxItem> 
          <ComboBoxItem>Vis3UC2</ComboBoxItem> 
          <ComboBoxItem>Vis3UC3</ComboBoxItem> 
         </ComboBox> 
        </Grid> 
        <Button Name="btnVisApply" HorizontalAlignment="Center" Click="btnVisApply_Click">Apply!</Button> 
       </StackPanel> 
      </Grid> 
     </Canvas> 

     <Grid Name="otherPeerVideo" HorizontalAlignment="Left" Canvas.Top="150" Canvas.Left="20"> 
      <Grid Width="960" Height="540"> 
       <Border BorderBrush="DarkOrange" BorderThickness="4"> 
        <WindowsFormsHost Name="wfServer"/> 
       </Border> 
      </Grid> 
     </Grid> 

     <StackPanel Name="feedbackAndMyVideoStackPanel" Background="WhiteSmoke" Canvas.Right="15" Canvas.Top="15" VerticalAlignment="Top" Width="454" Height="800"> 
      <Grid Name="vis1Grid" Height="160"></Grid> 
      <Grid Name="vis2Grid" Height="160"></Grid> 
      <Grid Name="vis3Grid" Height="160"></Grid> 
      <Grid Name="myVideoGrid" VerticalAlignment="Bottom" Height="320"> 
       <Grid HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="454" Height="300"> 
        <Border BorderBrush="Blue" BorderThickness="4"> 
         <WindowsFormsHost Name="myVideo" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="446" Height="292"/> 
        </Border> 
       </Grid> 
       <Button Name="myVideoHideButton" Click="myVideoHideButton_Click" HorizontalAlignment="Right" VerticalAlignment="Top" Width="80" Height="20">Hide/Unhide</Button> 
      </Grid> 
     </StackPanel> 
    </Canvas> 
</Grid>