Add external font failed but for all fonts - c#

So i try to add external font int my application using this post
Steps:
Add a /Fonts folder to your solution.
Add the True Type Fonts (*.ttf) files to that folder
Include the files to the project
Select the fonts and add them to the solution
Set BuildAction: Resource and Copy To Output Directory: Do not copy. Your .csproj file should now should have a section like this one:
<ItemGroup>
<Resource Include="Fonts\NotoSans-Bold.ttf" />
<Resource Include="Fonts\NotoSans-BoldItalic.ttf" />
<Resource Include="Fonts\NotoSans-Italic.ttf" />
<Resource Include="Fonts\NotoSans-Regular.ttf" />
<Resource Include="Fonts\NotoSansSymbols-Regular.ttf" />
</ItemGroup>
In App.xaml add Resources. It should look like in the following code sample. Note that the URI doesn't contain the filename when packing with the application.
<Applicaton ...>
<Application.Resources>
<FontFamily x:Key="NotoSans">pack://application:,,,/Fonts/#Noto Sans</FontFamily>
<FontFamily x:Key="NotoSansSymbols">pack://application:,,,/Fonts/#Noto Sans Symbols</FontFamily>
</Application.Resources>
</Application>
Apply your Fonts like this:
<TextBlock x:Name="myTextBlock" Text="foobar" FontFamily="{StaticResource NotoSans}"
FontSize="10.0" FontStyle="Normal" FontWeight="Regular" />
So i download several fonts but this works only for some of them.
Any ideas?

Related

How to insert a text file as a XAML application resource in UWP Desktop app?

I have several image files defined in the <Application.Resources> section of the App.xaml file. Is it possible to do the same with text files? If yes, how to do it?
<Application.Resources>
<Image x:Key="Img_DefaultCover" Source="/Images/defaultCover.png" />
</Application.Resources>

Building manually with MS Build fails at references [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am learning the WPF structure in C# and XAML. I believe the best way to learn is without the use of any tools other than a compiler and a basic text editor. Unfortunately, because I refuse to use Visual Studio I have had quite the difficulty running my code.
My Question: I have a basic WPF application with the following structure:
+HelloWorld
|-app.xaml
|-app.proj
|-compile.proj
|+main
|-mainWindow.xaml
|+obj
|+Debug
|-app.g.cs
|+main
|-mainwindow.g.cs
The files of interest are mainwindow.xaml, app.xaml, app.proj, and compile.proj.
I first compile the app.xaml and mainwindow.xaml into generated c# code with this:
msbuild compile.proj /tv:4.0
where compile.proj looks like:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask
TaskName="Microsoft.Build.Tasks.Windows.MarkupCompilePass1"
AssemblyFile="C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationBuildTasks.dll" />
<Target Name="MarkupCompilePass1Task">
<MarkupCompilePass1
AssemblyName="HelloWorld"
Language="C#"
OutputType="WinExe"
OutputPath="obj\Debug\"
ApplicationMarkup="App.xaml"
PageMarkup="main\mainwindow.xaml"
References="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.dll;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Xaml.dll;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\PresentationCore.dll;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\PresentationFramework.dll;C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\WindowsBase.dll" />
</Target>
</Project>
after this (which builds successfully) I compile the generated c# code into an executable. with this:
msbuild app.proj /tv:4.0
The code for app.proj is as follows:
<Project DefaultTargets="Compile"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask
TaskName="Microsoft.Build.Tasks.Windows.MarkupCompilePass1"
AssemblyFile="C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\PresentationBuildTasks.dll" />
<PropertyGroup>
<appname>HelloWorld</appname>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<ItemGroup>
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Core.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Xaml.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\WindowsBase.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\PresentationCore.dll" />
<Reference Include="C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\PresentationFramework.dll" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="app.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="G:\Docs\Programming\work\WPF\HelloWorld2\obj\debug\App.g.cs">
<DependentUpon>app.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Page Include="main/mainwindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="G:\Docs\Programming\work\WPF\HelloWorld2\obj\debug\main\mainwindow.g.cs">
<DependentUpon>main\mainwindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<Target Name="Compile">
<CSC
Sources = "#(Compile)"
References="#(Reference)"
OutputAssembly ="$(appname).exe">
<Output
TaskParameter = "OutputAssembly"
ItemName = "EXEFile" />
</CSC>
</Target>
</Project>
This also builds successfully. This then creates helloworld.exe. unfortunately when ran, the executable fails saying the following
Unhandled Exception: System.IO.IOException: Cannot locate resource 'main/mainwindow.xaml'.
After this it lists the function trail which ends in:
at HelloWorld.app.Main()
The only other two written files are app.xaml and mainwindow.xaml
app.xaml:
<Application x:Class="HelloWorld2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="main/mainwindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
mainwindow.xaml:
<Window x:Class="HelloWorld.mainwindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="mainwindow" Height="350" Width="525">
<Grid>
<TextBox Height="23" Width="120" Margin="78,89,319,207" Name="MainTextBox" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left"></TextBox>
</Grid>
</Window>
I have no clue why it can't find the proper xaml file. am I using a improper file structure? Am I missing a compile option on either of the msbuild calls? Thank you for your help.
That MarkupCompilePass1 task is used by MsBuild to perform a preliminary build. Why? Because in order for the XamlCompilePass to be successful, it needs to reference local types in a compiled binary.
Rather than reinvent the wheel, poorly, check out the structure of a WPF project file created by Visual Studio. You'll notice such things like default binary references and default property group values like the following:
<ProjectGuid>{CAFEFCAD-429A-4C61-BD3A-157F042E1FE7}</ProjectGuid>
<OutputType>WinExe</OutputType>
This should get you started on your way to building your WPF projects with MsBuild.
Regarding your issues with Visual Studio and MsBuild, did you know that MsBuild now ships with Visual Studio rather than the .NET Framework?

Accessing resource in WPF

I know this is simple but I am very new in XAML.
I have two folders in my solutions
Resources\Images\font.png
Shapes\dictonary1.xaml
the soultion also has app.xaml and mainwindow.xaml
<Label ToolTip="Label">
<Label.Background>
<ImageBrush ImageSource="\Resources\Images\Font.png" />
</Label.Background>
</Label>
In my dictonary1.xaml file I add a label containing a background brush
But throws me an error
This work fine in wen I do the same in MainWindow.xaml file.
The font.png file properties
Build action is Resource and
Copy to output directory is always Copy always
I hope I am clear
Did you merge dictionary1.xaml into the mainwindow.xaml? If the image is used in dictionary1.xaml then try this:
<ImageBrush ImageSource="../Resources/Images/Font.png" />

WPF Ribbon Changing Theme

Hi I am creating my first WPF application that uses Ribbons. The problem I am having is I cannot change the theme of the ribbons. I want to use the Office 2007 Black theme. I have looked everywhere and everywhere I find roughly the same example, I have tried writing it from scratch, copy pasting and everything but it keeps throwing an exception when I try t run the application.
Exception
Set property 'System.Windows.ResourceDictionary.Source' threw an exception.' Line number '11' and line position '18'.
Inner Exception
{"Cannot locate resource 'themes/office2007black.xaml'."}
Xaml Code
<r:RibbonWindow x:Class="DtcInvoicer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:r="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
Title="Dtc Invoicer"
Width="1000" Height="650" WindowStartupLocation="CenterScreen">
<r:RibbonWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/RibbonControlsLibrary;component/Themes/Office2007Black.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</r:RibbonWindow.Resources>
<Grid x:Name="LayoutRoot">
<r:Ribbon x:Name="TopMenuRibbon" Title="First Ribbon">
<r:RibbonTab Header="Home" />
<r:RibbonTab Header="Company" />
<r:RibbonTab Header="Employees" />
<r:RibbonTab Header="Receivables" />
<r:RibbonTab Header="Payables" />
<r:RibbonTab Header="Inventory" />
<r:RibbonTab Header="Settings" />
</r:Ribbon>
</Grid>
</r:RibbonWindow>
Anyone have idea why that might be happening? The RibbonControlsLibrary is in my references, everything works fine and the ribbons come up in the blue theme until I try to switch to black or silver, then I get the exception.
Resources that are specific to a theme are kept in a resource dictionary with a specific file name. These files must be in a folder named Themes that is a subfolder of the folder that contains the control.
See this MSDN article for more details.

How to include external font in WPF application without installing it

How to include external font in WPF application without installing it
I tried this code
System.Drawing.Text.PrivateFontCollection privateFonts = new System.Drawing.Text.PrivateFontCollection();
privateFonts.AddFontFile("C:\\Documents and Settings\\somefont.ttf");
System.Drawing.Font font = new Font(privateFonts.Families[0], 12);
this.label1.Font = font;
It working correctly in Windows Form Application but not in WPF.
There are two ways of doing this: One way is to package the fonts inside the application. The other way is to have the fonts in a folder. The difference is mostly the URI you need to load the files with.
Package with Application
Add a /Fonts folder to your solution.
Add the True Type Fonts (*.ttf) files to that folder
Include the files to the project
Select the fonts and add them to the solution
Set BuildAction: Resource and Copy To Output Directory: Do not copy. Your .csproj file should now have a section like this one:
<ItemGroup>
<Resource Include="Fonts\NotoSans-Bold.ttf" />
<Resource Include="Fonts\NotoSans-BoldItalic.ttf" />
<Resource Include="Fonts\NotoSans-Italic.ttf" />
<Resource Include="Fonts\NotoSans-Regular.ttf" />
<Resource Include="Fonts\NotoSansSymbols-Regular.ttf" />
</ItemGroup>
In App.xaml add <FontFamily> resources. It should look like in the following code sample. Note that the URI doesn't contain the filename when packing with the application.
<Applicaton ...>
<Application.Resources>
<FontFamily x:Key="NotoSans">pack://application:,,,/Fonts/#Noto Sans</FontFamily>
<FontFamily x:Key="NotoSansSymbols">pack://application:,,,/Fonts/#Noto Sans Symbols</FontFamily>
</Application.Resources>
</Application>
Apply your fonts like this:
<TextBlock x:Name="myTextBlock" Text="foobar" FontFamily="{StaticResource NotoSans}"
FontSize="10.0" FontStyle="Normal" FontWeight="Regular" />
You can also set the font imperatively:
myTextBlock.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "./Fonts/#Noto Sans");
Copy to Output Directory
Add a /Fonts folder to your solution.
Add the True Type Fonts (*.ttf) files to that order
Include the files to the project
Select the fonts and add them to the solution
Set BuildAction: Content and Copy To Output Directory: Copy if newer or Copy always. Your .csproj file should now have a section like this one:
<ItemGroup>
<Content Include="Fonts\NotoSans-Bold.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Fonts\NotoSans-BoldItalic.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Fonts\NotoSans-Italic.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Fonts\NotoSans-Regular.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Fonts\NotoSansSymbols-Regular.ttf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
In App.xaml add <FontFamily> resources. It should look like in the following code sample.
<Applicaton ...>
<Application.Resources>
<FontFamily x:Key="NotoSansRegular">./Fonts/NotoSans-Regular.ttf#Noto Sans</FontFamily>
<FontFamily x:Key="NotoSansItalic">./Fonts/NotoSans-Italic.ttf#Noto Sans</FontFamily>
<FontFamily x:Key="NotoSansBold">./Fonts/NotoSans-Bold.ttf#Noto Sans</FontFamily>
<FontFamily x:Key="NotoSansBoldItalic">./Fonts/NotoSans-BoldItalic.ttf#Noto Sans</FontFamily>
<FontFamily x:Key="NotoSansSymbols">./Fonts/NotoSans-Regular.ttf#Noto Sans Symbols</FontFamily>
</Application.Resources>
</Application>
Apply your fonts like this:
<TextBlock Text="foobar" FontFamily="{StaticResource NotoSansRegular}"
FontSize="10.0" FontStyle="Normal" FontWeight="Regular" />
References
MSDN: Packaging Fonts with Applications
I use such XAML code:
<Style x:Key="Hatten">
<Setter Property="TextElement.FontFamily" Value="Resources/#HATTEN" />
</Style>
#HATTEN - reference to hatten.tft in Resources.
Using the Style:
<TextBlock x:Name="lblTitle" Style="{DynamicResource Hatten}" FontSize="72"></TextBlock>
The best answer to this question I found here
http://geekswithblogs.net/Martinez/archive/2010/01/29/custom-font-in-wpf-application.aspx
SOLUTION
It involves using even more wicked string than before but works as expected:
<Label FontFamily="pack://application:,,,/Folder1/#Katana Sans">Text</Label>
Where is Folder1 is a folder of your project where you keep a TTF file.
Three notes at the end:
‘Katana Sans’ is the name of the font, not the name of the file. This is significant difference. To get the name of the font simply click the file twice. Note that spaces are included without any changes in this string.
Remember to put the hash sign ‘#’ in front of font name. It will not work otherwise.
Custom font may also be added to the project with ‘Build Action’ set to ‘Content’. This is not recommended approach however and for the sake of simplicity I ignored this possibility.
Some extra links
https://msdn.microsoft.com/en-us/library/ms753303(v=vs.100).aspx
https://msdn.microsoft.com/en-us/library/cc296385.aspx
The easiest way to include external fonts is to
Step 1. Create the folder Fonts And add your fonts into it.
Step 2. Set Build action to content and Copy to Output Directory to Copy always.
Step 3. Build the Solution to update it with the Font directory.
Step 4. Use the font in your elements using FontFamily="Fonts/#font name"
All Done!
Thanks to cscience !
I did not found any answer for exactly that.
But I found a solution that I did not saw on Internet.
I followed a recommendation of doing a folder and marking all the files inside as Resources.
But I needed to enumerate them, and that was my main problem, because I need to load all to my screen without recording the name somewhere. I just want to simple drop another font to that folder and list it.
I found this as a solution to list all the files inside my resources/fonts folder
Fonts.GetFontFamilies(new Uri("pack://application:,,,/resources/fonts/#"))
I expect it to help you organize your fonts.
I use xaml for this
<Window
FontFamily ="./Fonts/#somefont"
>
I included the font file inside the "Fonts" folder
First) Copy fonts to project folder like /Resources/Fonts/ and set font-properties -> Build Action:Resource
Second) Used on code
FontFamily="/AssemblyNamespace;component/Resources/Fonts/IranSans/#IRANSansWeb Medium"
Although the solution from #movgp0 worked at first, it failed when I wanted a Bold text. By searching a bit more online I found the following solution that works in all cases:
<Application.Resources>
<FontFamily x:Key="FiraSansBold">Fonts/FiraSansCondensed-Bold.ttf#Fira Sans Condensed</FontFamily>
<FontFamily x:Key="FiraSansBoldItalic">Fonts/FiraSansCondensed-BoldItalic.ttf#Fira Sans Condensed</FontFamily>
<FontFamily x:Key="FiraSansItalic">Fonts/FiraSansCondensed-Italic.ttf#Fira Sans Condensed</FontFamily>
<FontFamily x:Key="FiraSans">Fonts/FiraSansCondensed-Regular.ttf#Fira Sans Condensed</FontFamily>
</Application.Resources>
Usage: <Label FontFamily="{StaticResource FiraSansBold}">SomeBoldedLabel</Label>
I have a fonts folder inside an assets folder in my project. At build time it will copy those fonts into the folder as content. Then I can simply use the following XAML to use the FontAwesome font to have an icon on my button.
<Button Content="" FontFamily="./assets/fonts/#FontAwesome">
Here's What Worked for me:
(1) Add Directory to project: font
(2) Move ttf font file into font directory
(3) Add ttf font file to project
(4) Set the "Build" Property of the tff font file to "Resource"
(Note: I used "Resource" instead of "Embedded Resource"
in the combobox selection.)
(5) Open the Window where you are using the font and make the
following changes:
<Window ... >
<Window.Resources>
<FontFamily x:Key="YourFontNickName">
pack://application:,,,/font/#NameOfFont
<!-- Note: NameOfFont can be font by double clicking
on font from explorer and writing down name
of the font reported. Its not the same
as the file name -->
</FontFamily>
</Window.Resources>
<!-- Use font as Xaml -->
<StackPanel>
<Textblock FontFamily="{StaticResource YourFontNickName}">
This is a test
</Testblock>
<Textblock Name="text1"/>
</StackPanel>
...
</Window>
(6) If you want to change the font from code. Do this:
public partial class Window1 : Window {
// Use font as C# Code
public void UpdateText1() {
text1.Text = "Hi There";
FontFamily ff = this.Resources["YourFontNickName"]
as FontFamily;
if (ff == null) {
Messagebox.Show("Wrong Font Name", "ERROR");
return;
}
text1.FontFamily = ff;
text1.FontSize = 30;
}
...
}
You need to add fonts as resource item
according to the link; you may add your desired font as application resource file by following these steps:
copy your font file to anywhere in your project in most cases resource files is copied into a folder named "resources" if you want to follow this, create new folder in your project and name it as "resources" then copy your font file which is usually have ttf or otf format into that folder
then you need to declare this file as a resource file for doing so you have two options:
right click on your font file, select properties (or press F4 after selecting it)
and then change "Build Action" to "Resource"
go into your projects directory edit project-name.csproj file and the
following tag:
<ItemGroup>
<Resource Include="resources\<font-file-name>.ttf" />
</ItemGroup>```
finally within you application you may set font-family property like this:
FontFamily="./Resources/Fonts/#<font-name>"
be careful 'font-name' is different from font-file-name you may open font file and check for font name property (in Microsoft Window)
I was trying to get this to work as well with a different font,
it only worked for me in this format with the ./Fonts
<FontFamily x:Key="NotoSans">pack://application:,,,./Fonts/#Noto Sans</FontFamily>
Kind of, Really easy:
<TextBlock x:Name="lblTitle" FontSize="24" Margin="256,25,178,289" Text="gg" FontFamily="/FontTest;component/#Arvo"></TextBlock>
When you import a font, vs will gonna include it in font list😊.

Categories