简体   繁体   English

尝试使用带有Xamarin Forms的StreamReader从文件读取时,值为“null”

[英]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: 我试过的事情:

  • I've used assembly.GetManifestResourceNames() to make sure I'm typing the resource ID correctly. 我使用了assembly.GetManifestResourceNames()来确保我正确输入资源ID。 I originally had it as "ereader.Texts.JP.txt" because the program itself is called cdern_lab10, and I thought that was the namespace. 我最初把它作为“ereader.Texts.JP.txt”,因为程序本身叫做cdern_lab10,我认为那是命名空间。 However, GetManifestResourceNames() returned "JP.Texts.JP.txt", so I changed it. 但是,GetManifestResourceNames()返回“JP.Texts.JP.txt”,所以我改了它。
  • 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". 它存储在名为“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);
        }
    }
}

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.

相关问题 尝试使用StreamReader从文本文件中读取数据时,继续获取“输入字符串格式不正确” - Keep getting “Input string not in correct format” when trying to read data from a text file using StreamReader Xamarin表单从文本文件读取结果是空的 - Xamarin Forms Read From Text File Result is Null 如何使用StreamReader从URI读取文件? - How to read a file from a URI using StreamReader? 使用StreamReader读取文件时读取固定数量的字节 - Read a fixed number of bytes when reading file using StreamReader 如何使用C#中的StreamReader从特定位置读取文件? - How to read file from specific position using StreamReader in C#? Xamarin.Forms无法从资源返回空读取文本文件 - Xamarin.Forms Can not Read Text file from Resources Returned Null 使用 StreamReader 的 Xamarin.Forms 的 System.ArgumentNullException - System.ArgumentNullException for Xamarin.Forms using StreamReader 如何在 Xamarin Forms 中从 StreamReader 填写表单 - How to fill out a form from StreamReader in Xamarin Forms 在Xamarin表单中将DataTemplate用于Listview时,CommandParameter提供空值 - CommandParameter giving null value when use DataTemplate for listview in xamarin forms 使用 streamreader 以正确的语法从文件中读取路径 - read a path from file in a correct syntax with streamreader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM