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, 03: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.

    Comment


      #3
      Hello cls71,

      Also consider the information at the following link: http://stackoverflow.com/questions/2...s-dictionaries

      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 jaybedreamin, Today, 05:56 PM
          0 responses
          2 views
          0 likes
          Last Post jaybedreamin  
          Started by DJ888, 04-16-2024, 06:09 PM
          6 responses
          18 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by Jon17, Today, 04:33 PM
          0 responses
          1 view
          0 likes
          Last Post Jon17
          by Jon17
           
          Started by Javierw.ok, Today, 04:12 PM
          0 responses
          6 views
          0 likes
          Last Post Javierw.ok  
          Started by timmbbo, Today, 08:59 AM
          2 responses
          10 views
          0 likes
          Last Post bltdavid  
          Working...
          X