I have a complex page layout that involves a Grid into which I dynamically insert other Layouts/Views. On iOS and Android, this works fine, but on UWP I hit a problem when inserting an Entry (nested in a ScrollView) that is bound to a non-Empty string. In that situation, the height of the Entry does not take into account the FontSize of the text in the Entry.
I've simplified the code down to the following, where an instance of BugUWPEntryBindingPageView gets pushed onto the navigation stack.
If the Entry is bound to ValueOne (initially a non-empty string) the height of the Entry is incorrect when the Entry is added to the Grid.
If the Entry is bound to ValueTwo (initially an empty string) the height of the Entry is correct.
This looks like a Xamarin.Forms bug to me (I'm using XF 2.4.0), but (other than always starting with an empty string and later setting the correct value in the ViewModel) has anybody got any ideas for a workaround? (I don't want to wait for Xamarin to release a fix before getting this particular page working as expected)
using Xamarin.Forms;
namespace ViewsUsingXamarinForms
{
public class BugUWPEntryBindingViewModel
{
public BugUWPEntryBindingViewModel()
{
ValueTwo = string.Empty;
ValueOne = "123";
}
public string ValueOne { get; set; }
public string ValueTwo { get; set; }
} // public class BugUWPEntryBindingViewModel
public class BugUWPEntryBindingPageView : ContentPage
{
private static readonly BugUWPEntryBindingViewModel ViewModel = new BugUWPEntryBindingViewModel();
private Grid _grid;
protected override void OnAppearing()
{
_grid = new Grid
{
BackgroundColor = Color.White,
HorizontalOptions = LayoutOptions.Fill,
VerticalOptions = LayoutOptions.FillAndExpand,
ColumnDefinitions = new ColumnDefinitionCollection
{
new ColumnDefinition { Width = GridLength.Star },
},
RowDefinitions = new RowDefinitionCollection
{
new RowDefinition {Height = GridLength.Auto },
new RowDefinition {Height = GridLength.Star }
},
};
_grid.Children.Add(
new Button
{
Text = "Add/replace entries",
BackgroundColor = Color.Blue,
TextColor = Color.White,
Command = new Command(OnAddEntries)
}, 0, 1, 0, 1);
BindingContext = ViewModel;
Content = _grid;
}
private void OnAddEntries()
{
Entry entry = new Entry
{
FontFamily = "Verdana", //DeviceWrapper.DefaultFontFamily,
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Entry)),
};
entry.SetBinding(
Entry.TextProperty,
new Binding(
nameof(BugUWPEntryBindingViewModel.ValueOne),
BindingMode.TwoWay,
null,
string.Empty));
_grid.Children.Add(
new ScrollView
{
Content = new StackLayout
{
Children =
{
entry
}
}
}, 0, 1, 1, 2);
}
}
}