Hi, I am following Charles Petzold's new Xamarin Book published by MSFT and got stuck in the paragraph about scrollviews inside stack layout.
I haven't found a way to have this example (page 74) working correctly. Anyone can help?
It has an inner stacklayout inside a scrollview inside an outer stacklayout. The outer stacklayout has a label above the scroll view.
Mr Petzel writes:
_Notice that the ScrollView has its VerticalOptions property set to LayoutOptions.-
FillAndExpand. Without that, this program won’t work. With it, the text is scrollable while the title
stays in place.
__
It is not what I see... if I scroll the view upwards, the label is semi-covered by the label (see first Capture)
If I set the label's background to WHITE, it is even worse: the label disappears completely (see second screenshot)
How to tell the scrollview: do not expand ABOVE the label in your stacklayout?
This is the code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace BlackCat
{
class BlackCatPage : ContentPage
{
public BlackCatPage()
{
StackLayout mainStack = new StackLayout();
StackLayout textStack = new StackLayout
{
Padding = new Thickness(5),
Spacing = 10
};
// Get access to the text resource.
Assembly assembly = this.GetType().GetTypeInfo().Assembly;
string resource = "BlackCat.Texts.TheBlackCat.txt";
using (Stream stream = assembly.GetManifestResourceStream(resource))
{
using (StreamReader reader = new StreamReader(stream))
{
bool gotTitle = false;
string line;
// Read in a line (which is actually a paragraph).
while (null != (line = reader.ReadLine()))
{
Label label = new Label
{
Text = line,
// Black text for ebooks!
TextColor = Color.Black
};
if (!gotTitle)
{
// Add first label (the title to mainStack.
label.HorizontalOptions = LayoutOptions.Center;
label.Font = Font.SystemFontOfSize(NamedSize.Medium,
FontAttributes.Bold);
mainStack.Children.Add(label);
gotTitle = true;
}
else
{
// Add subsequent labels to textStack.
textStack.Children.Add(label);
}
}
}
}
// Put the textStack in a ScrollView with FillAndExpand.
ScrollView scrollView = new ScrollView
{
Content = textStack,
VerticalOptions = LayoutOptions.FillAndExpand,
Padding = new Thickness(5, 0)
};
// Add the ScrollView as a second child of mainStack.
mainStack.Children.Add(scrollView);
// Set page content to mainStack.
this.Content = mainStack;
// White background for ebooks!
this.BackgroundColor = Color.White;
// Add some iOS padding for the page
this.Padding = new Thickness(0, Device.OnPlatform(20, 0, 0), 0, 0);
}
}
}