[英]Value is “null” when trying to read from a file using StreamReader with Xamarin Forms
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. 所以我试图让我的代码从文件中读取文本并将其显示在scrollView中,顶部有一个固定标签。 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.
我从Xamarin网站下载的“使用Xamarin创建移动应用程序”一书中的示例逐字复制了文本。 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.
使用Visual Studio的调试器,我进入代码并意识到,由于某种原因,变量“stream”没有被赋值 - 根据调试器,它的值为null,这导致“值不能为null “例外。 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.
这只是我试图用Xamarin Forms制作的第二个代码,使用这本书作为模板,我只是难以理解为什么“流”没有赋值。 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). 如果它有助于查看我正在引用的代码,可以在这里可以从Microsoft下载的书中找到,PDF格式的第105-106页(或者如果您要阅读本书的第84-85页)页码)。
Things I've tried: 我试过的事情:
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);
}
}
}
You can try something like this: 你可以尝试这样的事情:
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(TextPage)).Assembly;
Stream stream = assembly.GetManifestResourceStream("JP.Texts.JP.txt");
string text = "";
using (var reader = new System.IO.StreamReader (stream)) {
text = reader.ReadToEnd ();
}
Debug.WriteLine(text);
Delete your bin and obj folders and rebuild. 删除bin和obj文件夹并重建。
Here is a link to docs with handling embedded resources: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/files?tabs=vsmac 以下是处理嵌入式资源的文档链接: https : //docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/files?tabs = vsmac
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.