1

i tryed to build WPF application using Add-Type, but it failed.


$a = @'
using System.Windows;
public class Program
{
[STAThread()]
public static void Main(string[] args)
{
Application app = new Application();
Window1 w = new Window1();
app.Run(w);
}
}
'@

Add-Type -TypeDefinition $a -UsingNamespace "System.Windows"


Add-Type's help document doesn't have usages of -UsingNamespace (and -ReferencedAssemblies), so i dont well understand...

2 Answers 2

2

Several problems:

$a = @'
 using System;
 using System.Windows;

 public class Program 
 { 
   [STAThread()] 
   public static void Main(string[] args) 
   { 
     Application app = new Application(); 
     Window w = new Window(); 
     app.Run(w); 
   } 
 } 
'@
  • Missing "using System;" reference for STAThread
  • Window w = new Window() - NOT Window1.

Then, compile with:

Add-Type -TypeDefinition $a -ReferencedAssemblies presentationcore, presentationframework, windowsbase
  • Missing WPF assembly refernces

Finally:

ps> [program]::main(@())

This last line will only work if you're using PowerShell ISE,or have started the console version o powershell 2.0 with the -STA flag.

Ultimately this is a academic exercise. You really should be using a mature product like PowerBoots (mentioned by the other poster). It's done all the hard work for you (thanks Jaykul!)

-Oisin

Sign up to request clarification or add additional context in comments.

2 Comments

thanks!!! and im sorry for my carelessness. btw, when should i use '-UsingNameSpace' ?
ps> get-help add-type -parameter usingnamespace This will tell you that that parameter belongs to the parameterset when using MemberDefinition (typically for quick p/invoke imports) rather than your case, TypeDefinition (full c# programs)
0

Personally I would give up this approach and try PowerBoots It's very powerfull PowerShell module with easy syntax and all the basic possibilities of WPF available.

There is much more to do than what you have presented in the script, sorry.

1 Comment

im sorry, but i wanted to know the usage of Add-Type rather than making wpf application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.