Bug 39399 - [UWP] Setting FontSize on Entry causes The application called an interface that was marshalled for a different thread
Summary: [UWP] Setting FontSize on Entry causes The application called an interface th...
Status: CONFIRMED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.1.0
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-03-06 21:17 UTC by Adam
Modified: 2017-01-05 20:09 UTC (History)
8 users (show)

Tags: ac
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 39399 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description Adam 2016-03-06 21:17:08 UTC
Version 2.1.0

When I set a FontSize to an Entry control I get the below error.

For example
<Entry HorizontalOptions="FillAndExpand" VerticalOptions="End" FontSize="26"  HeightRequest="50" HorizontalTextAlignment="Center" Keyboard="Numeric" />


   at Windows.UI.Xaml.Application.get_Resources()
   at Xamarin.Forms.Platform.UWP.FontExtensions.GetFontSize(NamedSize size)
   at Xamarin.Forms.Platform.UWP.WindowsBasePlatformServices.GetNamedSize(NamedSize size, Type targetElementType, Boolean useOldSizes)
   at Xamarin.Forms.Entry.<>c.<.cctor>b__45_0(BindableObject bindable)
   at Xamarin.Forms.BindableObject.CreateAndAddContext(BindableProperty property)
   at Xamarin.Forms.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes)
   at Xamarin.Forms.BindableObject.SetValue(BindableProperty property, Object value, Boolean fromStyle, Boolean checkAccess)
   at Xamarin.Forms.BindableObject.SetValue(BindableProperty property, Object value)
   at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.SetPropertyValue(Object xamlelement, XmlName propertyName, Object value, BindableObject rootElement, INode node, HydratationContext context, IXmlLineInfo lineInfo)
   at Xamarin.Forms.Xaml.ApplyPropertiesVisitor.Visit(ValueNode node, INode parentNode)
   at Xamarin.Forms.Xaml.ValueNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Xamarin.Forms.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Xamarin.Forms.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Xamarin.Forms.Xaml.ElementNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Xamarin.Forms.Xaml.RootNode.Accept(IXamlNodeVisitor visitor, INode parentNode)
   at Xamarin.Forms.Xaml.XamlLoader.Load(BindableObject view, String xaml)
   at Xamarin.Forms.Xaml.XamlLoader.Load(BindableObject view, Type callingType)
   at Xamarin.Forms.Xaml.Extensions.LoadFromXaml[TView](TView view, Type callingType)
Comment 1 Adam 2016-03-10 02:15:05 UTC
I just retested in the latest version 2.1.0.6526 and this error is still present.
Comment 2 Rui Marinho 2016-03-10 20:26:59 UTC
Thank you for taking the time to submit the bug. We tried to reproduce the issue you reported but were unable given the description. If you could please attach a reproduction to the bug by starting with a clean Xamarin.Forms project and adding just the code necessary to demonstrate the issue we would very much appreciate it.

Warm regards,
Xamarin Forms Team
Comment 3 Adam 2016-05-02 00:53:20 UTC
Repro

        public App()
        {
            InitializeComponent();
            MainPage = new NavigationPage();
            
            Task.Run(() => LoadPage());
            
        }

        private async Task LoadPage()
        {
            var mainPage = new MainPage();

            Device.BeginInvokeOnMainThread(() =>
            {
                try
                {
                    ((NavigationPage)MainPage).PushAsync(mainPage);
                }
                catch (Exception ex)
                {

                }
            });


        }
Comment 4 Adam 2016-05-02 02:03:18 UTC
I did hesitate on this PR https://github.com/xamarin/Xamarin.Forms/pull/156

As I am not sure on Xamarin policy you may just say, always call the constructor on a UI Thread and it shouldn't be fixed but I believe it fixes a potential issue that will save many UWP/WinRT developers a lot of time discovering the issue with none/negligible cost to app performance.
Comment 5 John Hardman 2016-08-21 19:57:13 UTC
I have also hit this problem, setting the FontSize of a Label on UWP using XF 2.3.1.114 .


The Label is not yet integrated into the page's UI hierarchy, so it should be possible to set FontSize, even on a different thread. The code below demonstrates the problem. This works on Android, but not on UWP.


        public async Task<View> CreateContentViewAsync()
        {
            await Task.Delay(5000);

            return await Task.Run<View>(() => new Label
            {
                Text = "Just checking",
                TextColor = Color.Black,
                BackgroundColor = Color.White,
                FontSize = 22,
                VerticalOptions = LayoutOptions.Center,
                HorizontalOptions = LayoutOptions.Center
            });
        }
Comment 6 John Hardman 2016-08-21 20:23:11 UTC
I've just read the conversation relating to the pull request mentioned above.

In my particular case, I hit this when trying to identify best practice for using IsBusy (taking the Xamarin Sport app as a starting point). When creating page B to push from page A, I found that I could set IsBusy to true on A, and at the start of the constructor for B could set IsBusy on B, but even so still did not see the busy indicator appear on Android (on UWP there are two issues about the busy indicator, which I am raising separately). In order to get the busy indicator to appear on Android, I thought (as I had been told during a Xamarin University session that this was a valid thing to do) I'd do the creation on a background task of the view that would become the content of a ContentPage when (on the UI thread) connected up to the page, with the IsBusy indicator being set and cleared as appropriate on the UI thread. This does what I want on Android, has no ill effect on iOS, but results in the wrong thread being reported on UWP. Removing the "FontSize = 22," from the label creation allows UWP to build everything successfully (other than setting the font size).

The code below is my experimental page for testing this. It uses a lot of separate infrastructure type stuff, so treat this as pseudo-code just to give some content:

using System;
using System.Threading.Tasks;

using Xamarin.Forms;

using CommonInfrastructure;

namespace ViewsUsingXamarinForms
{
    public interface IMyAppSupportsContentViewCreation
    {
        Task<View> CreateContentViewAsync();

    } // public interface IMyAppSupportsContentViewCreation

    public class ExperimentalBusyPageView : MyAppContentPageView, IMyAppSupportsContentViewCreation
    {
        private MyAppContentPageView _parentPage;
        private string _pageStyleStem; 

        public static Task ShowPageAsync(
            MyAppContentPageView parentPage)
        {
            using (new BusyPage(parentPage))
            {
                if (!PageTypeIsAlreadyAtTopOfStack(parentPage, typeof(ExperimentalBusyPageView)))
                {
                    ExperimentalBusyPageView pageView = new ExperimentalBusyPageView(
                        parentPage);
                    return parentPage.PushRightPageTaskAsync(pageView);
                }
                else
                {
                    return Task.FromResult(0);
                }
            }
        }

        private ExperimentalBusyPageView(MyAppContentPageView parentPage) 
            : base("ExperimentalBusyPageView", parentPage.MyAppViewState)
        {

            this.IsBusy = true;

            _parentPage = parentPage;
            _pageStyleStem = "MyAppStylePage";
        }

        private void SetPageStyle()
        {
            if (Orientation == MyAppOrientation.Portrait)
            {
                this.Style =
                    (Style)_parentPage.MyAppViewState.ApplicationResourceDictionary[_pageStyleStem + "Portrait"];
            }
            else
            {
                this.Style =
                    (Style)_parentPage.MyAppViewState.ApplicationResourceDictionary[_pageStyleStem + "Landscape"];
            }
        }

        protected override void OnOrientationChange()
        {
            base.OnOrientationChange();
            SetPageStyle();
        }

        private async Task CreateContentViewAndApplyToPageAsync()
        {
            View view = await CreateContentViewAsync();
            this.Content = view;
            SetPageStyle();
            this.IsBusy = false;
        }

        protected override async Task<bool> OnFirstAppearanceAsync()
        {
            try
            {
                await base.OnFirstAppearanceAsync();
                await CreateContentViewAndApplyToPageAsync();
                return true;
            }
            catch (Exception ex)
            {
                try
                {
                    await InsightsWrapper.ReportExceptionAsync(ex);
                }
                catch (Exception ex2)
                {
                    System.Diagnostics.Debug.WriteLine(ex2);
                }

                return false;
            }
        }

        protected override async void OnAppearing()
        {
            try
            {
                base.OnAppearing();

                if (NeedToRecreatePage)
                {
                    await CreateContentViewAndApplyToPageAsync();
                    NeedToRecreatePage = false;
                }
            }
            catch (Exception ex)
            {
                try
                {
                    await InsightsWrapper.ReportExceptionAsync(ex);
                }
                catch (Exception ex2)
                {
                    System.Diagnostics.Debug.WriteLine(ex2);
                }
            }
        }

        public async Task<View> CreateContentViewAsync()
        {
            await Task.Delay(5000);

            return await Task.Run<View>(() =>
            {
                Label label = new Label
                {
                    Text = "Just checking",
                    TextColor = Color.Black,
                    BackgroundColor = Color.White,
                    VerticalOptions = LayoutOptions.Center,
                    HorizontalOptions = LayoutOptions.Center
                };
                // this is a workaround for bug https://bugzilla.xamarin.com/show_bug.cgi?id=39399
                if (ApplicationType.IsAndroid() || ApplicationType.IsiOS())
                    label.FontSize = Device.GetNamedSize(NamedSize.Large, typeof (Label));

                return label;
            });

        }

    } // public class ExperimentalBusyPageView : ContentPage

} // namespace ViewsUsingXamarinForms

// eof