WPF font resouce not working, possible issue with font name - c#

I am trying to use a font called Suisse Int'l Cond which is located in my project as fonts/SuisseIntlCond-Regular.otf and a build action of Resource
Other examples of OTF font in the project work without issue.
Works:
<Setter Property="FontFamily" Value="fonts/moon.otf #moon"/>
Dosn't Work:
<Setter Property="FontFamily" Value="fonts/SuisseIntlCond-Regular.otf #Suisse Int'l Cond"/>
My guess is it might be an issue with the ' char in the name or something wrong with the font itself but I have been unable to confirm either of these as the issue.

Importing and then exporting the font using Glyphr Studio resolves the issue even with keeping the same name.
How do I change a font's metadata (specifically a title)?

Related

WPF Default DynamicRessource values

Is there a way to save a default value for dynamic ressources in a custom control library?
I have created a custom control library and therefore I use a default Style which resides in the Generic.xaml file. This "default style" uses references to dynamic resource markers (see in the example).
<Style TargetType="{x:Type local:BorderlessWindow}">
<Setter Property="Foreground" Value="{DynamicResource ForegroundColor}" />
<Setter Property="Background" Value="{DynamicResource BackgroundColor}" />
<Setter Property="TitleBackground" Value="{DynamicResource AltBackgroundColor}" />
<Setter Property="Template" Value="{StaticResource DefaultBoderlessWindowTemplate}" />
</Style>
Everything is working as expected if I reference to my custom control library in a new project and add the dynamic resource markers in this new projects app.xaml but the values are empty if I do not do this.
So I want some kind of default values. In other words:
"Take the value of {DynamicResource ForegroundColor} or if this do not exist blue."
I thought I just have to add the default values in the Generic.xaml (MergedDictionary) but this wont do the job. Does anyone have a solution?
The only solution I can think about is to replace the dynamicResource markers with the concret (default) values (e.g. blue, green, black) and handle resources in the "consuming" app if you know what I mean.
After InitializeComponent you can see if the resource you need is found and add a default if not.
public MainWindow()
{
InitializeComponent();
try
{
var resource = FindResource("ForegroundColor");
}
catch (ResourceReferenceKeyNotFoundException)
{
Resources.Add("ForegroundColor", new SolidColorBrush(Colors.Red));
}
}
you are on a complex problem
see that
https://wangmo.wordpress.com/2007/09/27/themesgenericxaml/
Why is my style in Generic.xaml not working?
take care how you define your controldefault style
the place where you put themes\generic.xaml
where did you include your color scheme (normaly in generic)
check assemblyinfo
etc
if all is well structured, it must take your default color in the custom control assembly
or simply just include your generic from assembly in the app.xaml
<ResourceDictionary Source="pack://application:,,,/Client.Core;Component/themes/generic.xaml"/>

WPF - Import custom fonts in C#

I would like to import custom fonts on my WPF application so that they work without having the client to install them.
All the answers I have found so far are in XAML, I would like to do it only in C#.
My fonts are in Resources/Fonts/.
I have already tried this :
Fonts.GetFontFamilies(new Uri("pack://application:,,,/Resources/Fonts/#"));
But it didn't work.
I did everything bluetoothfx said but it still did not work.
Then I changed the Build action of my fonts (it was to Content), to Embedded Resource, and it worked. Resource works also for me.
Thanks anyway.
I think the way that you are working will not work.
At first create a folder name fonts then Add the font to your project, change its Build Action to Content.
Now you need to find the internal name (Real name) of the font not the font-file name. You can have it by opening the font file and you can see it on top.
Now edit App.xaml
<Application.resources>
<style x:key="MYFONT_INTERNAL_NAME">
<setter property="TextElement.FontFamily"
value="pack://application:,,,/fonts/#MYFONT_INTERNAL_NAME" />
</style>
Now use it in your code like:
<TextBlock Style="{StaticResource MYFONT_INTERNAL_NAME}" FontSize="16" Text="Font Style" />
To know more search here:
http://www.alteridem.net/2014/02/24/custom-fonts-in-wpf-applications/

The name ... does not exist in the namespace

This problem is pretty much the same as this one. However my problem is that I get this error but it does compile run and find it find.
My XAML with the error is a resource dictionary (in a different project) and it is trying to use an enum called MainViewMode in a style. I have inherited the code from a previous team so I do not know why it was originally written like this but the xaml consists of all the path data for icons (using the <geometry> tag) as well as all of the Styles. I don't think this is relevant to the problem but just thought I would highlight it anyway.
As I said, when the program runs it does work fine however the designer mode is not working on one of the forms I need to modify and it is making it really annoying.
xmlns:cenum="clr-namespace:ABC;assembly=DEF"
...
<Style x:Key="MainViewToggleButtonStyle" TargetType="ToggleButton">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="CommandParameter" Value="{x:Static cenum:MainViewMode.Overview}" />
...
</style>
I have a resource dictionary, it calls an enum in a different project and says that it can't find it. It does however find it at runtime and work fine.
The main issue is that the designer doesn't load for any forms using this resource dictionary.
I believe your problem is that your namespace is incorrect. Take a look at the top answer in the question you linked to and notice how they have the example namespace. Since you're getting the enum from a different project you will also want to add that project as a reference.
To get the namespace right find the folder that the enum class is in, or if it's not in a folder just the project name. The namespace will be like one of the following,
If it's in a folder try something like this
ProjectName.FolderName;assembly=ProjectName
If not try something like this
ProjectName;assembly=ProjectName

Visual Studio Designer isn't displaying Embedded Font

I'm using some custom fonts in my C# WPF .NET 4.0 application (Open Sans and FontAwesome, specifically) with Visual Studio 2013.
I have:
Added FontAwesome.otf and OpenSans-Regular.ttf to my project (not as a link) under /Fonts.
Made sure both fonts are set to "Resource".
Installed both fonts locally (Windows 8.1).
Created new Styles in my Resource Dictionary (that contains many other pieces, so I'm confident it's working).
Restarted VS2013.
Here is a snippet of the Style I've created in my Resource Dictionary:
<Style x:Key="OpenSans">
<Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Fonts/#Open Sans" />
</Style>
<Style x:Key="FontAwesome">
<Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Fonts/#FontAwesome" />
</Style>
Now, in a new User Control I created purely to test the Designer and these fonts being included properly, I have written the following XAML:
<TextBlock Style="{StaticResource FontAwesome}" FontSize="64" Text="" />
<TextBlock Style="{StaticResource OpenSans}" FontSize="48" Text="the quick brown fox jumped over the lazy dog." />
<TextBlock FontFamily="Open Sans" FontSize="48" Text="the quick brown fox jumped over the lazy dog." />
FontAwesome works in the designer and when run, on both my PC and another PC without FoneAwesome installed.
The OpenSans Style does not display in the designer, but does when executed, including on another PC without Open Sans installed.
The Open Sans FontFamily selection displays in the designer, but does not display on another PC without Open Sans installed (expected).
Problem
I want to be able to use the Designer to see in design-time what the UI looks like given the provided Fonts I'm using. Leveraging the Styles I've created, I'm able to see the FontAwesome icons in the Designer, but not the Open Sans font. The only difference I can tell between the two is that FontAwesome is an Open-Type Font, whereas Open Sans is an True-Type Font.
Does anyone have an idea if I've overlooked something simple, or perhaps if there are obscure issues between OTF and TTF in the VS Designer?
Discoveries & Solution
I cannot discern why Open Sans is not rendering in the VS Designer. Every attempt I've made to coerce it to use Open Sans (from the font resource attached to the project) will fail, and the Designer falls back to the default font.
Using the same methods with FontAwesome works as expected, so there is some element to the font rendering system within VS that I can't explain.
However, I have come up a good (perhaps even better?) solution, and a tip that I didn't know about font selection in WPF:
The FontFamily Property in WPF supports fallback values, e.g.:
<Style x:Key="OpenSans">
<Setter Property="TextElement.FontFamily" Value="Open Sans, /<project name>;component/Fonts/#Open Sans" />
</Style>
Notice that the Value of this Property is firstly "Open Sans", and then following a comma, the URI to the font that's included in the project. (As a reminder, you need to be sure the font resource is of type "Resource" under Build Action Properties (Right-click on the Font file within the Solution Explorer.))
This syntax informs WPF to use the first font listed, and if not available, to then fallback to the second font family, and so forth on down the line.
This is (potentially) a better solution, as it asks the system if the desired font (in this example, Open Sans) is already available. If so, it uses that system font, and if not, it will load the embedded font file resource.
This provides a few benefits:
This setup causes the VS Designer to display fonts using Open Sans (in this example) where I use them, so I can design and see the font in question.
This setup also allows machines with the fonts being used to be loaded from the system first, and if not found, then load from the resource. Perhaps a minor performance-minded item, but still beneficial.
I hope this is beneficial to others who use Open Sans (or other fonts) that for some reason will not show up in the VS Designer unless referenced directly as a system font.

WPF RichTextBox Image Link issue

I am loading a .rtf file into a WPF Rich TextBox and my images that have links are getting this underline property added. I do not want the underline and cant seem to get rid of it.
public MainWindow()
{
InitializeComponent();
Assembly assembly = Assembly.GetExecutingAssembly();
Stream s = assembly.GetManifestResourceStream("WPFRichTextIssue.Sigs.MSC.rtf");
using (s)
{
TextRange TR = new TextRange(RTB.Document.ContentStart, RTB.Document.ContentEnd);
TR.Load(s, DataFormats.Rtf);
}
}
I have tired to find the underlined images with
if (TR.GetPropertyValue(Inline.TextDecorationsProperty) == TextDecorations.Underline)
{
}
else
{
// Do something
}
But the Image is not Inline.Text so it does not find it... Any help would be great.
I also thought maybe i could overwrite the Rich Textbox default functionality that causes the underline of links but all i can seem to find to make that change is to the Inline Text nothing for the images and the images are not affected.
The only other option i can think is if there was a way to change the .rtf file to make sure it did not have any underline attributes but unless I am missing something that does not seem to help either.
Entire VS 2012 project issue sample... Download Project
thanks
Looking in your XAML, just after the RTB opening tag that looks something like this:
<RichTextBox x:Name="RTB_Reply" HorizontalAlignment="Left" Height="157" Margin="302,338,0,0" VerticalAlignment="Top" Width="488" IsReadOnly="True">
Paste this:
<RichTextBox.Resources>
<Style TargetType="{x:Type Hyperlink}">
<Setter Property="TextDecorations" Value="{x:Null}"/>
</Style>
</RichTextBox.Resources>
done deal. =D

Categories