I'm trying to create a Grid Layout. I can't find any "Template" option for when binding to a ViewModel, so I'm trying to hand roll this thing.
Here's my XML
<ScrollView x:Name="ModuleScrolLView"
BackgroundColor="#d3d6db">
<StackLayout x:Name="ModuleStackLayout"
Orientation="Vertical"
VerticalOptions="FillAndExpand">
<Grid x:Name="SegmentGrid"
RowSpacing="10"
ColumnSpacing="10"
VerticalOptions="StartAndExpand">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
</Grid>
<WebView Source="{Binding DescriptionHtml}" BackgroundColor="White" />
</StackLayout>
</ScrollView>
And here's my code behind
public partial class Module : ContentPage
{
private readonly ModulePageViewModel _viewModel;
public Module()
{
_viewModel = new ModulePageViewModel();
InitializeComponent();
BindingContext = _viewModel;
var subscriber = new ModuleSubscription();
WatchForViewModelUpdates(subscriber);
}
private void WatchForViewModelUpdates(ModuleSubscription subscriber)
{
MessagingCenter.Subscribe<MainMenu, ModulePageViewModel>(subscriber, "CurrentModuleMessage", (s, e) =>
{
subscriber.CurrentModule = e;
_viewModel.Title = e.Title;
_viewModel.Description = e.Description;
_viewModel.Segments = FetchSegmentListAsync(e.Id).Result;
BuildSegmentCards();
});
}
private static Task<IList<SegmentViewModel>> FetchSegmentListAsync(Guid id)
{
var segmentService = App.Container.Resolve<ISegmentService>();
return Task.Run(() => segmentService.FindByModuleId(id));
}
private void BuildSegmentCards()
{
var grid = ((Grid)SegmentGrid);
grid.RowDefinitions.Clear();
for (var i = 0; i < _viewModel.Segments.Count; i++)
{
var column = i % 2;
var row = i / 2; // ints round up (1.5 rounds to 2)
if (row % 2 != 0)
{
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
}
grid.Children.Add(BuildFrame(_viewModel.Segments[i]), column, row);
}
}
private Frame BuildFrame(SegmentViewModel value)
{
var palleteFrame = new Frame
{
BackgroundColor = Color.White,
Padding = 12,
Content = CreateStack(value),
HasShadow = false,
VerticalOptions = LayoutOptions.FillAndExpand
};
return palleteFrame;
}
private StackLayout CreateStack(SegmentViewModel value)
{
var stack = new StackLayout
{
Spacing = 3,
Orientation = StackOrientation.Vertical,
VerticalOptions = LayoutOptions.FillAndExpand,
Children =
{
new Label {Text = value.Number.ToString(), BackgroundColor = Color.White, TextColor = Color.Aqua.WithLuminosity(0.3f)},
new Label {Text = value.Title, BackgroundColor = Color.White, TextColor = Color.Aqua.WithLuminosity(0.3f)},
},
};
return stack;
}
The problem I'm getting is the error
NaN is not a valid value for height
How can I fix this?
06-16 14:32:55.651 E/SQLiteLog( 4732): (1) statement aborts at 2: [ROLLBACK] cannot rollback - no transaction is active
06-16 14:32:55.651 E/mono ( 4732):
06-16 14:32:55.651 E/mono ( 4732): Unhandled Exception:
06-16 14:32:55.651 E/mono ( 4732): System.ArgumentException: NaN is not a valid value for height
06-16 14:32:55.651 E/mono ( 4732): at Xamarin.Forms.Size..ctor (Double width, Double height) [0x00000] in <filename unknown>:0
06-16 14:32:55.651 E/mono ( 4732): at Xamarin.Forms.StackLayout.SumOfSizeRequests (Double widthConstraint, Double heightConstraint, System.Int32& numOfExpanders) [0x00000] in <filename unknown>:0
06-16 14:32:55.651 E/mono ( 4732): at Xamarin.Forms.StackLayout.OnSizeRequest (Double widthConstraint, Double heightConstraint) [0x00000] in <filename unknown>:0
06-16 14:32:55.651 E/mono ( 4732): at Xamarin.Forms.VisualElement.GetSizeRequest (Double widthConstraint, Double heightConstraint) [0x00000] in <filename unknown>:0
06-16 14:32:55.651 E/mono ( 4732): at Xamarin.Forms.Layout.GetSizeRequest (Double widthConstraint, Double heightConstraint) [0x00000] in <filename unknown>:0
06-16 14:32:55.651 E/mono ( 4732): at Xamarin.Forms.ScrollView.LayoutChildren (Double x, Double y, Double width, Double height) [0x00000] in <filename unknown>:0
06-16 14:32:55.651 E/mono ( 4732): at Xamarin.Forms.Layout.UpdateChildrenLayout () [0x00000] in <filename unknown>:0
06-16 14:32:55.651 E/mono ( 4732): at Xamarin.Forms.Layout.OnSizeAllocated (Double width, Double height) [0x0
06-16 14:32:55.651 E/mono-rt ( 4732): [ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentException: NaN is not a valid value for height
06-16 14:32:55.651 E/mono-rt ( 4732): at Xamarin.Forms.Size..ctor (Double width, Double height) [0x00000] in <filename unknown>:0
06-16 14:32:55.651 E/mono-rt ( 4732): at Xamarin.Forms.StackLayout.SumOfSizeRequests (Double widthConstraint, Double heightConstraint, System.Int32& numOfExpanders) [0x00000] in <filename unknown>:0
06-16 14:32:55.651 E/mono-rt ( 4732): at Xamarin.Forms.StackLayout.OnSizeRequest (Double widthConstraint, Double heightConstraint) [0x00000] in <filename unknown>:0
06-16 14:32:55.651 E/mono-rt ( 4732): at Xamarin.Forms.VisualElement.GetSizeRequest (Double widthConstraint, Double heightConstraint) [0x00000] in <filename unknown>:0
06-16 14:32:55.651 E/mono-rt ( 4732): at Xamarin.Forms.Layout.GetSizeRequest (Double widthConstraint, Double heightConstraint) [0x00000] in <filename unknown>:0
06-16 14:32:55.651 E/mono-rt ( 4732): at Xamarin.Forms.ScrollView.LayoutChildren (Double x, Double y, Double width, Double height) [0x00000] in <filename unknown>:0
06-16 14:32:55.651 E/mono-rt ( 4732): at Xamarin.Forms.Layout.UpdateChildrenLayout () [0x00000] in <filename unknown>:0
06-16 14:32:55.651 E/mono-rt ( 4732): at Xamarin.Forms.Layout.OnSizeAllocated (Double width, Double