Embed xNa into WPF to run

Source: Internet
Author: User

First of all, we need to make it clear that the so-called xNa embedded into WPF is not directly embedded into WPF, but first embedded into winform control, then, embedded in WPF using the windowhost control. Because I have also looked for methods to embed xNa into WPF, and found a lotArticleIt also includes English, but after reading it for more than half a day, we found that the implementation methods were all transitioned through winform. It is also possible that when xNa appears, WPF has not yet appeared, so the support is not good. However, the bottom layer of xNa and WPF is d3d. Therefore, I think xNa can be easily embedded into WPF in the future.

Why can xNa be embedded in winfrom?ProgramThe window presented by xNa itself is a winform window. We can use the followingCodeObtain the window for running the xNa program. Form xnawindow = (form) control. fromhandle (game. window. handle), so we can see that xNa has good support for winfrom. From the code point of view, theoretically, all objects containing the handle can be called the rendering target object of xNa. Take the control object of winform as an example. The Code is as follows:

 This . _ Graphicsdevicemanager. preparingdevicesettings + = New Eventhandler <preparingdevicesettingseventargs> (graphics_preparingdevicesettings );
This . _ Snececontrol. onsizechanged + = New Eventhandler (snececontrol_resize );
Private Void Graphics_preparingdevicesettings ( Object Sender, preparingdevicesettingseventargs E)
{
E. graphicsdeviceinformation. presentationparameters. device?whandle = This . _ Snececontrol. controlhandle;
}
Private Void Snececontrol_resize ( Object Sender, eventargs E)
{
This . _ Graphicsdevicemanager. preferredbackbufferwidth = This . _ Snececontrol. controlwidth;
This . _ Graphicsdevicemanager. preferredbackbufferheight = This . _ Snececontrol. controlheight;
This . _ Graphicsdevicemanager. applychanges ();
}

The above Code contains the code for setting the rendering target. Set the rendering target as the control handle defined by the current user. When the control size changes, some rendering parameters need to change. The above code is the most basic code for loading xNa in winform, but these codes are not enough and there are some auxiliary code.

For example, if you want to put control in the WPF form, but xNa itself has a form, two forms will pop up at the same time during system running. Therefore, we need to add some auxiliary code. When the system starts, set the topmost of the WPF form to true. After the initialization is complete and the game object is obtained, use the game object to obtain the winform that comes with xNa and set the visibility of the form to false.

When using this function, you must first initialize a scene control and add it to the main form. Use the run function of game in the formload event function of the form. After the run function is used, the program will run the function again and enter the loop until the program runs successfully.

During my experiment in WPF, the opening of the shape is not rendered. A slight change to the size of the form will be displayed. Therefore, when initializing a form, I set the form to the minimized state. In the formload function, I call game. set the form to maximize before running, so that the size of the form is changed at startup, so that the rendering result is displayed. The Code is as follows:

Private VoidWindow_loaded (ObjectSender, routedeventargs E)
{
BM. winscene. UI. scenecontrol myscenecontrol =NewWinscene. UI. scenecontrol ();
This. Content = myscenecontrol;

Using(This. _ Game =NewWinscene. BLL. game1 (myscenecontrol ))
{
Myscenecontrol. setapplication (This. _ Game );
This. Windowstate = system. Windows. windowstate. maximized;
This. _ Game. Run ();
}
}

The following figure shows the system running in WPF:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.