为什么我的XAML应用程序没有显示

时间:2016-10-12 07:38:37

标签: c# wpf xaml

我正在尝试制作一个wpf应用程序。 问题是,当我运行应用程序时,我只得到一个白色矩形。 我搜索了很多,但我根本看不到错误。

我是一名wpf初学者,所以如果还有其他一些错误,或者可以做得更好的事情,请告诉我。 THANKZ!

这是xaml代码。 (我现在没有编码,只有xaml ......)

    <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Test.Visu"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="Test.Visu.MainWindow"
        Title="MainWindow" Height="600" Width="800" Topmost="True" WindowStyle="None" SnapsToDevicePixels="True" 
        Left="0" Top="0">
   <Window.Resources>
      <ObjectDataProvider x:Key="date" ObjectType="{x:Type sys:DateTime}"/>
   </Window.Resources>
    <Grid x:Name="MainGrid">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="65"/>
            <RowDefinition Height="*" MinHeight="250"/>
            <RowDefinition Height="65"/>
        </Grid.RowDefinitions>
      <!-- TOP Panel-->
        <Grid x:Name="TopPanel" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top"
            Margin="0,0,0,0"
            Height="{Binding RowDefinitions[0].ActualHeight, ElementName=MainGrid}"
            Width="{Binding ActualWidth, ElementName=MainGrid}">
            <Grid.Background>
                <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ControlDarkColorKey}}"/>
            </Grid.Background>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="{Binding RowDefinitions[0].ActualHeight, ElementName=MainGrid}"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="269"/>
                <ColumnDefinition Width="125"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="{Binding RowDefinitions[0].ActualHeight, ElementName=MainGrid}"/>
            </Grid.RowDefinitions>
            <Button x:Name="PageNumberButton" Content="100" Grid.Column="0"
                Background="{Binding Background, ElementName=TopPanel}"
                HorizontalAlignment="Left"
                Height="{Binding Height, ElementName=TopPanel}"
                Width="{Binding ActualHeight, ElementName=TopPanel}" Click="PageNumberButton_Click"/>
            <Label x:Name="PageTitleLabel" Content="PageTitle" Grid.Column="1"
                VerticalContentAlignment="Center" HorizontalContentAlignment="Center"
                HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,0,0,0" FontSize="16" 
                Width="319"
                Height="{Binding Height, ElementName=TopPanel}" 
                BorderBrush ="DarkGray" BorderThickness="1"/>
            <!--<local:TopMenu Grid.Column="2"/>-->
            <StackPanel Grid.Column="2"  Orientation="Horizontal">
                <Button x:Name="BtnProcess" Content="Process" Margin="1,0" VerticalAlignment="Top" 
                    Height="{Binding ActualHeight, ElementName=TopPanel}"
                    Width="{Binding ActualHeight, ElementName=TopPanel}"/>
                <Button x:Name="BtnMenu" Content="Menu" Margin="1,0" VerticalAlignment="Top" 
                    Height="{Binding ActualHeight, ElementName=TopPanel}"
                    Width="{Binding ActualHeight, ElementName=TopPanel}"/>
                <Button x:Name="BtnLogin" Content="Login" Margin="1,0" VerticalAlignment="Top" 
                    Height="{Binding ActualHeight, ElementName=TopPanel}"
                    Width="{Binding ActualHeight, ElementName=TopPanel}"/>
                <Button x:Name="BtnExit" Content="Exit" Margin="1,0" VerticalAlignment="Top" 
                    Height="{Binding ActualHeight, ElementName=TopPanel}"
                    Width="{Binding ActualHeight, ElementName=TopPanel}" Click="BtnExit_Click"/>
            </StackPanel>
            <!-- Date/Time Panel-->
            <Grid x:Name="DateTimePanel" VerticalAlignment="Bottom" HorizontalAlignment="Center" 
                Grid.Column="3"
                Width="125"
                Height="{Binding Height, ElementName=TopPanel}">
                <Grid.Background>
                    <ImageBrush ImageSource="Resources/logo.bmp" Stretch="Uniform"/>
                </Grid.Background>
                <Grid.RowDefinitions>
                    <RowDefinition Height="28"/>
                    <RowDefinition Height="19"/>
                    <RowDefinition Height="19"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Label Grid.Row="0" Content=" "/>
                <Label x:Name="DateLabel" Grid.Row="1" HorizontalContentAlignment="Center" FontSize="11"
                    Content="{Binding Mode=OneWay, Source={StaticResource date}, StringFormat=\{0:MM.dd.yy\}}" />
                <Label x:Name="TimeLabel" Grid.Row="2" HorizontalContentAlignment="Center" FontSize="11"
                    Content="{Binding Mode=OneWay, Source={StaticResource date}, StringFormat=\{0:HH:mm:ss\}}" />
            </Grid>
        </Grid>
        <DockPanel x:Name="MainPanel" Grid.Row="1" Grid.Column="0"
            Height="{Binding RowDefinitions[1].ActualHeight, ElementName=MainGrid}"
            Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}"
            Background="#FFF9DCB6">
            <Label Content="Main Panel" Height="462" VerticalAlignment="Center" HorizontalAlignment="Center" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="32"/>
        </DockPanel>
      <!-- BOTTOM Panel -->
        <StackPanel x:Name="BottomPanel" Grid.Row="2" Grid.Column="0"  HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,0,0,0"
            Height="{Binding RowDefinitions[0].ActualHeight, ElementName=MainGrid}"
            Width="{Binding ActualWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}"   >
            <StackPanel.Background>
                <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.ControlDarkColorKey}}"/>
            </StackPanel.Background>
        </StackPanel>
    </Grid>
</Window>

(在右上方的面板中我想显示日期和时间。这可以通过这种方式自动更新吗?)

以下是一些截图:

Blend正确显示窗口: blend Visual Studio有时也只显示一个白色矩形: studio 运行的应用程序只是一个白色矩形: running

此外,我需要从Windows中删除框架边框,但这已经在我的列表中。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

替换所有Xaml宽度和高度绑定:

Height="{Binding RowDefinitions[0].ActualHeight, ElementName=MainGrid}"
Width="{Binding ActualWidth, ElementName=MainGrid}"

这就是导致混乱的原因,将所有内容设置为&#34; Auto&#34;例如,您将看到您的屏幕,然后进行适当的调整。