Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k 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 Reply

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
  ...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...