One nice feature of WPF (Windows Presentation Foundation, .Net 3.0) is the integration of 3D library with the existing framework. Today I worked with Viewport3D class. This class handles it all for managing 3D views. The “Hello World” example I coded here basically demonstrates three features:
- Loading XAML dynamically at runtime
- Playing with 3D objects using Viewport3D and make them rotate using mouse.
- Creating transparent and full-screen windows
Let’s drill down to the technical details of above three aspects:
Firstly, loading XAML runtime can be achieved using System.Windows.Markup.XamlReader class. The Load function takes a stream object (containing XAML source) and return appropriate UI control. Actually, it returns the root control in the stream.
FileStream fs = File.OpenRead(fileName);
this.view3D = (Viewport3D) System.Windows.Markup.XamlReader.Load(fs);
Secondly, the class Viewport3D does it all for you to handle 3D objects. Here we have just hooked mouse events that allows rotation of 3D object in all directions. Moreover you can zoom in/out using right mouse button. Thanks Daniel for writing TrackBall class.
Lastly, setting few parameters as below we can make a Window appear transparent and full-screened:
this.win = new Window();
win.Content = view3D;
win.ShowInTaskbar = false ;
win.Background = Brushes.Transparent ;
win.AllowsTransparency = true;
win.WindowStyle = WindowStyle.None ;
win.WindowStartupLocation = WindowStartupLocation.CenterScreen ;
win.WindowState = WindowState.Maximized;
Well, I guess that’s enough for the first WPF post.
Download the source code for sample application: Load-3D-XAML-Runtime.zip
Thanks for reading.