One nice feature of Silverlight is the ability to load resources from the server at runtime. For example, suppose your application needs to display text in a specific font. Ideally, your code would call out to the server (using WebClient) to pull these resources only as they become necessary. The cool thing is that you can zip up any resource you have and then extract it for use on the client side.
1: WebClient webClient = new WebClient();
2: webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
3: webClient.OpenReadAsync(new Uri("myFonts.zip", UriKind.Relative));
1: private void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
2: {
3: if(e.Error == null && !e.Cancelled)
4: {
5: StreamResourceInfo zipResourceInfo = new StreamResourceInfo(e.Result, @"application/zip");
6: StreamResourceInfo fontInfo =
7: Application.GetResourceStream(zipResourceInfo, new Uri("MyFont.TTF", UriKind.Relative));
8: Header.FontSource = new FontSource(fontInfo.Stream);
9: Header.FontFamily = new FontFamily("MyFont");
10: }
11: }
Note: The code above is assuming that myFonts.zip is located in your ClientBin folder.
Also, if your architecture allows, you can also let Silverlight do all of the hard work for you. Simply refer to the zip file and font directly in your XAML:
1: <TextBlock FontFamily="fonts.zip#fontname" Text="Testing" />
Cool huh? Also, be sure to check out Tim Heuer's excellent write-up on the more automatic method. Enjoy!