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 rdtdale, Today, 01:02 PM
          1 response
          3 views
          0 likes
          Last Post NinjaTrader_LuisH  
          Started by alifarahani, Today, 09:40 AM
          3 responses
          16 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by RookieTrader, Today, 09:37 AM
          4 responses
          19 views
          0 likes
          Last Post RookieTrader  
          Started by PaulMohn, Today, 12:36 PM
          0 responses
          9 views
          0 likes
          Last Post PaulMohn  
          Started by love2code2trade, 04-17-2024, 01:45 PM
          4 responses
          41 views
          0 likes
          Last Post love2code2trade  
          Working...
          X