简体   繁体   English

Xamarin.Forms Android 不包含 Content 的定义

[英]Xamarin.Forms Android doesn't contain a definition for Content

I am trying to create/Open/Write a .txt file in the External Storage, but I can not set the path with " Android.Content.Context.GetExternalFilesDir ".我正在尝试在外部存储中创建/打开/写入一个 .txt 文件,但我无法使用“ Android.Content.Context.GetExternalFilesDir ”设置路径。

Here is my exact code:这是我的确切代码:

string sourceFile = Path.Combine(Android.Content.Context.GetExternalFilesDir(null).AbsolutePath, "myFile.txt");
File.Create(sourceFile);

The error message is:错误信息是:

Android doesn't contain a definition for Content Android 不包含 Content 的定义

What should I do?我应该怎么办?
I don't know how to modify the assembly我不知道如何修改程序集

Of course, if you create a Xamarin.Android project, you can use the android api easily in it.当然,如果您创建一个 Xamarin.Android 项目,您可以在其中轻松使用 android api。

In addition, you can also call the platform method with the dependency service.此外,您还可以使用依赖服务调用平台方法。 You can check the following link which shows how to use it to operate the android folder.您可以查看以下链接,该链接显示了如何使用它来操作 android 文件夹。

Link: Xamarin forms: How to create folder and a file in device external storage?链接: Xamarin 表单:如何在设备外部存储中创建文件夹和文件?

And then you need to add the write and read the external storage permission in the AndroidManifest.xml:然后你需要在AndroidManifest.xml中添加读写外部存储权限:

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

And grant the app the two permission with the permission request.并通过权限请求授予应用程序两个权限。 You can check the following case.您可以检查以下情况。

Case: Can't write to Android External storage even with permissions set案例: 即使设置了权限也无法写入 Android 外部存储

In addition, you need to put the SaveFile(string fileName) method into the AccessFileImplement class.此外,您需要将SaveFile(string fileName)方法放入 AccessFileImplement 类中。

I am still on the same point, here is what I did: in the Android Project , I created a class "Storage" ( Storage.cs ) with this function:我仍然在同一点上,这就是我所做的:在Android Project中,我使用此功能创建了一个类“Storage”( Storage.cs ):

 public void SaveFile(string fileName)
    {
        string path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
        string filePath = System.IO.Path.Combine(path, fileName);
        if (!System.IO.File.Exists(filePath))
        {
            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath, true))
            {
                writer.WriteLine(DateTime.Now.ToString() + ": " + "Example .txt created using Xamarin.");
            }
        }
        else
        {
            System.IO.File.Delete(filePath);
            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath, true))
            {
                writer.WriteLine(DateTime.Now.ToString() + ": " + "Example .txt created using Xamarin.");
            }
        }
    }

Then in the Forms Project , in MainPage.xaml.cs I added an interface with this code:然后在Forms ProjectMainPage.xaml.cs 中,我添加了一个带有以下代码的接口:

public interface IAccessFile
    {
        void SaveFile(string fileName);
    }

And then in the same item ( MainPage.xaml.cs ) in the main code, I added:然后在主代码的同一个项目( MainPage.xaml.cs )中,我添加了:

DependencyService.Get<IAccessFile>().SaveFile("workingHours.txt");

Also in AndroidManifest.xml , I added the permission:同样在AndroidManifest.xml中,我添加了权限:

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

The only code I did not add in the Android Project in my class Storage (before the function) is:我没有在我的类 Storage 中的 Android 项目中添加的唯一代码(在函数之前)是:

[assembly: Xamarin.Forms.Dependency(typeof(AccessFileImplement))]
namespace XamarinFirebase.Droid
{
    public class AccessFileImplement : IAccessFile

Because it doesn't work even with the using因为即使使用 using 也不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM