WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Windows Presentation Foundation

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Create a WPF Window from Xaml

 Keywords: Read Load XAML WPF Window Reader Windows Presentation Foundation System.Windows.Markup.XamlReader System.Xml.XmlReader 

;***************************************************************************
;**   Reads XAML input and creates a Window, using the WPF default XAML reader
;**
;** Purpose: Display a Window using the Windows Presentation Foundation
;** Inputs:
;** Outputs: Results in WPF Window
;** Reference:
;**       REQUIRES WinBatch 2013A or newer
;**
;** Developer: Deana Falk 2014.05.07
;***************************************************************************
If Version( )< '2013A'
   Pause('Notice', 'Need 2013A or Newer Version of WinBatch')
   Exit
EndIf

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;  Define non WIL Types
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_True = ObjectType( 'BOOL', 1 )
_False = ObjectType( 'BOOL', 0 )

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Load assemblies into the WinBatch process.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;The System.Windows.Markup.XamlReader documention states that the required assembly to load is PresentationFramework.dll.(http://msdn.microsoft.com/en-us/library/system.windows.markup.xamlreader(v=vs.110).aspx)
ObjectClrOption('appbase','C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF\') ; 'PresentationFramework.dll'
ObjectClrOption("use","PresentationFramework")
ObjectClrOption("use","PresentationCore")

ObjectClrOption("use","System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;  Enumerations
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
enumSizeToContent = ObjectClrNew( 'System.Windows.SizeToContent' )
enumResizeMode = ObjectClrNew( 'System.Windows.ResizeMode' )
enumWindowStartupLocation = ObjectClrNew( 'System.Windows.WindowStartupLocation' )
enumWindowStyle = ObjectClrNew( 'System.Windows.WindowStyle' )

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Create classes implemented by a managed assembly.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

;Read Xaml
oReader = ObjectClrNew('System.Xml.XmlReader')
xaml=`<Window xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><Border BorderThickness="20" BorderBrush="LightBlue" CornerRadius="9" Background='Blue'><StackPanel><Label FontSize="50" FontFamily='Stencil' Background='Blue' Foreground='White' BorderThickness='0'>WinBatch and dotNet Rocks!</Label><Label HorizontalAlignment="Center" FontSize="15" FontFamily='Consolas' Background='Red' Foreground='White' BorderThickness='0'>Visit us a http://www.winbatch.com!</Label></StackPanel></Border></Window>`
cReader = ObjectClrNew('System.IO.StringReader',xaml)
oXmlReader = oReader.Create(cReader)

;Load Xaml
oXamlReader = ObjectClrNew('System.Windows.Markup.XamlReader')
oWin = oXamlReader.Load(oXmlReader)
oWin = ObjectClrType( 'System.Windows.Window', oWin )  ;Associate a Framework based type name with a value
oWin.AllowsTransparency = _True
oWin.SizeToContent = ObjectClrType('System.Windows.SizeToContent',enumSizeToContent.WidthAndHeight)
oWin.ResizeMode = ObjectClrType('System.Windows.ResizeMode', enumResizeMode.NoResize )
oWin.Opacity = .7
oWin.Topmost = _True
oWin.WindowStartupLocation = ObjectClrType('System.Windows.WindowStartupLocation',enumWindowStartupLocation.CenterScreen)
oWin.WindowStyle = ObjectClrType('System.Windows.WindowStyle',enumWindowStyle.None)

;Show message for 5 seconds:
oWin.Show()
TimeDelay(5)
oWin.Close()
Exit

Article ID:   W17859
Filename:   Create a WPF Window from Xaml.txt
File Created: 2014:05:07:12:20:08
Last Updated: 2014:05:07:12:20:08