获得元素的实际大小 [英] Getting Actual Size of Elements
本文介绍了获得元素的实际大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我在我的MainPage上使用了一个包含3个PivotItems的Pivot。
I use on my MainPage a Pivot with 3 PivotItems.
在每个PivotItem中都是一个网格。在其中一个我想画一个液压回路。因此,我插入了一些Columns和Rows,以便为单元格中的每个元素提供一个固定的位置。
In every PivotItem is a grid. In one of them I want to draw a hydraulic circuit. Therefore I inserted some Columns and Rows to give every element a fixed position in a cell.
当我想获得一个单元格的大小(所有单元格具有相同的大小!)时使用:
When I want to get the Size of a Cell (all cells have the same size!) using:
double columnWidth = circuitGrid.ColumnDefinitions[0].ActualWidth;
我得到的结果是0。
当我使用时:
double columnWidth = circuitGrid.ColumnDefinitions[0].Width;
我得到结果NaN。
我使用的相同行为:
var circuitGridSize = new Size(circuitGrid.ActualWidth,
circuitGrid.ActualHeight);
有人有解决此问题的方法吗?
Does someone have a solution for this problem?
谢谢非常感谢!
这是MainPage.xaml:
Here is the 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>
</Page>
这是MainPage代码:
Here is the MainPage code:
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函数:
Here is the initCircuit function:
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));
}
推荐答案
我已经解决了类似的问题。我忘记了细节,研究它需要时间。现在我建议你在Loaded事件中进行大小调整。在构造函数中尚未计算大小。我所做的就是为诊断目的创建大小调整事件
;您可能会看到它多次执行。在适当的时间完成尺寸调整可能是一项挑战。你可能会读到一系列的事情;控件是自上而下放入窗口我相信但是尺寸
是自下而上完成但我忘记了细节。我稍后会尝试回到这里。
I have solved problems like that. I forget details and it will take time to research it. For now I will suggest that you do the sizing in the Loaded event. Sizes are not yet calculated during the constructor. Something I have done is to create Sizing events for diagnostic purposes; you will likely see it executing multiple times. It can be a challenge to get your sizing done at the appropriate time. You might read up on the sequence of things; controls are put into the window top-down I believe but the sizing is done bottom-up but I forget the details. I will try to get back to this later.
这篇关于获得元素的实际大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文