Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug] [UWP] Setting the FontFamily crashes a multi-window app #11705

Closed
KPixel opened this issue Aug 8, 2020 · 3 comments · Fixed by #11752
Closed

[Bug] [UWP] Setting the FontFamily crashes a multi-window app #11705

KPixel opened this issue Aug 8, 2020 · 3 comments · Fixed by #11752
Labels
a/fonts e/3 🕒 3 in-progress This issue has an associated pull request that may resolve it! p/UWP t/bug 🐛
Milestone

Comments

@KPixel
Copy link

KPixel commented Aug 8, 2020

Description

Since #2432, Xamarin.Forms supports creating apps with multiple windows in UWP.
However, if we use the same custom font on these windows, then we get an exception when trying to create the secondary windows.

Steps to Reproduce

The Control Gallery already has a multi-window sample, we just need to update it to set a custom FontFamily:

  1. Update the content page that is created for these secondary windows:

    After this line, add:
if (Device.RuntimePlatform == Device.UWP)
    layout.Children.Add(new Label { Text = "\xE76E", FontFamily = "Segoe MDL2 Assets", FontSize = 32 });
  1. Run the Control Gallery app and click on the "Open Secondary Window" button twice.

Expected Behavior

The app should create as many windows as we want.

Actual Behavior

The app crashes when we try to create its third window.

Basic Information

  • Version with issue: 4.8.0.1269
  • Last known good version: N/A
  • IDE: Visual Studio 2019 16.7
  • Platform Target Frameworks:
    • UWP: 18362

Solution

This issue is caused by the fact that fonts are cached; so they end up being shared by all windows (which is not allowed):

static Dictionary<string, FontFamily> FontFamilies = new Dictionary<string, FontFamily>();

The solution should be to change that cache to be thread-static:

[ThreadStatic]
static Dictionary<string, FontFamily> FontFamilies;

Then, instantiate it before this line:

if (FontFamilies.TryGetValue(fontFamily, out var f))

if (FontFamilies == null)
    FontFamilies = new Dictionary<string, FontFamily>();

That should be enough.

@KPixel KPixel added s/unverified New report that has yet to be verified t/bug 🐛 labels Aug 8, 2020
@samhouts samhouts added this to New in Triage Aug 8, 2020
@KPixel
Copy link
Author

KPixel commented Aug 9, 2020

Another bug (which could go into its own issue if needed)

The secondary window does not resize its ContentPage. It stays at the size of the parent window.
For example, you can maximize the Control Gallery app, then create the secondary window, and its content will always be at the size of a maximized window, even if you reduce the size of the window itself (the page's content will be clipped).

I don't know exactly why this happens, but my guess is that it is related to this code:

frameworkElement.Loaded += (sender, args) =>
{
visualElement.Layout(new Rectangle(0, 0, frameworkElement.ActualWidth, frameworkElement.ActualHeight));
};

My current workaround is to update the SecondaryWindowService:

Before this line, I added:

Window.Current.SizeChanged += (sender, args) =>
{
    instance.Layout(new Rectangle(0, 0, args.Size.Width, args.Size.Height));
};

With that, the page is resized accordingly.

@rmarinho rmarinho added p/UWP a/fonts e/5 🕔 5 and removed s/unverified New report that has yet to be verified labels Aug 11, 2020
@rmarinho rmarinho moved this from New to Ready For Work in Triage Aug 11, 2020
@rmarinho rmarinho added e/3 🕒 3 and removed e/5 🕔 5 labels Aug 11, 2020
@rmarinho rmarinho linked a pull request Aug 11, 2020 that will close this issue
2 tasks
@rmarinho
Copy link
Member

Thanks @KPixel , did a PR your recommendations.

@samhouts samhouts added the in-progress This issue has an associated pull request that may resolve it! label Aug 11, 2020
@samhouts samhouts added this to In Progress in vCurrent (4.8.0) Aug 11, 2020
@KPixel
Copy link
Author

KPixel commented Aug 12, 2020

Awesome. I will continue the discussion on the PR.

@samhouts samhouts added this to the 5.0.0 milestone Aug 13, 2020
@samhouts samhouts added this to To do in UWP Ready For Work Aug 17, 2020
@samhouts samhouts removed this from Ready For Work in Triage Aug 17, 2020
@samhouts samhouts moved this from To do to In progress in UWP Ready For Work Aug 17, 2020
@samhouts samhouts removed this from In Progress in vCurrent (4.8.0) Aug 26, 2020
@samhouts samhouts added this to In Progress in vNext+1 (5.0.0) Aug 26, 2020
@PureWeen PureWeen added this to To do in v5.0.1 via automation Nov 2, 2020
@PureWeen PureWeen removed this from In Progress in vNext+1 (5.0.0) Nov 2, 2020
@PureWeen PureWeen modified the milestones: 5.0.0, 5.0.1 Nov 2, 2020
@samhouts samhouts added this to In Progress in vNext+1 (5.0.0) Nov 2, 2020
@PureWeen PureWeen removed this from In Progress in vNext+1 (5.0.0) Nov 5, 2020
@samhouts samhouts added this to In Progress in vNext+1 (5.0.0) Nov 5, 2020
@PureWeen PureWeen removed this from In Progress in vNext+1 (5.0.0) Nov 5, 2020
UWP Ready For Work automation moved this from In progress to Done Jan 15, 2021
v5.0.1 automation moved this from To do to Done Jan 15, 2021
rmarinho added a commit that referenced this issue Jan 15, 2021
* [Controls] Add code to reproduce #11705

* [UWP] Cached fonts can't be share by multiple windows

* [UWP] Fixes for second window resize

* Update SecondaryWindowService.cs
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/fonts e/3 🕒 3 in-progress This issue has an associated pull request that may resolve it! p/UWP t/bug 🐛
Projects
Development

Successfully merging a pull request may close this issue.

4 participants