• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

ResourceDictionaries in xaml external

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

    ResourceDictionaries in xaml external

    Hello,

    I have a doubt: Can I load external resources (for example ResourceDictionaries) from xaml files to use them on my indicators ?

    I created a simple ResourceDictionary and saved it as myresource.xaml inside the Indicators folder. This is the code in myresources.xaml file:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ComponentModel="clr-namespace:System.ComponentModel;assembly=WindowsBa se" >

    <DataTemplate x:Key="mydatatemplate">

    <StackPanel VirtualizingPanel.IsVirtualizing="True">
    <TextBlock Text="{Binding}" ToolTip="{Binding}" FontFamily="{Binding}" />
    </StackPanel>

    </DataTemplate>

    </ResourceDictionary>

    But when I want use it on my indicator, an exception is thrown because the resource was not found. This code fails:

    var foo = new Uri("myresources.xaml", UriKind.Relative);
    Application.Current.Resources.MergedDictionaries.A dd(new ResourceDictionary() { Source = foo });
    DataTemplate dt = Application.Current.FindResource("mydatatemplate") as DataTemplate;
    Print(dt.ToString() );

    However, this works ok on Visual Studio.

    Thanks.
    Last edited by cls71; 08-20-2015, 02:17 AM.

    #2
    Hello cls71,

    Thank you for your post.

    Use Source to define the directory: https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

    If this does not resolve the matter, please attach your file to your response.
    Patrick H.NinjaTrader Customer Service

    Comment


      #3
      Hello cls71,

      Also consider the information at the following link: http://stackoverflow.com/questions/2...s-dictionaries
      Patrick H.NinjaTrader Customer Service

      Comment


        #4
        Thanks Patrick, but I have not been able to make this code work. I think the problem lies in the URI.

        Code:
        Uri u = new Uri("Indicators.myresources.xaml", UriKind.Relative );
        StreamResourceInfo info = Application.GetContentStream(u);
        In a VisualStudio solution, outside NinjaTrader framework, this code works fine, whether if the resource file is loose as if is embedded (GetResourceStream instead).

        In NinjaTrader, the resources files created by users are embedded ? If not, in which folder I must save a resources file.

        I attach the indicator and the resource file. Both are located in the Indicators folder.

        Thanks very much.
        Attached Files

        Comment


          #5
          Hello,

          Thank you for the question.

          In this case the path shown in the example would be invalid based on where the actual xaml file is located.

          Instead you could use the following to load a xaml resource dictionary and merge it.
          Code:
          string path =  Path.Combine(Core.Globals.UserDataDir, "bin", "Custom", "Indicators", "myresources.xaml");
          string xamlString = File.ReadAllText(path);
          ResourceDictionary resourceDictionary = (ResourceDictionary)System.Windows.Markup.XamlReader.Parse(xamlString);
          Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
                     
          DataTemplate dataTemplate = Application.Current.FindResource("mydatatemplate") as DataTemplate;
          if (dataTemplate != null)
          {
          	Print("dt is not null: " + dataTemplate );
          }
          I will attach the source as well, The main difference would be in the way the file is being loaded and parsed and also the path to the file being used.

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

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by marcus2300, Today, 02:25 AM
          0 responses
          1 view
          0 likes
          Last Post marcus2300  
          Started by drpapp, 01-24-2019, 06:57 AM
          5 responses
          67 views
          0 likes
          Last Post pjsmith
          by pjsmith
           
          Started by plydcv, Today, 12:40 AM
          0 responses
          3 views
          0 likes
          Last Post plydcv
          by plydcv
           
          Started by Sh00tHerMcGavin, Yesterday, 07:39 PM
          0 responses
          3 views
          0 likes
          Last Post Sh00tHerMcGavin  
          Started by Mberg, Yesterday, 05:13 PM
          0 responses
          1 view
          0 likes
          Last Post Mberg
          by Mberg
           
          Working...
          X