以编程方式将子项添加到堆栈面板不起作用

发布于 2024-12-26 11:30:22 字数 850 浏览 2 评论 0原文

有人能发现我犯的错误吗?

这是代码:

        StackPanel stackPanel = new StackPanel();
        stackPanel.Orientation = Orientation.Vertical;
        for (int index = _elements.Count - 1; index >= 0; index--)
        {
            FrameworkElement element = _elements[index];
            WriteableBitmap tempBitmap = new WriteableBitmap(element, null);
            Image image = new Image();
            image.Source = tempBitmap;
            stackPanel.Children.Add(image);
        }

        stackPanel.UpdateLayout();

        _bitmap = new WriteableBitmap(stackPanel, null);

        _bitmap.Invalidate();

如您所见,我正在创建一个临时图像,然后将其添加到 stackpanel,然后创建最终的 WriteableBitmap。我的 stackpanel 第一个孩子的高度是 154,第二个孩子的高度是 389。在这一行之后:

 _bitmap.Invalidate();

当我看到 PixelHeight 时,它只是 389。我的第一个孩子去了哪里?

Can anybody spot the mistake that I am doing?

Here is the code:

        StackPanel stackPanel = new StackPanel();
        stackPanel.Orientation = Orientation.Vertical;
        for (int index = _elements.Count - 1; index >= 0; index--)
        {
            FrameworkElement element = _elements[index];
            WriteableBitmap tempBitmap = new WriteableBitmap(element, null);
            Image image = new Image();
            image.Source = tempBitmap;
            stackPanel.Children.Add(image);
        }

        stackPanel.UpdateLayout();

        _bitmap = new WriteableBitmap(stackPanel, null);

        _bitmap.Invalidate();

As you can see I am creating a temporary Image and then adding it to stackpanel and then creating a final WriteableBitmap. Myy 1st children of stackpanel is of height 154 and 2nd one is of 389. After this line:

 _bitmap.Invalidate();

when I see PixelHeight it is just 389. Where did my 1st child go?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

む无字情书 2025-01-02 11:30:22

虽然 sLedgem 和 Bathineni 给出的答案都是正确的,但它似乎不适合我的情况。另外为什么我想将它们添加到布局中?如果你方便的话,你可以,但就我而言,我希望它们位于 100% 内存中,因为我的帮助程序类基本上用于打印,没有对屏幕上存在的任何 UIElements 的任何引用。在这种情况下,显然我不想将我的 LayoutRoot 或其他一些面板传递给我的帮助器类只是为了进行此黑客攻击!

要求 Silverlight 运行时渲染内存中的元素。你需要调用测量和安排:

所以通常我所缺少的是:

stackPanel.Measure(new Size(width, height));
stackPanel.Arrange(new Rect(0, 0, width, height));

之前:

_bitmap = new WriteableBitmap(stackPanel, null);

我可以摆脱这一行:

stackPanel.UpdateLayout();

希望这可以帮助那些来到这个线程并且有像我一样的问题的人,因为找到LayoutRoot是不可行的在你的助手类中。

While both of the answers given by sLedgem and bathineni are correct, it doesn't seem to fit my situation. Also why would I want to add them to the layout? If it's convenient for you, you can but in my case I want them to be in 100% memory because my helper class used basically for printing didn't have any reference to any of the UIElements present on the screen. In that case obviously I wouldn't want to pass my LayoutRoot or some other Panel to my helper class just to make this hack!

To ask the Silverlight runtime to render the elements in memory. You need to call Measure and Arrange:

So typically all I was missing was:

stackPanel.Measure(new Size(width, height));
stackPanel.Arrange(new Rect(0, 0, width, height));

before:

_bitmap = new WriteableBitmap(stackPanel, null);

And I can get rid of this line:

stackPanel.UpdateLayout();

Hope this helps someone who comes on this thread and has same problem like me where it is not feasible to find LayoutRoot in your helper class.

耳根太软 2025-01-02 11:30:22

stackPanel 或任何其他面板在添加到 visulatree(任何视觉元素)之前不会呈现。
意味着..如果您向 stackpanel 添加了 100 个项目,那么直到 stackpanel 在屏幕上显示其视觉外观之前,这 100 个元素将不会生成。

如果您不想在屏幕上显示 stackpanel,则添加 stackpanel 并立即将其删除。它将使 stackpanel 渲染它的子元素...

或者

在 XAML 中创建一个具有 1 像素高度和宽度的网格或 stackpanel,并将您的 stackpanel 添加到该网格或 stackpanel ...因此它将在屏幕上不可见,并且它将在渲染在背景上......

stackPanel or any other panels will not render until they added to visulatree (any visual element)..
means.. if you have added 100 items to stackpanel those 100 elements will not be generated until stackpanel has its visual appearence on screen..

if you dont want to show stackpanel on screen then add stackpanel and remove it immediatly. it will make stackpanel render it child elements...

OR

Create a grid or stackpanel in XAML which has 1 pixel height and width and add your stackpanel to that grid or stackpanel ... so it will not be visiable on screen and it will be rendered on background....

梦初启 2025-01-02 11:30:22

Bathineni 是对的,您需要在拍摄快照之前渲染堆栈面板。我建议让它渲染一帧,然后在渲染后抓取位图

     CompositionTarget.Rendering += CompositionTarget_Rendering;
 }

 void CompositionTarget_Rendering(object sender, EventArgs e)
  {
     _bitmap = new WriteableBitmap(stackPanel, null);
     _bitmap.Invalidate();

  }

bathineni is right, you need to render the stackpanel before taking a snapshot. I would suggest letting it render a frame and then grab the bitmap after it has rendered

     CompositionTarget.Rendering += CompositionTarget_Rendering;
 }

 void CompositionTarget_Rendering(object sender, EventArgs e)
  {
     _bitmap = new WriteableBitmap(stackPanel, null);
     _bitmap.Invalidate();

  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文