简体   繁体   中英

Android.OS.Environment… which namespace?

I want to save some data to the storage (internal/external) of an Anroid device. I found this post where they got the external storage path with the following line:

var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.Path;

Seems fair. But my Visual Studio says the following:

The name 'Android' does not exist in the current context

So I've been wondering in which namespace does this Android reside? I tried quite a lot (eg AutoImport, but that seems to give me the wrong parent; or pressing F1 for documentation like suggested here ) but didnt figure out how to get this to work. Which NuGet- Package do I have to get to obtain this function. Or am I doing something completely wrong?

This is the namespace that AutoImport gives me:

Xamarin.Forms.PlatformConfiguration.Android.OS.Environment.ExternalStorageDirectory.Path;

but then it tells me that Android has no OS member. And upon checking, I found that Xamarin.Forms.PlatformConfiguration.Android has no members at all, so theres nothing beneath it. Is my Xamarin.Forms depricated? According to my NuGet- Packagemanager, everything is up to date.

These are the NuGet- Packages my project uses:

  1. Xamarin.Essentials
  2. Xamarin.Forms
  3. OxyPlot.Core
  4. OxyPlot.Xamarin.Forms

Just typing the answer here so we can close this question...

The problem with what you are trying to do is that you need to understand that there is a separation of concerns/abstraction. The Core project is referenced by the Android & iOS projects, but the Android & iOS projects are not referenced by the Core project. Schematically,

Core-->Android & Core-->iOS but Core<-x-Android, Core<-x-iOS

This is by design, and even though you can make it do what you are trying, you shouldn't.

We know that the iOS & Android SDKs are very different. So in order to get the path/file name, you have to perform different functions underneath. And the code that you shared is the correct way of retrieving the path/file name natively ONLY for Android.

In order to make it work for both iOS & Android, Xamarin Forms provides us with an abstraction layer that takes care of everything underneath for us, and allows us to use this:

string filePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); 
var fileName=Path.Combine(filePath,".");

There's also many other ways of doing this as shown in the official Microsoft docs here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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