Import a new font into WPF application MVVM - wpf

I have file 'resource.xaml' for all the styles. But I need to add a new font file to my project and re-use it instead of default font. How can I add a new font and use it?
I searched the internet and see some methods. but nothing worked for me.
Thanks in Advance.

when using a custom font you must respect the following syntax:
"/FontPath/FontFileName.ttf#FontName"
for exemple :
<Setter Property="FontFamily" Value="/Fonts/VLADIMIR.TTF#Vladimir Script"/>
where the name of this font is "Vladimir Script".

I've had this problem before, and here's how I did it:
Create a folder in your solution and add all of the font .ttf files in there.
In App.xaml, or a resource dictionary, add the following code:
pack://application:,,,/Path/To/Font/#Name Of Font
Then, you can reference the font resource in your XAML code:
<TextBlock FontFamily="{DynamicResource NameOfResource}" Text="Hello World"/>

Related

How to set Startup directory to a Image in xaml

Sorry for bad describe in the title. I have a image in "C:\Users\aUser\Desktop\Program\Image\Image.png"
But my program is in the same folder with the Image. I can set the Directory manually <Image Source="C:\Users\aUser\Desktop\Program\Image.png" But when the parent directory is moved, the code will no longer work. So how can I set the Source of the image that in the child folder without use of the code behind
Image.Source = new BitmapSource(new Uri(AppDomain.CurrentDomain.BaseDirectory + #"\Image\Image.png")
You will need to be more specific what XAML 'version' you are using (e.g. WPF, UWP or Xamarin.Forms). Anyways, here goes:
UWP
You should read through these docs. Your XAML code could look like this:
<Image Source="ms-appx:///Assets/Image.png"/>
Where Assets/Image.png is a path to your image
Xamarin.Forms
Have a look at these docs. The solution depends on the platform
WPF
Have a look at these docs. Your XAML code could look like this:
<Image>
<Image.Source>
<BitmapImage UriSource="/Images/image.png" />
</Image.Source>
</Image>

XAML - Using fonts from resource dictionary from another assembly

I am building an application that uses ResourceDictionaries from another assembilies and I'm having problems with using fonts.
There is an assembly named MyFontAssembly that stores fonts along with references to them as an ResourceDictionary. The structure of it looks as follows:
MyFontAssembly
- FontDictionary.xaml - (stores references to fonts)
- Fonts
- FontA.ttf
- FontB.ttf
...
There is also another assembly that stores ResourceDictionaries for styling controls and it's called MyStylesAssembly. ResourceDictionaries from MyStylesAssembly are then merged in an App.xaml of an application in order to provide reusable styles.
The problem is that my styles does recognise font resources (the code is not crashing because it couldn't find resource by its key), but it looks like fonts stored as ttf files were not applied.
I have tried the following in my FontDictionary.xaml, but none of it works:
<FontFamily x:Key="MyFontKey">Fonts/#MyFontName</FontFamily>
<FontFamily x:Key="MyFontKey">pack://application:,,,/MyFontAssemblyName;Component/Fonts/#MyFontName</FontFamily>
<FontFamily x:Key="MyFontKey">/MyFontAssemblyName;Component/Fonts/#MyFontName</FontFamily>
<FontFamily x:Key="MyFontKey">pack://application:,,,/Fonts/#MyFontName</FontFamily>
NOTE:
I am sure that my fonts form ttf work and are named correctly. I was using <FontFamily x:Key="MyFontKey">Fonts/#MyFontName</FontFamily> implementation in the single exe project with the same structure and everything was just fine, the problem appeared when I have split the implementation into few assemblies, just like I described, during refactoring.
MyFontAssembly is merged in MyStylesAssembly correctly. I just call it by that name here, in real code it has also a few more ResourceDictionaries that are used correctly by MyStylesAssembly. The problem appears to be with just <FontFamily> tags not recognising ttf files.
MyFontAssembly and MyStylesAssembly are projects of tyle ClassLibrary and does not contain any code other than in ResourceDictionaries (no classes or code behind)
Found an answer here. I had to set build action to resource, then reference them by using:
<FontFamily x:Key="MyFontKey">pack://application:,,,/MyFontAssemblyName;Component/Fonts/#MyFontName</FontFamily>
or the shorter version:
<FontFamily x:Key="MyFontKey">/MyFontAssemblyName;Component/Fonts/#MyFontName</FontFamily>
Create A WPF Class Library.Lets Say FontLibraryCommon
Now Delete App.Xaml and MainWIndow.Xaml as shown below
Now change the project properties to class library and compile
Now Add Font Folder and existing TTF files under it as shown. I use Pacifico font for example
Now add ResourceDictioanry let say FontDictioanry.xaml in your library
and your xaml should look like this
Here is the code
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FontLibraryCommon">
<FontFamily x:Key="Pacifico">Fonts/#Pacifico</FontFamily>
</ResourceDictionary>
Now in your other project add Font Library as reference
And in your MainWindow.Xaml `
<Window.Resources>
<ResourceDictionary Source="pack://application:,,,/FontLibraryCommon;component/FontDictioanry.xaml"></ResourceDictionary>
</Window.Resources>`
And last Lets Say label you can set like this
<Label FontFamily="{StaticResource Pacifico}"> Raman</Label>
Here is one way to reference a font from deferent library.
Say we have library called MyAwesomeFonts and here is its project structure:
MyAwesomeFontsLibrary
|
|__ Fonts\
| |
| |__ Fonts.cs
| |__ MyAwesomeFont.ttf
|
|__ Themes\
|
|__ generic.xaml
In the Fonts\ folder you put your fonts, and in the generic.xaml file you declare your font normally like this:
xmlns:fonts="clr-namespace:MyAwesomeFontsLibrary.Fonts"...
<FontFamily
x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type fonts:Fonts}, ResourceId=MyAwesomeFont}">Fonts/#My Awesome Font Name</FontFamily>
and in the Fonts.cs file contains:
// say this class declared in namespace: MyAwesomeLibrary.Fonts
public class Fonts
{
public static ComponentResourceKey MyAwesomeFontKey => new ComponentResourceKey(typeof(Fonts), "MyAwesomeFont");
}
For every font you have you do the same things done above, you add them to Fonts folder, declare them as resource in generic.xaml file and finally create a static property in Fonts.cs
Now build your library, add reference to it in project where you want to use it and add a namespace of your AwesomeFontsLibrary to your XAML something like that:
<... xmlns:fonts="clr-namespace:MyAwesomeFontsLibrary.Fonts;assembly=MyAwesomeFontsLibrary" >
Now here is two snippets of reusing your AwesomeFontsLibrary Fonts in TextBlock and style:
In Style:
<Style x:Key="MyTextBlockStyle"
TargetType="TextBlock">
<Setter Property="FontFamily" Value="{DynamicResource {x:Static fonts:Fonts.MyAwesomeFontKey}}"></Setter>
</Style>
Or directly in Textblock:
<TextBlock FontFamily="{DynamicResource {x:Static fonts:Fonts.MyAwesomeFontKey}}" FontSize="50">Welcome!</TextBlock>
You must use Dynamic Resource, not static resource, when using a ComponentResourceKey
Your new font my not appear in designer window but in runtime it will work.
Steps above are tested on my machine and they work. Hopefully this will help.

how use icon [Font-awesome] in WPF

I am new in WPF. I want to use Font-awesome Icon in textbox and button. but the icon is not bind with my textbox
I install Font-awesome resource to my application.
Let me know the way how can I use it
Thank You,
I really need it please help me..
Example
Step 1 : Download Font-Awesome
Tools -> Library Package Manager -> Package Manager Console
Install
PM > Install-Package FontAwesome.WPF
Step 2 : Add Resource
<Application> xmlns:fa="http://schemas.fontawesome.io/icons/" </Application>
Step 3 : Put App.xaml
<Application.Resources>
<Style x:Key="FontAwesome">
<Setter Property="TextElement.FontFamily" Value="pack://application:,,,/fonts/#FontAwesome" />
</Style>
</Application.Resources>
Step 4 : Use it in Demo.xaml
<TextBlock Style="{StaticResource FontAwesome}"
FontSize="75"
Text="" />
Step 5 :- Output
First, download Font Awesome, extract the ZIP file and copy fonts/fontawesome-webfont.ttf into a Fonts folder in your solution. Set the Build Action in the properties to Resource if it isn’t already
Next, add a Style to the Resources in App.xaml. Don’t forget the # at the front of the font name and remember to use the internal name of the font, not the name of the file. To check the name of the font, just double click on the font file and it will open in the Windows Font Viewer. The font name will be at the top.
<Application.resources>
<FontFamily x:Key="FontAwesome">/Fonts/fontawesome-webfont.ttf#FontAwesome</FontFamily>
</Application.resources>
Open MainWindow.xaml and replace the grid with below snippet:
<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
<StackPanel Orientation="Horizontal" >
<TextBlock Text="I" FontSize="32" Margin="10" VerticalAlignment="Center"></TextBlock>
<TextBlock x:Name="tbFontAwesome" Text="" FontFamily="{StaticResource FontAwesome}" Foreground="Red" FontSize="32" Margin="10" VerticalAlignment="Center"></TextBlock>
<TextBlock Text="Font Awesome" FontSize="32" Margin="10" VerticalAlignment="Center"></TextBlock>
</StackPanel>
</Grid>
Notice "Text" property of "tbFontAwesome" textblock, its the Unicode for Heart.
Cheat Sheet
To extend the accepted answer because it's somewhat out of date and missing information, here's what I did:
Download FontAwesome
Unzip the archive
Inside the unzipped folder, under the use-on-desktop folder, locate the version you want. N.B. Solid has the most icons free; some icons require a Pro payment for Regular and Light versions.
For me, this was Font Awesome 5 Free-Solid-900.otf.
Following the accepted answer and most tutorials, first create a sub-folder in your C# project named Fonts. Paste the fonts file inside this folder.
I renamed this file FontAwesome.otf for brevity
Set the properties of this file:
Build Action: Resource
Copy to Output Directory: Copy if newer/Copy always
In your App.xaml <Application.Resources> or other <ResourceDictionary>, insert:
<FontFamily x:Key="FontAwesome">/YOUR_PROJECT_NAME;component/Fonts/FontAwesome.otf#Font Awesome 5 Free Solid</FontFamily>
Replace YOUR_PROJECT_NAME with your project name. I include this because it is needed should you use MergedDictionaries across projects.
If you did not place the file in a project sub-folder Fonts, rename or omit this part of the path.
Check that the filename matches: replace FontAwesome.otf with the filename (or rename the file itself).
Check the internal font name. You can do this by following the accepted answer. (Open the .otf or .tff file from explorer to start Windows Font Viewer, copy the font name).
Replace the Font Awesome 5 Free Solid with the font name (after the #).
Do not install the font otherwise you cannot verify that you have followed these steps correctly and the font may not work across computers that do not have the font installed.
I know this is an old question, and this option may not have been available to you at the time, so I thought I would provide an updated method for anyone who stumbles across this. I recommend you install the NuGet package 'FontAwesome5' from here:
FontAwesome5 on NuGet.org
or search "fontawesome5" in Visual Studio's built-in NuGet Package Manager window:
Tools > NuGet Package Manager > Manage NuGet Packages for Solution...
-it is usually the top result when using this exact search term.
Then simply add the following namespace to your XAML document:
xmlns:fa5="http://schemas.fontawesome.com/icons/"
and then you should find relative ease in adding icons to your project. For example:
<fa5:ImageAwesome Icon="Solid_ExclamationTriangle" Foreground="#FFFF7400"/>
or
<fa5:FontAwesome Icon="Solid_ExclamationTriangle" Foreground="#FFFF7400"/>
And the beauty of this is that IntelliSense will show you a list of all available icons, saving you the hassle of going to the website to search them up.
You could as well manually select FontAwesome from the FontFamily property of the TextBlock.That will solve the problem.
If FontAwesome is not among the list of fonts then you probably need to import the font file just like the first answer suggested.

using font file in wpf usercontrol

I am using a WPF UserControl inside a Winforms form. I have it working.
I would like the text that I use in the WPF UserControl to use a font that I have as a TTF.
I do not know how to reference this TTF and have the control use it. I am assuming I should load the TTF (this is not an installed font) in the UserControl and tell the control (a label) to use it but I only see reference to the font-family.
Here is how to load a font in wpf from a TTF font file
<TextBlock FontSize="48" FontFamily="/Assets/Fonts/Algeria.TTF#Algeria" FontWeight="Normal">test value</TextBlock>
Use a Resource to store the font...
Make sure the font name is correct ....
from git hub I found the following font
github.com/mozilla/Fira/blob/master/ttf/FiraSans-Medium.ttf. For this font the font would be fira Sans Medium so the Xaml would look like
<Label FontFamily="/Resources/firaSans-Medium.ttf#fira Sans Medium" x:Name="TopText" Content="Memory Disk Registry System Program" FontSize="3" Margin="0,-2,0,0"/>

Images bound to images added to resx files using XAML

My WPF application includes a resource file MyResources.resx, containing several strings and images. Because the application will need to be localized, all my references to globalized resources must be made via named properties of the auto-generated MyResources class. The following code works well for string resources:
<Button Content="{x:Static local:Properties.MyResources.ButtonText}" />
However the same does not work for images. Assuming I have an image eflag.bmp added to the resources as a resource named Flag, I would like to be able to do something like this:
<Image Source="{x:Static local:Properties.MyResources.Flag}" />
Please note that the following alternative approach:
<Image Source="/MyNamespace;component/Resources/eflag.bmp" />
cannot be used in this case because it will not be able to handle localization. The problem can be solved using code behind but I am looking for a XAML based solution.
Turn your x:Static into a Binding.Source and add a Converter which does Bitmap to ImageSource.
Source="{Binding Source={x:Static local:Properties.MyResources.Flag},
Converter={StaticResource BitmapToImageSourceConverter}}"
Alternatively you can make the converter a custom markup extension which takes a Bitmap and returns the ImageSource in ProvideValue.
Source="{me:BitmapToImageSource {x:Static local:Properties.MyResources.Flag}}"

Resources