Android.OS.Environment ...哪个名称空间? [英] Android.OS.Environment... which namespace?

查看:201
本文介绍了Android.OS.Environment ...哪个名称空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些数据保存到Anroid设备的存储(内部/外部).我发现了这篇文章,其中他们获得了带有以下行的外部存储路径:

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

似乎很公平.但是我的Visual Studio会说以下话:

名称"Android"在当前上下文中不存在

所以我一直在想这个Android驻留在哪个名称空间中?我做了很多尝试(例如AutoImport,但这似乎给了我错误的父母;或者按F1键显示建议的文档解决方案

只需在此处输入答案,我们就可以结束此问题...

您要执行的操作存在的问题是,您需要了解关注点/摘要是分开的.核心项目由Android& iOS项目,但Android& Core项目未引用iOS项目.

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

这是设计使然,即使您可以使其执行您尝试的操作,也不应这样做.

我们知道iOS& Android SDK非常不同.因此,为了获取路径/文件名,您必须在下面执行不同的功能.而且,您共享的代码是本地唯一检索路径/文件名的正确方法,仅适用于Android.

为了使其适用于iOS&在Android上,Xamarin Forms为我们提供了一个抽象层,该层负责我们下面的所有事务,并允许我们使用此层:

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

还有许多其他方法可以执行此操作,如官方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 (e.g. 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.

这篇关于Android.OS.Environment ...哪个名称空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆