Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
940 views
in Technique[技术] by (71.8m points)

xaml - UWP: how to get element size before painting

My code will draw a graphic and, before the paint event, I need to set the size of element containing the graphic. In part, this comes from a value in an XAML file:

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="35" />
  </Grid.RowDefinitions>
  ...
</Grid>

During the view initialization, I'm hoping to be able to modify the graphic width based on some other factors, but I need the height value, from XAML.

At a breakpoint, I can view the various View values, and at this point ActualHeight and ActualWidth are still 0. I don't see anything else I could use.

Is there another event, coming before paint, that I could use ?

question from:https://stackoverflow.com/questions/65851739/uwp-how-to-get-element-size-before-painting

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The answer is to use SizeChanged event.

In XAML, for example:

<skia:SKXamlCanvas 
  x:Name="EICanvas" 
  SizeChanged="OnSizeChanged" />

And in code-behind:

private void OnSizeChanged (Object sender, SizeChangedEventArgs e)
{
  var newH = e.NewSize.Height;
  var oldH = this.ActualHeight; // in pixels
  ...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...