// create window object out of XAML string public static CustomWindow LoadWindowFromXaml(string xamlString) { // Get the XAML content from a string. // prepare XML document XmlDocument XAML = new XmlDocument(); // read XAML string XAML.LoadXml(xamlString); // and convert to XML XmlNodeReader XMLReader = new XmlNodeReader(XAML); // generate WPF object tree CustomWindow objWindow = (CustomWindow)XamlReader.Load(XMLReader); // return CustomWindow object return(objWindow); }
public static void Main() { // check if ps2exe.ps1 is present in the application's directory string directoryOfExecutable = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); if (directoryOfExecutable.StartsWith("file:\\") || directoryOfExecutable.StartsWith("http:\\")) { directoryOfExecutable = directoryOfExecutable.Substring(6); } if (!System.IO.File.Exists(directoryOfExecutable + "\\ps2exe.ps1")) { MessageBox.Show("ps2exe.ps1 has to be in the same directory as Win-PS2EXE.exe", "Win-PS2EXE", MessageBoxButton.OK, MessageBoxImage.Error); return; } // XAML string defining the window controls string strXAML = @" <local:CustomWindow xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:local=""clr-namespace:WPFApplication;assembly=***ASSEMBLY***"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" x:Name=""Window"" Title=""Win-PS2EXE"" WindowStartupLocation=""CenterScreen"" Background=""#FFE8E8E8"" Width=""504"" Height=""370"" ShowInTaskbar=""True""> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width=""auto"" /> <ColumnDefinition Width=""auto"" /> <ColumnDefinition Width=""auto"" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height=""auto"" /> <RowDefinition Height=""auto"" /> <RowDefinition Height=""auto"" /> <RowDefinition Height=""auto"" /> <RowDefinition Height=""auto"" /> <RowDefinition Height=""auto"" /> <RowDefinition Height=""auto"" /> <RowDefinition Height=""auto"" /> <RowDefinition Height=""auto"" /> <RowDefinition Height=""auto"" /> <RowDefinition Height=""auto"" /> <RowDefinition Height=""*"" /> </Grid.RowDefinitions> <TextBlock Height=""32"" Margin=""0,10,0,0"" FontSize=""16"" Grid.Row=""0"" Grid.Column=""1"" >Win-PS2EXE: Graphical front end to PS2EXE-GUI</TextBlock> <Label Grid.Row=""1"" Grid.Column=""0"">Source file: </Label> <TextBox x:Name=""SourceFile"" Height=""18"" Width=""362"" Margin=""0,0,10,0"" AllowDrop=""True"" ToolTip=""Path and name of the source file (the only mandatory field)"" Grid.Row=""1"" Grid.Column=""1"" PreviewDragEnter=""TextBox_PreviewDragOver"" PreviewDragOver=""TextBox_PreviewDragOver"" PreviewDrop=""TextBox_PreviewDrop"" /> <Button x:Name=""SourceFilePicker"" Background=""#FFD0D0D0"" Height=""18"" Width=""24"" Content=""..."" ToolTip=""File picker for source file"" Grid.Row=""1"" Grid.Column=""2"" Click=""FilePicker_Click"" /> <Label Grid.Row=""2"" Grid.Column=""0"">Target file: </Label> <TextBox x:Name=""TargetFile"" Height=""18"" Width=""362"" Margin=""0,0,10,0"" AllowDrop=""True"" ToolTip=""Optional: Name and possibly path of the target file"" Grid.Row=""2"" Grid.Column=""1"" PreviewDragEnter=""TextBox_PreviewDragOver"" PreviewDragOver=""TextBox_PreviewDragOver"" PreviewDrop=""TextBox_PreviewDrop"" /> <Label Grid.Row=""3"" Grid.Column=""0"">Icon file: </Label> <TextBox x:Name=""IconFile"" Height=""18"" Width=""362"" Margin=""0,0,10,0"" AllowDrop=""True"" ToolTip=""Optional: Name and possibly path of the icon file"" Grid.Row=""3"" Grid.Column=""1"" PreviewDragEnter=""TextBox_PreviewDragOver"" PreviewDragOver=""TextBox_PreviewDragOver"" PreviewDrop=""TextBox_PreviewDrop"" /> <Button x:Name=""IconFilePicker"" Background=""#FFD0D0D0"" Height=""18"" Width=""24"" Content=""..."" ToolTip=""File picker for icon file"" Grid.Row=""3"" Grid.Column=""2"" Click=""FilePicker_Click"" /> <Label Margin=""0,10,0,0"" Grid.Row=""4"" Grid.Column=""0"">Version:</Label> <WrapPanel Margin=""0,10,0,0"" Grid.Row=""4"" Grid.Column=""1"" > <TextBox x:Name=""FileVersion"" Height=""18"" Width=""72"" Margin=""0,0,10,0"" ToolTip=""Optional: Version number in format n.n.n.n"" /> <Label Margin=""30,0,0,0"" >File description: </Label> <TextBox x:Name=""FileDescription"" Height=""18"" Width=""156"" ToolTip=""Optional: File description displayed in executable's properties"" /> </WrapPanel> <Label Grid.Row=""5"" Grid.Column=""0"">Product name:</Label> <WrapPanel Grid.Row=""5"" Grid.Column=""1"" > <TextBox x:Name=""ProductName"" Height=""18"" Width=""100"" Margin=""0,0,10,0"" ToolTip=""Optional: Product name displayed in executable's properties"" /> <Label Margin=""30,0,0,0"" >Copyright: </Label> <TextBox x:Name=""Copyright"" Height=""18"" Width=""156"" ToolTip=""Optional: Copyright displayed in executable's properties"" /> </WrapPanel> <CheckBox x:Name=""noConsole"" IsChecked=""True"" Margin=""0,10,0,0"" ToolTip=""Generate a Windows application instead of a console application"" Grid.Row=""6"" Grid.Column=""1"">Compile a graphic windows program (parameter -noConsole)</CheckBox> <WrapPanel Grid.Row=""7"" Grid.Column=""1"" > <CheckBox x:Name=""noOutput"" IsChecked=""False"" ToolTip=""Supress any output including verbose and informational output"" >Suppress output (-noOutput)</CheckBox> <CheckBox x:Name=""noError"" IsChecked=""False"" Margin=""6,0,0,0"" ToolTip=""Supress any error message including warning and debug output"" >Suppress error output (-noError)</CheckBox> </WrapPanel> <CheckBox x:Name=""requireAdmin"" IsChecked=""False"" ToolTip=""Request administrative rights (UAC) at runtime if not already present"" Grid.Row=""8"" Grid.Column=""1"">Require administrator rights at runtime (parameter -requireAdmin)</CheckBox> <CheckBox x:Name=""configFile"" IsChecked=""False"" ToolTip=""Enable creation of OUTPUTFILE.exe.config"" Grid.Row=""9"" Grid.Column=""1"">Generate config file (parameter -configFile)</CheckBox> <WrapPanel Grid.Row=""10"" Grid.Column=""1"" > <Label>Thread Apartment State: </Label> <RadioButton x:Name=""STA"" VerticalAlignment=""Center"" IsChecked=""True"" GroupName=""ThreadAppartment"" Content=""STA"" ToolTip=""'Single Thread Apartment' mode (recommended)"" /> <RadioButton x:Name=""MTA"" Margin=""4,0,0,0"" VerticalAlignment=""Center"" IsChecked=""False"" GroupName=""ThreadAppartment"" Content=""MTA"" ToolTip=""'Multi Thread Apartment' mode"" /> <Label Margin=""6,0,0,0"" >Platform: </Label> <ComboBox x:Name=""Platform"" Height=""22"" Margin=""2,0,0,0"" ToolTip=""Designated CPU platform"" > <ComboBoxItem IsSelected=""True"">AnyCPU</ComboBoxItem> <ComboBoxItem>x64</ComboBoxItem> <ComboBoxItem>x86</ComboBoxItem> </ComboBox> </WrapPanel> <WrapPanel Margin=""0,5,0,0"" HorizontalAlignment=""Right"" Grid.Row=""11"" Grid.Column=""1"" > <Button x:Name=""Compile"" Background=""#FFD0D0D0"" Height=""22"" Width=""72"" Margin=""10"" Content=""Compile"" ToolTip=""Compile source file to an executable"" IsDefault=""True"" Click=""Button_Click"" MouseEnter=""Button_MouseEnter"" MouseLeave=""Button_MouseLeave"" /> <Button x:Name=""Cancel"" Background=""#FFD0D0D0"" Height=""22"" Width=""72"" Margin=""10"" Content=""Cancel"" ToolTip=""End program without action"" IsCancel=""True"" Click=""Button_Click"" MouseEnter=""Button_MouseEnter"" MouseLeave=""Button_MouseLeave"" /> </WrapPanel> </Grid> </local:CustomWindow>"; // generate WPF object tree CustomWindow objWindow; try { // assign XAML root object objWindow = CustomWindow.LoadWindowFromXaml(strXAML.Replace("***ASSEMBLY***", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name)); } catch (Exception ex) { // on error in XAML definition XamlReader sometimes generates an exception MessageBox.Show("Error creating the window objects from XAML description\r\n" + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } // and show window objWindow.ShowDialog(); }
// create window object out of XAML string public static CustomWindow LoadWindowFromXaml(string xamlString) { // Get the XAML content from a string. // prepare XML document XmlDocument XAML = new XmlDocument(); // read XAML string XAML.LoadXml(xamlString); // and convert to XML XmlNodeReader XMLReader = new XmlNodeReader(XAML); // generate WPF object tree CustomWindow objWindow = (CustomWindow)XamlReader.Load(XMLReader); // add event handlers // search object of Button Button objButton = (Button)objWindow.FindName("Compile"); // add event handlers objButton.Click += objWindow.Button_Click; objButton.MouseEnter += objWindow.Button_MouseEnter; objButton.MouseLeave += objWindow.Button_MouseLeave; // search object of Button Button objButton2 = (Button)objWindow.FindName("Cancel"); // add event handlers objButton2.Click += objWindow.Button_Click; objButton2.MouseEnter += objWindow.Button_MouseEnter; objButton2.MouseLeave += objWindow.Button_MouseLeave; // search object of TextBox TextBox objTextBox = (TextBox)objWindow.FindName("SourceFile"); // add event handlers objTextBox.PreviewDragEnter += objWindow.TextBox_PreviewDragOver; objTextBox.PreviewDragOver += objWindow.TextBox_PreviewDragOver; objTextBox.PreviewDrop += objWindow.TextBox_PreviewDrop; // search object of Button Button objButton3 = (Button)objWindow.FindName("SourceFilePicker"); // add event handlers objButton3.Click += objWindow.FilePicker_Click; // search object of TextBox TextBox objTextBox2 = (TextBox)objWindow.FindName("TargetFile"); // add event handlers objTextBox2.PreviewDragEnter += objWindow.TextBox_PreviewDragOver; objTextBox2.PreviewDragOver += objWindow.TextBox_PreviewDragOver; objTextBox2.PreviewDrop += objWindow.TextBox_PreviewDrop; // search object of Button Button objButton4 = (Button)objWindow.FindName("TargetFilePicker"); // add event handlers objButton4.Click += objWindow.FilePicker_Click; // search object of TextBox TextBox objTextBox3 = (TextBox)objWindow.FindName("IconFile"); // add event handlers objTextBox3.PreviewDragEnter += objWindow.TextBox_PreviewDragOver; objTextBox3.PreviewDragOver += objWindow.TextBox_PreviewDragOver; objTextBox3.PreviewDrop += objWindow.TextBox_PreviewDrop; // search object of Button Button objButton5 = (Button)objWindow.FindName("IconFilePicker"); // add event handlers objButton5.Click += objWindow.FilePicker_Click; // return CustomWindow object return(objWindow); }