asthomas
Topic Author
Posts: 17
Joined: 28 Dec 2021, 16:20

Can I set the default font using XAML?

19 Jan 2022, 15:34

I see the included themes use MergedDictionaries, like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary ResourceDictionary.Source="NoesisTheme.Brushes.LightAqua.xaml" />
    <ResourceDictionary ResourceDictionary.Source="NoesisTheme.Fonts.xaml" />
    <ResourceDictionary ResourceDictionary.Source="NoesisTheme.Styles.xaml" />
  </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
I decided I would like to change the default font face to "Segoe UI" and the default size from 15 point to 12 point, so I did this in App.xaml:
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Noesis.GUI.Extensions;component/Theme/NoesisTheme.Brushes.LightAqua.xaml" />
                <ResourceDictionary>
                    <FontFamily x:Key="Font.Family.Default">Segoe UI</FontFamily>
                    <sys:Double x:Key="Font.Size.Header">13</sys:Double>
                    <sys:Double x:Key="Font.Size.Normal">12</sys:Double>
                    <sys:Double x:Key="Font.Size.ToolTip">10</sys:Double>
                </ResourceDictionary>
                <ResourceDictionary Source="pack://application:,,,/Noesis.GUI.Extensions;component/Theme/NoesisTheme.Styles.xaml" />
            </ResourceDictionary.MergedDictionaries>
Effectively, I replaced the Fonts.xaml resources with my own, leaving everything else the same.

This does not affect the fonts for TextBlock, Button, etc. Looking in NoesisTheme.Styles.xaml, it appears that only a few controls like groupbox, tooltips and menuitems even reference these font settings.

So, I added this code to App.OnStartup, just to prove that I could affect the fonts at all:
            string[] fonts = new string[] { "Segoe UI", "Arial", "Segoe UI Emoji" };
            Noesis.GUI.SetFontFallbacks(fonts);
            Noesis.GUI.SetFontDefaultProperties(12, FontWeight.Normal, FontStretch.Normal, FontStyle.Normal);
That seems to have worked.

The problem is that this only applies to a Noesis build. If I build the same code for WPF, it does not get these settings. The result is that the WPF target, which is the best editing environment, does not look like the eventual Noesis build.

So, can I affect the default font through XAML and have it be consistent between WPF and Noesis builds?
 
asthomas
Topic Author
Posts: 17
Joined: 28 Dec 2021, 16:20

Re: Can I set the default font using XAML?

19 Jan 2022, 15:43

One possible approach would be to use Application.FindResource to look at the XAML and then execute code in App.OnStartup.
See https://docs.microsoft.com/en-us/dotnet ... work-4.6.1

However, Application.FindResource is not implemented. Is that intentional or an oversight?
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: Can I set the default font using XAML?

19 Jan 2022, 19:12

Hello,

In Blend if you don't specify a font in your xamls it will use the font from the operating system (Segoe UI) as the default one. In Noesis that default font is set using SetFontFallbacks and SetFontDefaultProperties. This is like the fallback value if you don't define a font family in your styles.
So, can I affect the default font through XAML and have it be consistent between WPF and Noesis builds?
Usually you design the theme styles to allow property inheritance of the FontFamily and FontSize, so you can override the default font of your application or any part of the UI by just setting the properties in a root element. This is the approach we followed with NoesisTheme.
<Window ...
        FontFamily="{StaticResource Font.Family.Default}"
        FontSize="{StaticResource Font.Size.Normal}">
</Window>
<StackPanel TextElement.FontFamily="Fonts/#Arial">
  <Button Content="Button 1"/> <!-- font properties inherited from the StackPanel -->
  <Button Content="Button 2"/>
  <Button Content="Button 3"/>
</StackPanel>
Instead of that approach you can explicitly define the font and its properties for all the controls and text styles in your theme:
<Style x:Key="BaseStyle">
  <Setter Property="TextElement.FontFamily" Value="Fonts/#Arial"/>
  <Setter Property="TextElement.FontSize" Value="12"/>
</Style>

<Style TargetType="TextBlock" BasedOn="{StaticResource BaseStyle}"/>
<Style TargetType="Control" BasedOn="{StaticResource BaseStyle}">
  ...
</Style>
<Style TargetType="Button" BasedOn="{StaticResource {x:Type Control}}">
  ...
</Style>
...
This way the application will use those properties without specifying anything else.
In case you want to change a particular element you still can do that by setting the properties locally in the element:
<Button FontFamily="Fonts/#Roboto"/>
But inheritance won't work as style properties will have more priority than inherited values:
<StackPanel TextElement.FontFamily="Fonts/#Arial">
  <Button Content="Button 1"/> <!-- these buttons will still use the font defined in the Button style -->
  <Button Content="Button 2"/>
  <Button Content="Button 3"/>
</StackPanel>
One possible approach would be to use Application.FindResource to look at the XAML and then execute code in App.OnStartup.
See https://docs.microsoft.com/en-us/dotnet ... work-4.6.1

However, Application.FindResource is not implemented. Is that intentional or an oversight?
Sorry, not implemented yet, could you please report it in our bugtracker? In the meantime you can directly search for the resource in the application resources like this:
Application.Current.Resources["somekey"]
I hope this helps.
 
asthomas
Topic Author
Posts: 17
Joined: 28 Dec 2021, 16:20

Re: Can I set the default font using XAML?

20 Jan 2022, 00:46

Thanks for the explanation. I rolled my own FindResource and confirmed that I can set the default font characteristics during Application.OnStartup from the XAML in App.xaml. I was hoping to produce something that automatically used the same font family and size as a native WPF application, but on further thought it does not seem like a reasonable request since Noesis is cross-platform. I'll just hard-code the Windows 10 defaults into my App.xaml and that will have to do.
 
User avatar
jsantos
Site Admin
Posts: 3906
Joined: 20 Jan 2012, 17:18
Contact:

Re: Can I set the default font using XAML?

20 Jan 2022, 13:10

I am going to mark this as solved. Application.FindResource will be implemented in #2245

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot], Semrush [Bot] and 72 guests