2
votes

I am trying to link to an external file, as in not in the compiled silverlight app, for my resource dictionary. I'd like to be able to drop 'themes' into a folder on my website and be able to use them without having to recompile and upload every time I create a new one.

I have it changing the resource successfully but I cannot figure out a way to link to a file that is not in the compiled application.

Here is what I have so far:

private void BlueButtonClick(object sender, RoutedEventArgs e)
{
  var s = App.GetResourceStream(new System.Uri("/DynamicDictionary;component/dict2.xaml", System.UriKind.Relative)).Stream;
  var reader = new StreamReader(s);
  var xaml = reader.ReadToEnd();
  var rdd = (ResourceDictionary)XamlReader.Load(xaml);
  Resources.MergedDictionaries.Add(rdd);
  VictoryIsMine();
}

I'd like to be able to change the '/DynamicDictionary;component/dict2.xaml' to something like 'http://localhost:9393/dict2.xaml' but it's giving me an error.

Anyone have any suggestions?

* EDIT **

It is giving me the following error when I try to reference it with localhost:

{System.UriFormatException: A relative URI cannot be created because the 'uriString' parameter represents an absolute URI. at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at System.Uri..ctor(String uriString, UriKind uriKind) at DynamicDictionary.MainPage.RedButtonClick(Object sender, RoutedEventArgs e) at System.Windows.Controls.Primitives.ButtonBase.OnClick() at System.Windows.Controls.Button.OnClick() at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)}

1

1 Answers

3
votes

There is no practical way to source it from outside the file in the direct manner you are looking for.

You have two options that I know of (not saying there aren't others, but this is how I see it typically done):

  1. You can use MEF's DeploymentCatalog or take a WebClient and load it as an assembly, using AssemblyParts to parse it in, then create the resource dictionary reference and manually add it to the resources collection for the application

  2. You can use the WebClient to grab the XAML for the resource. You can then use the XAML Loader to load and process it then add it to the resource collection. This will be far trickier, however, because you have to anticipate the namespaces referenced within the XAML and be sure to configure and pass them to the XAML loader

While it looks "nice" that themes are in XAML, XAML is nothing more than a declarative object graph. Underneath it, it is actual code. So you must load code in order for themes to work, and the only way to add that code to the running deployment is to parse it as a dynamically added assembly, or load the XAML into a process space and add it that way.

PS -- I see you're using a reader, you might try to pass in UriKind.Absolute instead - if that doesn't work, use a WebClient and pass in the text that way.