Loading resources from XAML, problem with fonts
-
I am (I have to) using an XAML file as a resource from my main window. In this simplified example, the XAML file looks like this
The file is loaded that way (the XAML file, as well as a 'image1.jpg' file, are in a 'Resources' folder along with the exe file)
Imports System.IO
Class MainWindow
Private Sub MainWindow_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
loadInterface()
End Sub
Sub loadInterface()
' Load xaml file as content of the window
Dim GridUri As String = System.AppDomain.CurrentDomain.BaseDirectory & "Resources\theme.xaml"
Dim fs As FileStream = New FileStream(GridUri, FileMode.Open, FileAccess.Read)
Dim sri = TryCast(System.Windows.Markup.XamlReader.Load(fs), Grid)
Me.Content = sri
fs.Close()
End Sub
End Class..and that works nicely Now I would like to use a label whit a font file taken in the same 'Resources' folder (the font used in this example: http://www.dafont.com/fr/digital-7.font)
<Label FontFamily="pack://siteoforigin:,,,/Resources/#Digital-7" Content="Have a nice day!"/>
...but then the label text isn't displayed with the proper FontFamily! What should I please do for the text of the label to be displayed using the font in the resources folder? Thanks very much!! PS: the font file CAN'T be in resource of the application. Think of this XAML as a theme, any font could be inside and the application couldn't have all those possible fonts in resources!
-
I am (I have to) using an XAML file as a resource from my main window. In this simplified example, the XAML file looks like this
The file is loaded that way (the XAML file, as well as a 'image1.jpg' file, are in a 'Resources' folder along with the exe file)
Imports System.IO
Class MainWindow
Private Sub MainWindow_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
loadInterface()
End Sub
Sub loadInterface()
' Load xaml file as content of the window
Dim GridUri As String = System.AppDomain.CurrentDomain.BaseDirectory & "Resources\theme.xaml"
Dim fs As FileStream = New FileStream(GridUri, FileMode.Open, FileAccess.Read)
Dim sri = TryCast(System.Windows.Markup.XamlReader.Load(fs), Grid)
Me.Content = sri
fs.Close()
End Sub
End Class..and that works nicely Now I would like to use a label whit a font file taken in the same 'Resources' folder (the font used in this example: http://www.dafont.com/fr/digital-7.font)
<Label FontFamily="pack://siteoforigin:,,,/Resources/#Digital-7" Content="Have a nice day!"/>
...but then the label text isn't displayed with the proper FontFamily! What should I please do for the text of the label to be displayed using the font in the resources folder? Thanks very much!! PS: the font file CAN'T be in resource of the application. Think of this XAML as a theme, any font could be inside and the application couldn't have all those possible fonts in resources!
You can always include the font as a content item. All you need to do is make sure that the font is available in the application directory. If you do this, you can then reference it like this:
<Label FontFamily="#Digital-7" Content="Have a nice day!" />
This space for rent