Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The first time I get the visual of a UIElement the size is always 0. #333

Open
groovykool opened this issue Jul 24, 2019 · 7 comments
Open
Assignees

Comments

@groovykool
Copy link

Issue Type

[x ] Bug Report

Current Behavior

The first time I get the visual of a UIElement the size is always 0. If I get the visual a second time in a separate method call then the size is correct. And then it is correct on any following calls. The enclosing method must finish. Getting the visual twice in the same method doesn't work size is 0.
private void Getvisual()
{
string logtext = "";
Visual Vis = ElementCompositionPreview.GetElementVisual(pp);
logtext += $"[Page pp] Visual Size{Vis.Size.ToString()}" + "\n";
tt.Text = logtext;
}

Expected Behavior

I expect the visual size to be correct the first time I get the visual using ElementCompositionPreview.GetElementVisual(xxx)

Steps to Reproduce (for bugs)

Run the above method from an event method. First time size is 0. Second time size is correct. And then always correct?

Your Environment

  • SDK #: 1809
  • Operating System build # : Windows 10 1809
  • Visual Studio Version # : 2019 16.1.6
@daneuber
Copy link
Member

@groovykool Thanks for reaching out. It's possible the elements have not fully loaded yet when you're trying to access the size. In your repro steps you mention calling your GetVisual method from an 'event method.' Have you tried calling your it in a page Loaded event to ensure loading has completed?

@groovykool
Copy link
Author

Yes I tried calling it from page loaded event. Size is 0. If I then call a second time from another event like a button click, It is correct.

@groovykool
Copy link
Author

Here is an example repo.
https://github.com/groovykool/UIElementVisual.git

@JohnnyWestlake
Copy link

JohnnyWestlake commented Aug 1, 2019

@groovykool the point is it will always be 0 immediately after creation. The point of using loaded isn't too immediately get it's size, but too make sure it's ready when you need it. You could also do the same thing using the Loading event or the XAML constructor for the containing class. Essentially "precaching" the visual.

As to why - probably because the compositor exists outside your application, and hence giving it a command and waiting for it to sync details about the visual between your app and the compositor will not be immediate without introducing delay / holding up your XAML thread.

It's also possible you could use an expression animation to do what you need to with the size - what exactly are you trying to do with it?

@groovykool
Copy link
Author

I am using the visual for capturing parts of the screen using GraphicsCaptureItem.CreateFromVisual(Vis);. If the visual size is 0 the screen capture method throws an exception. I can work around problem by getting the visual twice.

@robmikh
Copy link
Member

robmikh commented Aug 7, 2019

It's true that the frame pool will throw an exception if you try to make the buffers a size of 0x0, but you aren't required to make the buffers the size of your capture item. I'd recommend hooking the Loaded event on the control you want to capture instead of the page or to specify a custom size to the frame pool.

Feel free to take a look at https://github.com/robmikh/VisualCaptureDemo.

@groovykool
Copy link
Author

This is still a bug. You can wait forever after the UIElement is loaded, but the size and offset properties of the visual are ALWAYS zero the first time you get them.

Regards,
Tony

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants