none
how to define a class with a XamlReader member?

    Question

  • Normally I have something like
    Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,System.Windows.Forms;
    and then that lets me instantiate XamlReaders without issue.
    But How do I add a XamlReader into a PowerShell class?
    Is this even possible? adding the Add-Type into a constructor does not help.
    PowerShellEditorServices just keeps putting a red squiggy and says that it can't find
    Windows.Markup.XamlReader.
    class AppForm {
        [string]$FormName;
        [string]$FormPath;
        [System.Xml.XmlNodeReader]$xmlNodeReader;
        [Windows.Markup.XamlReader]$xamlReader;
    }
    • Edited by sbogar Thursday, April 20, 2017 4:42 PM
    Thursday, April 20, 2017 4:28 PM

Answers

  • This works in VS Code without any squiggies

    Add-Type -AssemblyName PresentationFramework
    class AppForm {
      $xamlReader = (New-Object Windows.Markup.XamlReader);
    }


    Thank you for the help!
      You are correct this may have been more an issue with PowerShellEditorServices.exe than Powershell per se.
    • Marked as answer by sbogar Thursday, April 20, 2017 7:51 PM
    • Edited by sbogar Thursday, April 20, 2017 7:54 PM
    Thursday, April 20, 2017 7:51 PM

All replies

  • Add-Type -AssemblyName PresentationFramework
    class AppForm {
    	[string]$FormName
    	[string]$FormPath
    	$reader = [Windows.Markup.XamlReader]::new()
    }


    \_(ツ)_/



    Thursday, April 20, 2017 6:39 PM
    Moderator
  • No dice.  VS Code is still throwing the squiggly

    I feel like this should be acceptable as coded below:  

    Add-Type -AssemblyName PresentationFramework class AppForm {    [string]$FormName;    [string]$FormPath;    [System.Xml.XmlNodeReader]$xmlNodeReader; [Windows.Markup.XamlReader]$xamlReader; }

    Is this a limitation of powershell classes?


    • Edited by sbogar Thursday, April 20, 2017 7:41 PM
    Thursday, April 20, 2017 7:34 PM
  • What does VSCode have to do with this.  VSCode is a public domain attempt at an editor and cannot resolve all references.  The class gets built and can be instantiated.


    \_(ツ)_/


    Thursday, April 20, 2017 7:40 PM
    Moderator
  • This works in VS Code without any squiggies

    Add-Type -AssemblyName PresentationFramework
    class AppForm {
      $xamlReader = (New-Object Windows.Markup.XamlReader);
    }


    Thank you for the help!
      You are correct this may have been more an issue with PowerShellEditorServices.exe than Powershell per se.
    • Marked as answer by sbogar Thursday, April 20, 2017 7:51 PM
    • Edited by sbogar Thursday, April 20, 2017 7:54 PM
    Thursday, April 20, 2017 7:51 PM
  • VSCode has some missing pieces at this time.  They may be addressed in later versions.  The code I posted works correctly in PS CLI and ISE.

    Be aware that WPF will not work as expected when wrapped in a class.  You will have many issues that will make the code inaccessible.  Classes are designed to support DSC and not intended to be an emulation of classes in a compiled program language.


    \_(ツ)_/


    Thursday, April 20, 2017 7:56 PM
    Moderator