开发者

Rendered timing in navigation (frame) with checkbox

开发者 https://www.devze.com 2023-04-03 08:08 出处:网络
I hard to work for my own transition animation in Frame. But following mysterious problem hit me with deadly deadly damage.

I hard to work for my own transition animation in Frame. But following mysterious problem hit me with deadly deadly damage.

First, here is a sample window. It consists of one frame and one button. Of course it differs to my trunk project, nevertheless, it still showed same problem.

Rendered timing in navigation (frame) with checkbox

When I clicked the button, the frame navigates to Page1.

Rendered timing in navigation (frame) with checkbox

EDITED : here is source state of Page1. It was taken in VS2010 IDE.

Rendered timing in navigation (frame) with checkbox

So, what is problem? - problem is occurring when I call RenderToBitmap() to getting the visual of the destination page. (In this case, destination is the Page1.)

I call RenderToBitmap() with the following code. Here is all of MainWindow's code behind.

public partial class MainWindow : Window
{
    public MainWindow() { InitializeComponent()开发者_StackOverflow; }

    Page1 pg1 = new Page1();
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        this.frame.Navigate(pg1); //start to navigate.
    }

    private void frame_Navigated(object sender, NavigationEventArgs e)
    {
        //SaveVisualToPng is my own static method.
        WPFHelper.SaveVisualToPng("d:\\a.png", pg1);
    }
}

And finally, here is a state of visual when frame_Navigated is called. The TextBox and the TextBlock was rendered, and CheckBox and RadioButton also Rendered. Mysteriously, but, CheckBox and RadioButtons' checked state (notching image and ellipse image) isn't rendered yet.

Rendered timing in navigation (frame) with checkbox

How can I deal this hell forged problem? Of cause this is a very bit things. But I think this part is a basement of my application, so I want to make flawlessly.

EDITED(2) here is a source of SaveVisualToPng(). Originally it contains a some methods to help work, like GetDPI, but i omitted that. Instead following code make a same result.

public static class WPFHelper
{

    public static void SaveVisualToPng(string path, Visual v)
    {
        int width = Convert.ToInt32(((FrameworkElement)v).ActualWidth);
        int height = Convert.ToInt32(((FrameworkElement)v).ActualHeight);

        RenderTargetBitmap myBmp = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
        myBmp.Render(v);

        if (myBmp != null)
        {
            PngBitmapEncoder png = new PngBitmapEncoder();
            png.Frames.Add(BitmapFrame.Create(myBmp));
            using (Stream stm = System.IO.File.Create(path))
            {
                png.Save(stm);
            }
        }
    }

}


See if queuing your work on the dispatcher (to give WPF time to render the controls) helps:

private void frame_Navigated(object sender, NavigationEventArgs e) 
{ 
    Action save = () => WPFHelper.SaveVisualToPng("d:\\a.png", pg1); 
    Dispatcher.BeginInvoke(DispatcherPriority.Background, save);
} 

Without your WPFHelper implementation I can't test it, but it's worth a try.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号