I am creating an application with Xamarin using the shared definition, and my application should run on Android and as an UWP app. I had followed the sample showed in DataGrid's repository, and it works very well on Android, but I am having an issue when I try to run as a UWP app. The issue is that, when I run as a UWP app with a populated List and try to binding this list on the DataGrid, the app crashes, showing this Exception:
"System.Runtime.InteropServices.COMException: Error HRESULT E_FAIL has been returned from a call to a COM component.
at Windows.UI.Xaml.UIElement.Measure(Size availableSize)
at Xamarin.Forms.Platform.UWP.VisualElementRenderer`2.MeasureOverride(Size availableSize)
at Windows.UI.Xaml.UIElement.Measure(Size availableSize)
at Xamarin.Forms.Platform.UWP.VisualElementRenderer`2.MeasureOverride(Size availableSize)
at Windows.UI.Xaml.UIElement.Measure(Size availableSize)
at Xamarin.Forms.Plat"
As I had said this exception only occur on UWP, and only happens when this List is populate. The list is create in this way:
private List<TableValue> _tableValues;
public List<TableValue> TableValues
{
get { return _tableValues; }
set
{
TableValues= value;
OnPropertyChanged(nameof(TableValues));
}
}
private List<TableValue> CreateTableValues()
{
return new List<TableValue>{
new TableValue{Val1="", Val2="", Val3="", Val4="", Val5="", Val6="", Val7=""}
};
}
public async Task PopulateItems()
{
_tableValues = CreateTableValues();
UpdateProperty();
}
This list is create inside of the MVVM class of the View. The DataGrid element is create in the View in that way:
private ScrollView CreateTable()
{
table = new DataGrid();
table.Columns.Clear();
table.Columns.AddRange(CreateColumns());
table.SelectionEnabled = true;
table.BackgroundColor = Color.White;
table.SetBinding(DataGrid.ItemsSourceProperty, new Binding("TableValues"));
table.HeaderBackground = Color.FromHex("#D4D0C8");
table.HeaderFontSize = 18;
table.HeaderTextColor = Color.FromHex("#08098F");
StackLayout tableStack = new StackLayout
{
BackgroundColor = Color.White,
HeightRequest = 850,
VerticalOptions = LayoutOptions.FillAndExpand
};
tableStack.Children.Add(table);
var scroll = new ScrollView
{
BackgroundColor = Color.White,
Margin = new Thickness(5, 5, 5, 5),
Content = tableStack,
Orientation = ScrollOrientation.Horizontal,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand
};
return scroll;
}