So I'm trying to have my code read text in from a file and display it in a scrollView, with a stationary label at the top. Think of an e-reader, where the book's title is pinned to the top of the screen, but the content scrolls. I've copied the text verbatim from an example in the "Creating Mobile Apps with Xamarin" book that I downloaded from Xamarin's website. However, when I run my code, no text is displayed; the screen just shows blank white, as though the app is running on the emulator, but has no code assigned to it. Using Visual Studio's debugger, I stepped into the code and realized that, for some reason, the variable "stream" is not being assigned a value -- its value is null, according to the debugger, which is causing a "value cannot be null" exception. This is only the second code I've tried to make with Xamarin Forms, using the book as a template, and I'm just stumped on why "stream" is not being assigned a value. Hopefully one of you lovely people can help me out!
If it would help to see the code I'm referencing, that can be found in the book available for download from Microsoft here, on pages 105-106 of the PDF (or pages 84-85, if you're going by the book's page numbers).
Things I've tried:
- I've used assembly.GetManifestResourceNames() to make sure I'm typing the resource ID correctly. I originally had it as "ereader.Texts.JP.txt" because the program itself is called cdern_lab10, and I thought that was the namespace. However, GetManifestResourceNames() returned "JP.Texts.JP.txt", so I changed it.
- I've double-checked to make sure the resource is marked as an embedded resource, and it is. I've also deleted and re-added the resource, and checked again to make sure it's embedded. It's stored in a folder within the project named "Texts".
Note: There was a similar question asked on here about a year ago (here), but it appears it was never really answered.
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using Xamarin.Forms;
namespace ereader
{
class TextPage : ContentPage
{
public TextPage()
{
StackLayout mainStack = new StackLayout();
StackLayout textStack = new StackLayout
{
Padding = new Thickness(5),
Spacing = 10
};
Assembly assembly = GetType().GetTypeInfo().Assembly;
string resource = "JP.Texts.JP.txt";
using (Stream stream = assembly.GetManifestResourceStream(resource))
{
using (StreamReader reader = new StreamReader (stream))
{
bool gotTitle = false;
string line;
while (null != (line = reader.ReadLine()))
{
Label label = new Label
{
Text = line,
TextColor = Color.Black
};
if (!gotTitle)
{
label.HorizontalOptions = LayoutOptions.Center;
label.FontSize = Device.GetNamedSize(NamedSize.Medium, label);
label.FontAttributes = FontAttributes.Bold;
mainStack.Children.Add(label);
gotTitle = true;
}
else
{
textStack.Children.Add(label);
}
}
}
}
ScrollView scrollView = new ScrollView
{
Content = textStack,
VerticalOptions = LayoutOptions.FillAndExpand,
Padding = new Thickness(5, 0),
};
mainStack.Children.Add(scrollView);
Content = mainStack;
BackgroundColor = Color.White;
Padding = new Thickness(0, 0, 0, 0);
}
}
}