Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms 我不能让文件处理工作,并已遵循所有的职位_Forms_Xamarin_File Io - Fatal编程技术网

Forms 我不能让文件处理工作,并已遵循所有的职位

Forms 我不能让文件处理工作,并已遵循所有的职位,forms,xamarin,file-io,Forms,Xamarin,File Io,我通过添加了一个文本文件,您可以从我的代码中看到,我实现了他们的代码。在运行时,它获取System.AggregateException,并显示消息“拒绝访问路径” 我读了以下帖子: 这是我的密码: using System.IO; using System.Reflection; using System.ComponentModel; using Xamarin.Forms; using System; using System.Threading.Tasks; namespace R

我通过添加了一个文本文件,您可以从我的代码中看到,我实现了他们的代码。在运行时,它获取System.AggregateException,并显示消息“拒绝访问路径”

我读了以下帖子:

这是我的密码:

using System.IO;
using System.Reflection;
using System.ComponentModel;

using Xamarin.Forms;
using System;
using System.Threading.Tasks;

namespace ReadTxt
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {


        public MainPage()
        {
            InitializeComponent();



                Label l = new Label()
                {
                    Text = ReadTextLine().Result
                };
                StackLayout s = new StackLayout();
                s.Children.Add(l);
            Content = new ScrollView { Content = s };

            async Task<string> ReadTextLine()
            {
                var backingFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "TextFile1.txt");
                using (var writer = File.CreateText(backingFile))
                {
                    await writer.WriteLineAsync("Updated text");
                }

                using (var reader = new StreamReader(backingFile, true))
                {
                    string line;
                    while ((line = await reader.ReadLineAsync()) != null)
                    {

                    }


                    return line;
                }
            }
        } } }
使用System.IO;
运用系统反思;
使用系统组件模型;
使用Xamarin.Forms;
使用制度;
使用System.Threading.Tasks;
名称空间ReadTxt
{
//了解有关使自定义代码在Xamarin.Forms预览器中可见的更多信息
//参观https://aka.ms/xamarinforms-previewer
[设计时间可见(错误)]
公共部分类主页:ContentPage
{
公共主页()
{
初始化组件();
标签l=新标签()
{
Text=ReadTextLine()。结果
};
StackLayout s=新的StackLayout();
s、 儿童。添加(l);
内容=新的滚动视图{Content=s};
异步任务ReadTextLine()
{
var backingFile=Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),“TextFile1.txt”);
使用(var writer=File.CreateText(backingFile))
{
等待writer.WriteLineAsync(“更新文本”);
}
使用(var reader=newstreamreader(backingFile,true))
{
弦线;
while((line=wait reader.readlinesync())!=null)
{
}
回流线;
}
}
} } }

您需要向Android.Manifest添加权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

检查这个答案。也许对你有帮助

一般来说,处理文件的更简单方法是在中使用这些方法,例如:

   public MainPage()
    {
        InitializeComponent();

        Label l = new Label()
        {
        };
        StackLayout s = new StackLayout();
        s.Children.Add(l);
        Content = new ScrollView { Content = s };

        var backingFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "TextFile1.txt");

        File.WriteAllText(backingFile, "updatedText");
        string text = File.ReadAllText(backingFile);

        l.Text = text;
    }
调试代码时,您似乎正在更新
子线程中的文本,这在
iOS
Android
中都是不允许的。另外,我认为从文件中读取文本时不需要while语句。我修改了您的代码,如下所示,它在我这边起作用:

public MainPage()
{
    InitializeComponent();

    Label l = new Label()
    {
    };
    StackLayout s = new StackLayout();
    s.Children.Add(l);
    Content = new ScrollView { Content = s };

    ReadTextLine();

    async void ReadTextLine()
    {
        var backingFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "TextFile1.txt");
        using (var writer = File.CreateText(backingFile))
        {
            await writer.WriteLineAsync("Updated text");
        }

        using (var reader = new StreamReader(backingFile, true))
        {
            string line;

            line = await reader.ReadLineAsync();

            Device.BeginInvokeOnMainThread(() => {
                l.Text = line;
            });
        }
    }
}
我没有添加权限,它可以在Android Q模拟器和iOS 13模拟器上运行。您可以尝试添加:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


要请求Android项目中的权限,正如他在回答中提到的那样。

您是在iOS还是Android上测试这个?我复制了您的代码,并在UWP上运行了它,但出现了相同的错误。下一步我将尝试ANdroid并报告。在ANdroid上工作。感谢you@RichardOsborne如果这个答案对你有帮助,你能帮我标记一下吗:)。