如何:在WPF中以像素为单位获取网格大小

时间:2018-02-22 14:27:23

标签: c# wpf raspberry-pi3 windowsiot windows-iot-core-10

我在我的MainPage上使用带有3个PivotItems的Pivot。

每个PivotItem都是一个网格。在其中一个我想画一个液压回路。因此,我插入了一些Columns和Rows,以便为每个元素提供一个固定位置。我需要知道网格的大小,因为根据这个大小,线条(代表电路的管道)位于单元格的中间,并且具有单元格的半长度(或高度取决于方向)。

Here you can see how it should look like in the end。当Raspberry 3连接到另一台显示器时,某些线路不在正确的位置。

此外,屏幕未完全填充网格,如can be seen here。在电路下仍然是一些空白区域,但具有相同的设置,这不会发生在其他PivotItems上。

当我想获得一个单元格的大小时(所有单元格都具有相同的大小! *)使用:

double columnWidth = circuitGrid.ColumnDefinitions[0].ActualWidth;

我得到结果0。

当我使用时:

double columnWidth = circuitGrid.ColumnDefinitions[0].Width;

我得到结果NaN。

我使用的行为相同:

var circuitGridSize = new Size(circuitGrid.ActualWidth, circuitGrid.ActualHeight);

我尝试了什么:

  • 使用UI元素属性Height和ActualHeight
  • 使用包含和不包含尺寸类型的属性
  • 将initCircuit函数放入多个已加载事件

有人有解决此问题的方法吗?

非常感谢!

这是MainPage.xaml:

<Page
x:Class="I2cPortExpander.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:I2cPortExpander"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="0">
    <StackPanel>
        <Pivot Margin="0" Height="769" >
            <PivotItem x:Name="AutoMode1" Header="Automatikbetrieb" Margin="0">
                <FlipView>
                    <FlipViewItem>
                        <Grid x:Name="Auto1Grid"  Background="LightGray" Visibility="Visible">
                            <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                                <TextBlock Text="Anzahl der zu prüfenden Kreisläufe:" Style="{ThemeResource BaseTextBlockStyle}"/>
                                <RadioButton x:Name="Option1Cycle" Content="1"  HorizontalAlignment="Center" Checked="Option1Cycle_Checked"/>
                                <RadioButton x:Name="Option2Cycle" Content="2"  HorizontalAlignment="Center" Checked="Option2Cycle_Checked"/>
                                <RadioButton x:Name="Option3Cycle" Content="3"  HorizontalAlignment="Center" Checked="Option3Cycle_Checked"/>
                            </StackPanel>
                        </Grid>
                    </FlipViewItem>
                    <FlipViewItem>
                        <Grid x:Name="Auto2Grid" Background="LightGray" Visibility="Visible">
                            <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                                <TextBlock TextWrapping="Wrap" Text="Festlegen der Zeiten der Prüfschritte in Minuten " Margin="0,0,0,20" FontSize="20"/>
                                <TextBlock x:Name="airTimeText" TextWrapping="Wrap" Text="Druckprüfung mit Luft: 0 Minuten" />
                                <Slider x:Name="airTimeSlider" HorizontalAlignment="Center"  VerticalAlignment="Top" Width="500" Margin="0,0,0,20" TickFrequency="10" Maximum="100" TickPlacement="BottomRight" ValueChanged="airTimeSlider_ValueChanged" />
                                <TextBlock x:Name="cleanTimeText" TextWrapping="Wrap" Text="Spülgang: 0 Minuten"/>
                                <Slider x:Name="cleanTimeSlider" HorizontalAlignment="Center"  VerticalAlignment="Top" Width="500" Margin="0,0,0,20" TickFrequency="10" Maximum="100" TickPlacement="BottomRight" ValueChanged="cleanTimeSlider_ValueChanged"/>
                                <TextBlock x:Name="oilTimeText" TextWrapping="Wrap" Text="Drückprüfung mit Öl: 0 Minuten"/>
                                <Slider x:Name="oilTimeSlider" HorizontalAlignment="Center"  VerticalAlignment="Top" Width="500" Margin="0,0,0,20" TickFrequency="10" Maximum="100" TickPlacement="BottomRight" ValueChanged="oilTimeSlider_ValueChanged"/>
                                <TextBlock x:Name="flowRateTimeText" TextWrapping="Wrap" Text="Durchflussmessung: 0 Minuten"/>
                                <Slider x:Name="flowRateTimeSlider" HorizontalAlignment="Center"  VerticalAlignment="Top" Width="500" Margin="0,0,0,20" TickFrequency="10" Maximum="100" TickPlacement="BottomRight" ValueChanged="flowRateTimeSlider_ValueChanged"/>
                                <TextBlock x:Name="blowTimeText" TextWrapping="Wrap" Text="Ausblasen: 0 Minuten"/>
                                <Slider x:Name="blowTimeSlider" HorizontalAlignment="Center"  VerticalAlignment="Top" Width="500" Margin="0,0,0,20" TickFrequency="10" Maximum="100" TickPlacement="BottomRight" ValueChanged="blowTimeSlider_ValueChanged"/>
                            </StackPanel>
                        </Grid>
                    </FlipViewItem>
                    <FlipViewItem>
                        <Grid x:Name="Auto3Grid" Background="LightGray" Visibility="Visible">
                            <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                                <TextBlock TextWrapping="Wrap" Text="Festlegen der Druckparameter in Bar " Margin="0,0,0,20" FontSize="20"/>
                                <TextBlock x:Name="desiredPressure" TextWrapping="Wrap" Text="Druckprüfung mit: 0 Bar" />
                                <Slider x:Name="desiredPressureSlider" HorizontalAlignment="Center"  VerticalAlignment="Top" Width="500" Margin="0,0,0,20" TickFrequency="2" Maximum="20" TickPlacement="BottomRight" ValueChanged="desiredPressureSlider_ValueChanged" />
                                <TextBlock x:Name="breakPressure" TextWrapping="Wrap" Text="Maximaler Druckverlust während Prüfung: 0 Bar"/>
                                <Slider x:Name="breakPressureSlider" HorizontalAlignment="Center"  VerticalAlignment="Top" Width="500" Margin="0,0,0,20" TickFrequency="1" Maximum="10" TickPlacement="BottomRight" ValueChanged="breakPressureSlider_ValueChanged"/>
                                <Grid>
                                    <Button x:Name="Automatik3_Start" Content="Start" HorizontalAlignment="Right"  VerticalAlignment="Stretch"  Margin="10" Width="75" Click="Automatik3_Start_Click"/>
                                </Grid>
                            </StackPanel>
                        </Grid>
                    </FlipViewItem>
                </FlipView>
            </PivotItem>
            <PivotItem x:Name="HandModus" Header="Handbetrieb" Margin="0">
                <Grid x:Name="circuitGrid" Background="LightGray" Visibility="Visible" Margin="0" >

                    <Grid.RowDefinitions>

                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>

                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>

                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>

                    </Grid.ColumnDefinitions>


                    <Ellipse x:Name="pneuConOuterCirc" Grid.Column="0" Grid.Row="1" Stroke="Black"  HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    <Ellipse x:Name="pneuConInnerCirc" Grid.Column="0" Grid.Row="1" Fill="Black" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    <Button x:Name="HandModeAir" Content="Luft"  HorizontalAlignment="Stretch" VerticalAlignment="Center" Grid.Column="1" Grid.Row="1"  Background="Gray" Click="HandModeAir_Click"/>
                    <Button x:Name="HandModeCycle1" Content="Kreislauf 1" HorizontalAlignment="Stretch" VerticalAlignment="Center" Grid.Column="3" Grid.Row="0" Background="Gray" Click="HandModeCycle1_Click"/>
                    <Button x:Name="HandModeCycle2" Content="Kreislauf 2" HorizontalAlignment="Stretch" VerticalAlignment="Center" Grid.Column="3" Grid.Row="1" Background="Gray" Click="HandModeCycle2_Click"/>
                    <Button x:Name="HandModeCycle3" Content="Kreislauf 3" HorizontalAlignment="Stretch" VerticalAlignment="Center" Grid.Column="3" Grid.Row="2" Background="Gray" Click="HandModeCycle3_Click"/>
                    <Button x:Name="HandModeWaySelector" Content="Pumpe RL" HorizontalAlignment="Stretch"  VerticalAlignment="Top" Canvas.Left="0" Grid.Column="6" Grid.Row="5" Background="Gray" Click="HandModeWaySelector_Click"/>
                    <Button x:Name="HandModePump" Content="Pumpe Aus" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Column="2" Grid.Row="3" Background="Gray" Click="HandModePump_Click" />
                    <Button x:Name="HandModeMeasureSelector" Content="zu" HorizontalAlignment="Stretch"  VerticalAlignment="Top" Grid.Column="4" Grid.Row="7" Background="Gray" Click="HandModeMeasureSelector_Click" />
                    <TextBlock Text="Werkzeug" Grid.Column="5" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" RenderTransformOrigin="0.5,0.5" >
                        <TextBlock.RenderTransform>
                            <CompositeTransform Rotation="90"/>
                        </TextBlock.RenderTransform>
                    </TextBlock>
                    <Rectangle  Stroke="Black" StrokeThickness="4" Grid.Column="5" Grid.Row="0" Grid.RowSpan="3"/>
                    <Rectangle x:Name="Line1" Grid.Column="0" Grid.Row="1" Height="10" Fill="Green" Width="48" HorizontalAlignment="Right"/>
                    <Rectangle x:Name="Line2" Grid.Column="2" Grid.Row="1" Height="10" Fill="Red" HorizontalAlignment="Stretch" />
                    <Rectangle x:Name="Line4" Grid.Column="2" Grid.Row="2" Width="65" Fill="Red" Height="10" HorizontalAlignment="Right"/>
                    <Rectangle x:Name="Line3" Grid.Column="2" Grid.Row="0"  Fill="Red" Height="10" Width="65" HorizontalAlignment="Right"/>
                    <Rectangle x:Name="Line5" Grid.Column="2" Grid.Row="0"  Fill="Red" Width="10" Height="65" VerticalAlignment="Bottom"/>
                    <Rectangle x:Name="Line6" Grid.Column="2" Grid.Row="1" Width="10" Fill="Red" VerticalAlignment="Stretch" />
                    <Rectangle x:Name="Line7" Grid.Column="4" Grid.Row="0" Height="10" Fill="Red" HorizontalAlignment="Stretch"/>
                    <Rectangle x:Name="Line8" Grid.Column="4" Grid.Row="1" Height="10" Fill="Red" HorizontalAlignment="Stretch"/>
                    <Rectangle x:Name="Line9" Grid.Column="4" Grid.Row="2" Height="10" Fill="Red" HorizontalAlignment="Stretch"/>
                    <Rectangle x:Name="Line10" Grid.Column="6" Grid.Row="0"  Fill="Red" Height="10" Width="65" HorizontalAlignment="Left"/>
                    <Rectangle x:Name="Line11" Grid.Column="6" Grid.Row="1"  Fill="Red" Height="10" Width="65" HorizontalAlignment="Left"/>
                    <Rectangle x:Name="Line12" Grid.Column="6" Grid.Row="2"  Fill="Red" Height="10" Width="65" HorizontalAlignment="Left"/>
                    <Rectangle x:Name="Line13" Grid.Column="6" Grid.Row="0"  Fill="Red" Width="10" Height="65" VerticalAlignment="Bottom"/>
                    <Rectangle x:Name="Line14" Grid.Column="6" Grid.Row="1"  Grid.RowSpan="4" Fill="Red" Width="10"/>
                    <Rectangle x:Name="Line15" Fill="RED" Grid.Column="5" Grid.Row="5" Grid.ColumnSpan="1"  Height="10" HorizontalAlignment="Stretch" VerticalAlignment="Top" />
                    <Rectangle x:Name="Line16" Fill="RED" Grid.Column="3" Grid.Row="5"  Height="10" HorizontalAlignment="Stretch" VerticalAlignment="Top"/>
                    <Rectangle x:Name="Line17" Grid.Column="2" Grid.Row="5"  Fill="Red" Height="10" Width="65" VerticalAlignment="Top" HorizontalAlignment="Right" Canvas.ZIndex="1"/>
                    <Rectangle x:Name="Line18" Fill="Green" Grid.Column="6" Grid.Row="5"  Height="10" Width="65" HorizontalAlignment="Left" Canvas.ZIndex="1"/>
                    <Rectangle x:Name="Line19" Fill="Green" Grid.Column="3" Grid.Row="5" Grid.ColumnSpan="3" Height="10" HorizontalAlignment="Stretch" Canvas.ZIndex="1"/>
                    <Rectangle x:Name="Line20" Grid.Column="2" Grid.Row="5"  Fill="Green" Height="10" Width="65" HorizontalAlignment="Right" Canvas.ZIndex="1"/>
                    <Rectangle x:Name="Line21" Grid.Column="2" Grid.Row="5"  Fill="Green" Width="10" VerticalAlignment="Stretch" Canvas.ZIndex="1" HorizontalAlignment="Left" Margin="59,0,0,0"/>
                    <Rectangle x:Name="Line22" Grid.Column="2" Grid.Row="4"  Fill="Green" Width="10" Canvas.ZIndex="1"/>
                    <Rectangle x:Name="Line23" Grid.Column="2" Grid.Row="2"  Fill="Red" Width="10" Canvas.ZIndex="1"/>
                    <Rectangle x:Name="Line24" Grid.Column="6" Grid.Row="5"  Fill="Green" Width="10" Height="50" Canvas.ZIndex="1" HorizontalAlignment="Center"/>
                    <Image Source="images/durchfluss.png" Grid.Column="3" Grid.Row="4" Margin="25,25,25,-10"/>
                    <Image Source="images/druckmesser.png" Grid.Column="5" Grid.Row="4" Margin="25,25,25,-10"/>
                </Grid>
            </PivotItem>
            <PivotItem x:Name="Properties" Header="Einstellungen" HorizontalAlignment="Left" Margin="0">
                <Grid>

                </Grid>
            </PivotItem>
        </Pivot>
    </StackPanel>
</Grid>

这是MainPage代码:

public MainPage()
{

    this.InitializeComponent();

    InitDisplay();

    //this funtion sets up the size of the elements
    initCircuit();

    // Register for the unloaded event so we can clean up upon exit 
    Unloaded += MainPage_Unloaded;

    I2C.InitializeSystem();
}

这是initCircuit功能:

private void initCircuit()
{

    var displayInformation = DisplayInformation.GetForCurrentView();
    var screenSize = new Size(displayInformation.ScreenWidthInRawPixels,
                              displayInformation.ScreenHeightInRawPixels);
    var circuitGridSize = new Size(circuitGrid.ActualWidth,
                                    circuitGrid.ActualHeight);

    double screenWidth = screenSize.Width;
    double screenHeight = screenSize.Height;


    double rowHeight = circuitGridSize.Height / circuitGrid.RowDefinitions.Count;

    double gridWidth = circuitGrid.Width;


    //Initialisierung des Schaltplanes

    //Initialisierung der Verbindungen

    //Pneumatikanschluss
    pneuConInnerCirc.Height = rowHeight / 2.5;
    pneuConOuterCirc.Height = rowHeight / 2;

    //Werkzeuganschluss
    PathGeometry toolSupplyPathVL = new PathGeometry();
    PathGeometry toolSupplyPathRL = new PathGeometry();



    //Werkzeugsymbol


    //Pumpenanschluss
    PathGeometry pumpSupplyPath = new PathGeometry();


    //Messzweig
    PathGeometry measurePath1 = new PathGeometry();
    PathGeometry measurePath2 = new PathGeometry();


    //Initialisierung der Aktoren des Schaltplans


    //Initialisierung der Messinstrumente
    TextBlock actualPressure = new TextBlock();
    TextBlock actualFlowrate = new TextBlock();

    //-----------------------------------------------//

    //Luftanschluss

    Line1.Width = ((screenWidth / circuitGrid.ColumnDefinitions.Count) - pneuConOuterCirc.Width) / 2;
    Line5.Height = (screenHeight / circuitGrid.RowDefinitions.Count + Line3.Height) / 2;
    Line3.Width = (screenWidth / circuitGrid.ColumnDefinitions.Count) / 2;
    Line4.Width = (screenWidth / circuitGrid.ColumnDefinitions.Count) / 2;
    Line10.Width = (screenWidth / circuitGrid.ColumnDefinitions.Count) / 2;
    Line11.Width = (screenWidth / circuitGrid.ColumnDefinitions.Count) / 2;
    Line12.Width = (screenWidth / circuitGrid.ColumnDefinitions.Count) / 2;
    Line18.Width = (screenWidth / circuitGrid.ColumnDefinitions.Count + Line24.Width) / 2;
    Line17.Width = (screenWidth / circuitGrid.ColumnDefinitions.Count) / 2;
    Line20.Width = (screenWidth / circuitGrid.ColumnDefinitions.Count) / 2;
    Line13.Height = (screenHeight / circuitGrid.RowDefinitions.Count + Line3.Height) / 2;

    HandModeWaySelector.Width = (screenWidth / circuitGrid.ColumnDefinitions.Count);
    HandModeWaySelector.Height = (screenHeight / circuitGrid.RowDefinitions.Count) / 3;

    //  Line24.Height= (screenSize.Height / circuitGrid.RowDefinitions.Count)/2 -HandModeWaySelector.Height;
    // Line24.VerticalAlignment = VerticalAlignment.Stretch;
    Line24.Margin = new Thickness((screenWidth / circuitGrid.ColumnDefinitions.Count - Line24.Width) / 2, HandModeWaySelector.Height / 2, (screenWidth / circuitGrid.ColumnDefinitions.Count - Line24.Width) / 2, (screenHeight / circuitGrid.RowDefinitions.Count) / 2);
    Line21.Margin = new Thickness((screenWidth / circuitGrid.ColumnDefinitions.Count - Line24.Width) / 2, 0, (screenWidth / circuitGrid.ColumnDefinitions.Count - Line24.Width) / 2, (screenHeight / circuitGrid.RowDefinitions.Count) / 2 - Line20.Height / 2);
    Line15.Margin = new Thickness(0, (HandModeWaySelector.Height / 2 - Line15.Height) / 2, 0, screenHeight / circuitGrid.RowDefinitions.Count - (HandModeWaySelector.Height - Line15.Height / 2));
    Line16.Margin = Line15.Margin;
    Line17.Margin = new Thickness((screenWidth / circuitGrid.ColumnDefinitions.Count) / 2, (HandModeWaySelector.Height / 2 - Line15.Height) / 2, 0, screenHeight / circuitGrid.RowDefinitions.Count - (HandModeWaySelector.Height - Line15.Height / 2));

}

2 个答案:

答案 0 :(得分:1)

在测量过程之前,你不应该依赖任何措施。执行(并稳定)。尝试在initCircuit MainPage处理程序中移动Loaded调用。你应该获得一些结果。

编辑:更好的方法:

    public MainPage()
    {
        this.InitializeComponent();
        this.circuitGrid.SizeChanged += circuitGrid_SizeChanged;
    }

    private void circuitGrid_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        //check for valid size
        //if (valid) {
        //  initCircuit()
        //}
    }

    private void initCircuit()
    {
        //
    }

这个想法是&#34;听&#34;每个网格大小都会发生变化,然后评估是否绘制。验证是直接检查NaN或数字的东西。我建议在执行具体initCircuit之前执行快速测试作为验证。这是因为在初始渲染过程中您可能会看到SizeChanged次调用的紧密序列。 不是防弹解决方案,但此时您应该看到事情开始起作用。

答案 1 :(得分:0)

根据您提供的代码,行的位置是根据screenSize计算的,circuitGrid是当前应用视图的大小。GetForCurrentView的结果是物理显示分辨率(请注意,它是不是显示分辨率。。我认为位置应该从private void initCircuit() { ... double screenWidth = circuitGridSize.Width; double screenHeight = circuitGridSize.Height; ... } 计算。请参考以下代码:

 if (TextUtils.isEmpty(fName_text.trim())) {
        fName.setError("Field cannot be empty");
    }

    else if (TextUtils.isEmpty(lName_text.trim())) {
        lName.setError("Field cannot be empty");
    }

    else if (TextUtils.isEmpty(username_text.trim())) {
        userName.setError("Field cannot be empty");
    }

   else {                                                                    
         db.insertUserData(fullname, username_text, password_text, email_text);
        Toast.makeText(getApplicationContext(), "Successfully Signed In!" ,Toast.LENGTH_SHORT).show();
    } use it i think you don't code it inside else. happy coding.

另外,Pivot的高度固定为769,会影响位置的适应性。如果它配置为垂直填充StackPanel会更好。