Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Failed to create 'Type'

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Failed to create 'Type'

    I'm trying to load a windows XAML page in a Ninjascript and I'm getting an error I've never seen before. (attached)

    I need to load a tree control that implements the MVVM design pattern. I've done all the legwork that I usually do - I've implemented several of these dialogs this way and never had a problem until now. Of course, I've never done a WPF TreeView. But now I'm getting this error and I have no idea what I need to do to fix it.

    I'm not sure I'm implementing the model view properly because I haven't gotten far enough but I'm thinking if I can get passed this and any other XAML reading errors, then I'm home free - any problems that remain should be easy.

    Can anyone help me with what I'm not aware I should be doing? I've attached the Visual Studio project which will show what it's supposed to look like and the Ninjascript and XAML files (had to wrap the XAML file in a ZIP) that go in the Strategies directory (Version 8).

    Thanks!!
    Attached Files

    #2
    Hello,

    Thank you for the question.

    I reviewed the files and I have as of yet been unable to get the TreeView specifically working as I have never used a tree view in this way before.

    I did note a couple of items while reviewing this and was able to use the ViewModel with a DataGrid, so potentially that will assist in finding the answer for the treeview now that the code can be confirmed working.

    The first item, the error you are getting. This is a XAML error or XAML Parse error coming from where you are creating the rootObject.

    I noted that you are using:
    Code:
    DataType="{x:Type RegionViewModel}"
    But the type RegionViewModel does not exist in the current context, I tried adding the XMLNS:

    Code:
    xmlns:mvvmGui="clr-namespace:MVVM_GUI"
    
    DataType="{x:Type mvvmGui:RegionViewModel}"
    This created another problem, the XamlReader.Load() does not seem to like when you use a namespace outside of the current context, or it is unable to find the assembly to be able to parse the xaml. Potentially this is the correct solution, or to find the correct xmlns that can be used in XamlReader.Load(). While looking into this I did locate one item which may help with this if you choose to test and see if it is the solution, ParserContext https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
    This appears to allow you to do something like the following example, I did try this but was unable to get it to work without investing a lot of effort.

    Code:
    ParserContext pc = new ParserContext();
    pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
    pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
    pc.XmlnsDictionary.Add("mvvmGui", "clr-namespace:MVVM_GUI;assembly=???");
    StreamReader myMVVM = new StreamReader(string.Format(@"{0}bin\Custom\Strategies\MVVMPage.xaml", NinjaTrader.Core.Globals.UserDataDir));
    rootObject = XamlReader.Load(myMVVM.BaseStream, pc) as FrameworkElement;
    I skipped this and moved on to checking if the ViewModel and DataContext were working and correct so the TreeView could run in theory.

    I did modify the sample for testing to check the ViewModel, for this I used a DataGrid.
    I had also changed the rootObject from DependencyObject to FrameworkElement, this was to allow for a "DataContext" to be applied to the root object for the view model.

    This seems to be working now with the DataGrid. I can see the ViewModel is populated with data, also the Object can be used as the ItemSource. Again I am unsure how to set up the TreeView specifically related to this specific example, it appears this is on the right track though. I hope this information can help in solving the question.

    I look forward to being of further assistance.
    Attached Files
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you Jesse! I appreciate you taking the time and giving me even more to think about. If I ever figure it out, I'll post back here how I got it.

      Thank you, thank you, thank you!!


      Originally posted by NinjaTrader_Jesse View Post
      Hello,

      Thank you for the question.

      I reviewed the files and I have as of yet been unable to get the TreeView specifically working as I have never used a tree view in this way before.

      I did note a couple of items while reviewing this and was able to use the ViewModel with a DataGrid, so potentially that will assist in finding the answer for the treeview now that the code can be confirmed working.

      The first item, the error you are getting. This is a XAML error or XAML Parse error coming from where you are creating the rootObject.

      I noted that you are using:
      Code:
      DataType="{x:Type RegionViewModel}"
      But the type RegionViewModel does not exist in the current context, I tried adding the XMLNS:

      Code:
      xmlns:mvvmGui="clr-namespace:MVVM_GUI"
      
      DataType="{x:Type mvvmGui:RegionViewModel}"
      This created another problem, the XamlReader.Load() does not seem to like when you use a namespace outside of the current context, or it is unable to find the assembly to be able to parse the xaml. Potentially this is the correct solution, or to find the correct xmlns that can be used in XamlReader.Load(). While looking into this I did locate one item which may help with this if you choose to test and see if it is the solution, ParserContext https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
      This appears to allow you to do something like the following example, I did try this but was unable to get it to work without investing a lot of effort.

      Code:
      ParserContext pc = new ParserContext();
      pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
      pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
      pc.XmlnsDictionary.Add("mvvmGui", "clr-namespace:MVVM_GUI;assembly=???");
      StreamReader myMVVM = new StreamReader(string.Format(@"{0}bin\Custom\Strategies\MVVMPage.xaml", NinjaTrader.Core.Globals.UserDataDir));
      rootObject = XamlReader.Load(myMVVM.BaseStream, pc) as FrameworkElement;
      I skipped this and moved on to checking if the ViewModel and DataContext were working and correct so the TreeView could run in theory.

      I did modify the sample for testing to check the ViewModel, for this I used a DataGrid.
      I had also changed the rootObject from DependencyObject to FrameworkElement, this was to allow for a "DataContext" to be applied to the root object for the view model.

      This seems to be working now with the DataGrid. I can see the ViewModel is populated with data, also the Object can be used as the ItemSource. Again I am unsure how to set up the TreeView specifically related to this specific example, it appears this is on the right track though. I hope this information can help in solving the question.

      I look forward to being of further assistance.

      Comment


        #4
        Got it!

        Hey Jesse,

        I got it!! And since I promised I would post the solution, here goes...

        Bottom line: All the stuff you threw out there for me to chase after was vital and necessary. The only thing missing was the magic line:
        (using System.Reflection)
        Code:
        string assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
        So that gives us the assembly name to plug in, along with the namespace in the ParserContext code that you suggested...
        Code:
          
        string assName = Assembly.GetExecutingAssembly().GetName().Name;
        ParserContext pc = new ParserContext();
        pc.XmlnsDictionary.Add("mvvmGui", "clr-namespace:MVVM_GUI_ORIGINAL;assembly="+assName);
        StreamReader myMVVM = new StreamReader(string.Format(@"{0}bin\Custom\Strategies\MVVMPage.xaml", NinjaTrader.Core.Globals.UserDataDir));
        rootObject = XamlReader.Load(myMVVM.BaseStream, pc) as FrameworkElement;
        Also, I needed to swap out "local" with "mvvgGui" in the XAML file.

        Check it out! Thank you so much for all your help Jesse because I don't see any way I would have gotten this without it...
        Attached Files

        Comment


          #5
          Hello,

          Excellent, I thought we were on the right track there with the ParserContext.

          Thank you for following through with that as now we have a good example of a reason to use ParserContext in NinjaScript.

          Also this will be helpful for many users I would think, anyone dealing with any sort of complex data structure could find this useful for any type of control really.

          Please let me know if I may be of further assistance.
          JesseNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by bmartz, 03-12-2024, 06:12 AM
          5 responses
          32 views
          0 likes
          Last Post NinjaTrader_Zachary  
          Started by Aviram Y, Today, 05:29 AM
          4 responses
          13 views
          0 likes
          Last Post Aviram Y  
          Started by algospoke, 04-17-2024, 06:40 PM
          3 responses
          28 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by gentlebenthebear, Today, 01:30 AM
          1 response
          8 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by cls71, Today, 04:45 AM
          1 response
          8 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Working...
          X