App-wide Font resources in Silverlight - silverlight

I'm looking to see if it is possible to use app-wide font in Silverlight controls that can be set in a application.resources and then used throughout the app. They don't need to specifify the other things about a font like bold or italic, just the name.
Mostly, this is for custom fonts that would require to be embedded into the app. For example, I'm looking for something simple like (not of any particular control or control type)...
<Setter x:Key="My First Font"
Property="FontFamily"
Value="VINERTIC.TTF#Viner Hand ITC" />
Then in any given control I would type...
<TextBlock FontFamily="{StaticResource "My First Font"}"
x:Name="ApplicationTitle"
Text="NEXT PAGE" Foreground="Red"/>
...or...
FontFamily="{Binding "My First Font"}"
or some such thing.
I know what I did doesn't work, but that is the desired effect.
I can't seem to find any documention on how to set app-wide font families. Any advice?

Here is some information I think will get you started.
First off, the easiest way I know to embed a font in a shared library (ex: a Silverlight Class library), is to do the following:
Have a class library, and know its default namespace. For this example, the default namespace of the class library is Common.Windows.
Have a font. For this example, Action Man Bold.ttf, which contains the named font Action Man.
Add the font somewhere in the shared library project. For example, the Assets/Fonts folder.
Set the build action on the file to Resource.
Build the project.
After following these steps, you can refer to the font wherever you please, including styles, with syntax similar to the text block below:
<TextBlock
FontFamily="/Common.Windows;component/Assets/Fonts/Action Man Bold.ttf#Action Man" />
Note that the FontFamily expression above worked within the container project and within a referencing project. I didn't test this process with a zip archive of fonts, but I'd bet a fancy rock out of the garden that it would still work.
That should get you started. If you are interested in going the extra (and useful) step of declaring the font in a style, and using the style on your controls, consider taking a look at this resource:
http://www.silverlight.net/learn/quickstarts/control-styles/

Related

How to combined multiple PATH objects into a vector image that can be used as a style in WPF/XAML

I have a complex logo developed in Illustrator that I want to use as an image in WPF. I can seem to combine the Path's when importing into Blend to get a single Path without loosing all the fill colours.
What is the best way to convert the illustrator image into something that can be used in WPF/XAML.
Usually i just combine the Path's into a single Path and then reference the path using the following:
<Application.Resources>
<Path x:Key="pathKeyName" Data="..."/>
</Application.Resources>
<Path Style="{StaticResource pathKeyName}" />
However there seems to be no easy way to group multiple paths together into a single image that can be used as a resource in a similar manner.
Perhaps there is a way to convert these paths into a Geometry ?
EDIT
In the meantime I have used Blend to create a UserControl from the selected paths and then I place this control inside a Viewbox - not quite as simple but it seems to work.
No sweat, go grab yourself Mike Swanson's AI to XAML exporter plugin for Illustrator (dont pay attention to the versions mentioned on there, it works just fine with the newest version I use it all the time) and export your logo to XAML for WPF.
Then take that XAML and plop it in a ContentControl like I show how on a previous SO answer post.
Then you plop that Style Template into your resource dictionary or wherever and reference it as such with one line wherever/however you need it, voila. :)
Hope this helps, cheers.
OH PS, if there's a lot of shapes/colors etc, you'll never get it into just one Path, sorry amigo.

Apply different styles depending on user theme

I have a WPF application in which I change the default styles a bit, to add some common padding or margin, or something like that. I usually do that either in the Window.Resources or in some resource dictionary I either load in the window’s or application’s resources. An example style could be this:
<Style TargetType="{x:Type Button}">
<Setter Property="Margin" Value="5" />
</Style>
So far so good. Note though that I am changing a default component, not a user component.
Today I noticed that the Aero2 theme that is used in Windows 8 comes with a few different standard styles than I am used to from Windows 7’s Aero theme. Unfortunately I was not really able to make a generic style that would just work in both.
So, I want to be able to specify different styles, depending on which theme the user is using. For example, if the user is using Aero, I want to load Aero-styles.xaml, and if it’s Aero2 on Windows 8, I want to load Aero2-styles.xaml instead.
I was hoping this would be possible by using the Themes directory and providing Aero.NormalColor.xaml and Aero2.NormalColor.xaml. Unfortunately that seems to only work for custom controls, but not the built-in controls. It also does not make the available keys available to be used as a static/dynamic reference.
Is there a way to do this? To provide maybe a base resource dictionary that contains customizations and then another theme-dependent dictionary with minor adjustments to the used theme?
Detect theme on startup (before any UI is loaded), and dynamically add appropriate ResourceDictionary to Application.Resources.MergedDictionaries.
This worked for me.

how to use xaml magic

I have problems figuring out where to use what xaml keywords because its hard to figure out what hierarchy it wants. It seems there is some grand design on how and where to use attributes, properties or child nodes.
<Node Attribute="True">
<Node.Property />
</Node>
I found this beautiful page explaining all the ribbon menu properties, but have no idea how to use them in xaml. After half an hour of searching and trying everything I managed to get an Icon to show in the ribbon menu button.
What is the logic behind this all and how to figure out what to use where?
How to merge the ribbon menu with the application bar (the top bar on most windows applications)? So I get a nice Ribbon Application Menu, like in the example.
Is there a way to turn off xaml background compliation? I'd sacrifice Intellisense for this.
Because the xaml editor performance is abysmal, the are many suggestions for this, but none working so far.
Edit:
I know the xaml syntax, but there's no hint on what hierarchy to use. So if I find the object I want to use (because they are all available) it will only say I'm using the wrong object, it should ask for the kind of object it wants to be in.
Also in normal programming when you use a reference you can always use all classes in it. With xaml we must suddenly know what reference our class came from, also it won't find the reference for you, you either have to try all references to see if they have a certain class or find a code example.
Good questions. Its a little hard to get a feel for exactly what you're asking for in your #1 question, but I'll take a brief stab at that one. I do have an answer for your #2 question. I do not have an answer for your #3.
"1. What is the logic behind this all and how to figure out what to use where?"
Like Clemens mentioned, the XAML Overview does a pretty good job at explaining things.
I'm guessing that one of the main things that you're asking about is basically "when do you use attribute syntax vs property element syntax". From that doc:
For some properties of an object element, attribute syntax is not possible, because the object or information necessary to provide the property value cannot be adequately expressed within the quotation mark and string restrictions of attribute syntax. For these cases, a different syntax known as property element syntax can be used....
Now about this part of your question...
"Also in normal programming when you use a reference you can always use all classes in it. With xaml we must suddenly know what reference our class came from, also it won't find the reference for you, you either have to try all references to see if they have a certain class or find a code example."
If part of your question is more about how can you more-easily handle your XAML (or more appropriately xmlns) namespaces so that it is easier to get references ironed out in your XAML, there is a technique that you may find useful. It lets you consolidate namespaces so that you can use fewer XAML namespace prefixes (or even no namespace prefixes if you take this technique to its extreme).
"2. How to merge the ribbon menu with the application bar (the top bar on most windows applications)? So I get a nice Ribbon Application Menu, like in the example."
Essentially it seems that you're asking how to: (a) extend the window chrome area (the Aero glass area) down into the client part of the window (the part that your application normally gets to put things) and (b) extend the client part of the window up into the window chrome area. If you can do both of these things, then you can end up with something that looks like Microsoft office products or modern web browsers. Fortunately there is the WPF Shell Integration Library which helps you do both of these things. I found this blog and this blog (and the source code they offer) good guides for getting started with using the WPF Shell Integration Library.
Using this library, I was able to make this window (all but the Aero color changing abilities which is a whole other topic). Notice that both of qualities I mentioned are working here (the TabControl is being display up in the normal window chrome top bar area and the window chrome Aero glass is being displayed down in the normal client area):
Here's my take
1) What is the logic behind this all and how to figure out what to use
where?
Whatever you can fit between "" can go inline like:
<TextBlock Text="{Binding Name}" />
Whatever can't, go the element way:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} ({1})">
<Binding Path="Name" />
<Binding Path="Gender" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
2) How to merge the ribbon menu with the application bar (the top bar on
most windows applications)? So I get a nice Ribbon Application Menu,
like in the example.
You'll find more or less complicated mumbo jumbo around google, this is the essence of it:
<Window ...
WindowStyle="None" AllowsTransparency="True" Background="Transparent"
...>
<!-- Fill it up with a PNG image if you want to play with transparency -->
</Window>
Then make the ribbon the top element, and re-create Close/Maximize buttons
3) Is there a way to turn off xaml background compliation? I'd sacrifice
Intellisense for this.
Yep, it's called Notepad++

TextBlock foreground being reset to inherited value after dynamic resource from merged dictionary is applied

EDIT: I was able to reproduce this in a very stripped-down version of the application. Here is a link to the .zip file
http://www.mediafire.com/?cn918gi15uph1xe
I have the module add the view to two different regions - the status bar region along the top is where the issue occurs. The weird part is, when the same view type is added to the main region, there are no problems. The status bar region is an ItemsControl and the main region is a ContentPresenter. That is the only difference.
Please let me know if you have any insight! Thanks.
-----Original Post-----
Hello all,
I am seeing some weird behavior with WPF. I'm using .NET 4 and PRISM v4. The way we have our application structured is that the skin resource dictionaries exist in their own assembly. The modules do not reference this assembly - instead we have a skin manager class that reads from a config file which skin we want and loads the appropriate components into a merged dictionary. The merged dictionary is set in the Application resources (we clear out the Application.Resources.MergedDictionaries before we add it). The idea is that we can later switch skins at runtime if needed, and the modules don't need to know about the skins until runtime.
Then in our xaml we are referencing styles using DynamicResource. The problem has to do with a TextBlock style defined in the skin and referenced by key, such as
<TextBlock Style="{DynamicResource someKey}" ... />
The style defines the font family, font size, and foreground. The font family and size are applied correctly (I verified this). The foreground, however, is always black. I used Snoop and WPF Inspector to see that the foreground value is "inherited" instead of coming from the style.
I also have a control that inherits from TextBlock and all it does is add some properties that determine what the text value should be (it doesn't affect the style at all). I was able to add a property changed override for the Foreground property and found out that the style's foreground value gets applied, and then the inherited value gets applied after that. I wasn't able to get the .NET source debugging to work so I couldn't figure out why/where it was being called from the second time...
Here is a link to an old, old post from a guy with the exact same problem - he did not find the answer, but instead a workaround. Unfortunately the workaround only works on the inherited control (I can't set InheritanceBehavior for TextBlocks).
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/3501ed19-ab40-4064-81b5-e9b7b9d35b56
My guess is that for TextBlock the foreground property is inherited from its parent unless you explicitly set it on your TextBlock instane. For example if you change the Window or UserControl that this Textblock is in to have a Foreground of blue does it work? One thing you can try doing is in the style instead of setting just Foreground to a color, set TextElement.Foreground. See if that works.
It doesn't explain why the problem exists, but a fix was found here:
http://compositewpf.codeplex.com/discussions/257596
The fix is to load the skins before creating/adding views to the shell.

Setting the colour scheme for a Silverlight app from an external resource

I have a Silverlight 3 application containing six custom user controls. I'd like to load the colour scheme for these controls from an external resource.
The code and XAML containing a default colour scheme would be built in the XAP. Then a parameter on the object tag would contain a URL from where alternate colours can be dynamically loaded.
By the way, the Silverlight 3 application theme feature could be used if that's possible but is really overkill. Only colours need to be changed.
Is this possible and how would you recommend to do it?
Here is how I would do it.
In App.xaml I would define the application Resource dictionary like this:-
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ColorTable.xaml" />
</ReourceDictionary>
<!-- rest of your application resource entries here -->
</ResourceDictionary>
</Application.Resources>
Now I would place the ColorTable.xaml outside of the XAP in the same folder that the XAP is sited. This doesn't quite meet all your criteria since an external ColorTable is always required. It is possible to flex this somewhat to achieve the full requirement but it'll be quite messy in comparison.
I would take a look at the technique Corinna Barber uses in these two articles:
http://blogs.msdn.com/corrinab/archive/2009/11/24/9927729.aspx
http://blogs.msdn.com/corrinab/archive/2009/12/02/9931283.aspx
Basically what she does is, at application startup, she creates a bunch of brushes (both solid and gradients) in a binding helper class (hers is called SysColors). Then she simply binds to these brushes, like so: Background="{Binding CalendarGradient, Source={StaticResource SysColors}}"
The main downside to her approach is that you have to write quite a bit of code when creating gradient brushes. And all the different gradient stops would have to be stored independently in your database (or xml or whatever). I'm thinking now that you could probably store your brushes as xaml and just use XamlReader.Load to load the entire brush object at once. That sounds like a better plan to me, but I haven't tried this, I'm just thinking out loud.
In your situation, at application startup, you could easily load your default scheme OR pick up your color values from a WCF service or wherever. You could even implement INotiyPropertyChanged for all the brushes, and thus be able to swap them at runtime. But I guess that might give you bad performance.

Resources