简体   繁体   English

我可以在 Xamarin Forms 上检查设备中的可用存储吗?

[英]Can I check available storage in the Device on Xamarin Forms?

I am trying to make an app that requires that I frequently send recorded data to a firebase.我正在尝试制作一个需要我经常将记录的数据发送到 firebase 的应用程序。 When network goes out or battery is about to die, I am saving all of the data that wasn't stored into firebase locally.当网络中断或电池即将耗尽时,我将所有未存储到 firebase 的数据保存在本地。 However, to do this I require about 20 MB of data (my data is pretty big).但是,要做到这一点,我需要大约 20 MB 的数据(我的数据非常大)。 That being said, I want to find a way to check the available storage (and the battery if possible) to give the user a warning.话虽如此,我想找到一种方法来检查可用的存储空间(如果可能的话,还要检查电池)给用户一个警告。 Is this possible using Xamarin Forms?这可以使用 Xamarin Forms 吗? Also, is there a way that I can make this solution universal (ie I don't need a separate piece of code for iOS vs. Android)?另外,有没有一种方法可以使这个解决方案变得通用(即我不需要为 iOS 与 Android 编写单独的代码)? (I am new to Xamarin and C#) (我是 Xamarin 和 C# 的新手)

Use the following code to check the free size of a device:使用以下代码检查设备的可用大小:

For iOS:对于 iOS:

NSFileManager.DefaultManager.GetFileSystemAttributes (Environment.GetFolderPath (Environment.SpecialFolder.Personal)).FreeSize;

For Android:对于 Android:

var freeExternalStorage = Android.OS.Environment.ExternalStorageDirectory.UsableSpace;

There is no universal option, but you can wrap it an interface like so and implement it on each project:没有通用选项,但您可以将它包装成这样的接口并在每个项目中实现它:

public interface IStorage
{
    double GetRemainingStorage();
}

It would be great if a feature like this could be added in Xamarin.Essentials - there was a request on GitHub but it never really made it.如果可以在 Xamarin.Essentials 中添加这样的功能,那就太好了——在 GitHub 上有一个请求,但它从未真正实现过。

If there are any issues with the code - please tell me.如果代码有任何问题 - 请告诉我。

Hope this helped,希望这有帮助,

Instructions Create an interface in your shared project titled IStorage .说明在您的共享项目中创建一个名为IStorage的接口。

In your Android project create a class titled AndroidStorageManager (you can name it however you would like).在您的 Android 项目中,创建一个名为AndroidStorageManager的 class(您可以随意命名)。 Make it so it extends IStorage .使它扩展IStorage The method GetRemainingStorage() should be of return type double . GetRemainingStorage()方法的返回类型应该是double

在此处输入图像描述

 public class AndroidStorageManager : IStorage
    {
        public double GetRemainingStorage()
        {
            var freeExternalStorage = Android.OS.Environment.ExternalStorageDirectory.UsableSpace;

            return freeExternalStorage;
        }
    }

For iOS Create a class in your iOS project titled iOSStorageManager which extends IStorage :对于 iOS在您的 iOS 项目中创建一个 class ,标题为iOSStorageManager扩展IStorage

 public class iOSStorageManager : IStorage
    {
        public double GetRemainingStorage()
        {
            return NSFileManager.DefaultManager.GetFileSystemAttributes(Environment.GetFolderPath(Environment.SpecialFolder.Personal)).FreeSize;
        }
    }

In your Android implementation - add the following code above the namespace:在您的 Android 实现中 - 在命名空间上方添加以下代码:

[assembly: Xamarin.Forms.Dependency(
          typeof(AndroidStorageManager))]

For iOS:对于 iOS:

[assembly: Xamarin.Forms.Dependency(
          typeof(iOSStorageManager))]

To get the storage:获取存储空间:

 IStorage storageManager = DependencyService.Get<IStorage>();
            double remaining = storageManager.GetRemainingStorage();

Hope this clarified things.希望这能澄清事情。

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

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