C# UI元素在运行时从WPF中消失

C# UI元素在运行时从WPF中消失,c#,wpf,xaml,C#,Wpf,Xaml,我正在开发一个WPF应用程序,允许在视频聊天中连接两个对等方。在过去的两个月里,我一直在XAML设计器中设计UI,一切都很顺利,当我运行应用程序时,它确实显示了一切,直到昨天,完全没有明显的原因。 当代码在另一台笔记本电脑上运行时,UI的某些部分不再显示。我坚持这一事实,它以前确实有效,现在不再有效了。我也没有在代码中更改任何可能与UI的这一部分有关的内容,也没有在xaml中更改任何内容 我也检查过代码中的任何可见性参数,所以我看不到应用程序行为突然改变的任何原因 有人知道为什么会这样吗 以下是

我正在开发一个WPF应用程序,允许在视频聊天中连接两个对等方。在过去的两个月里,我一直在XAML设计器中设计UI,一切都很顺利,当我运行应用程序时,它确实显示了一切,直到昨天,完全没有明显的原因。 当代码在另一台笔记本电脑上运行时,UI的某些部分不再显示。我坚持这一事实,它以前确实有效,现在不再有效了。我也没有在代码中更改任何可能与UI的这一部分有关的内容,也没有在xaml中更改任何内容

我也检查过代码中的任何可见性参数,所以我看不到应用程序行为突然改变的任何原因

有人知道为什么会这样吗

以下是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>

关于电话
您的ip地址是:
呼叫
可视化设置
可视化块1:
粘度1UC1
Vis1UC1_2
粘度1uC2
Vis1UC2_2
Vis2UC1
Vis2UC2
Vis2UC3
Vis3UC1
Vis3UC2
Vis3UC3
可视化块2:
粘度1UC1
Vis1UC1_2
粘度1uC2
Vis1UC2_2
Vis2UC1
Vis2UC2
Vis2UC3
Vis3UC1
Vis3UC2
Vis3UC3
可视化块3:
粘度1UC1
Vis1UC1_2
粘度1uC2
Vis1UC2_2
Vis2UC1
Vis2UC2
Vis2UC3
Vis3UC1
Vis3UC2
Vis3UC3
申请!
隐藏/取消隐藏

以下是designer中的屏幕截图(在两台机器上):

下面是应用程序在我的机器上运行时的屏幕截图:

下面是应用程序在笔记本电脑上运行时的屏幕截图:
我编译/尝试了你的代码。 当窗口变小时,许多组件将消失。 因为利润

保证金替换示例

  • 之前:
留有可供放置的边距

<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(硬编码大小)


建议

使用网格+边距或画布是非常接近的方法。但使用网格,您可以创建列和行来组织布局。您可以将网格(或其他面板,如StackPanels、DockPanels、WrapPanels)组合到网格中以组织布局

免责声明: 仅凭我的一段代码是不够的。
您需要使用许多面板来组织所有GUI。
我会从头开始重新创建窗口:空页面,然后放置第一个网格(或任何合适的面板),将其拆分为列或行。然后将从上一个窗口导入的一些内容放在网格级别,然后进入子网格以放置更多网格/导入的内容


正如@HighCore和@Emmanuel DURIN所指出的,问题在于我定义了带有边距的布局,如果我在不同的屏幕大小上运行应用程序,这可能会导致大麻烦。 为了纠正这个问题,我使用Canvas来放置每个元素相对于它们的容器。 现在,我可以在不同的屏幕上使用该应用程序,所有内容都可以按照我的要求显示

以下是布局更好的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>

关于电话
您的ip地址是:
呼叫
可视化设置
可视化块1:
粘度1UC1
Vis1UC1_2
粘度1uC2
Vis1UC2_2
Vis2UC1
Vis2UC2
Vis2UC3
Vis3UC1
Vis3UC2
Vis3UC3
可视化块2:
粘度1UC1
Vis1UC1_2
粘度1uC2
Vis1UC2_2
Vis2UC1
Vis2UC2
Vis2UC3
Vis3UC1
Vis3UC2
Vis3UC3
<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>