【问题标题】:How to get free internal storage space using Xamarin IOS如何使用 Xamarin IOS 获得免费的内部存储空间
【发布时间】:2019-03-02 22:03:56
【问题描述】:

我可以使用 (NSProcessInfo.ProcessInfo.PhysicalMemory) 获取 RAM 详细信息。但我想使用 Xamarin IOS 获得免费的内部设备存储。

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 我没有尝试过..我为 SWIFT 或 Objective C 获得的所有代码。我正在使用 xamarin ios (C#)。所以我需要与此相关的代码。
  • 您是否尝试将objective-c代码转换为c#?
  • 我是 Objective -C 和 C# 的新手。我对此没有太多了解。该怎么做?

标签: ios xamarin xamarin.forms xamarin.ios xamarin-studio


【解决方案1】:

内部空闲空间的获取方法是这样的:

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

如果你使用Xamarin.Forms,可以制作自定义界面

namespace Your.Namespace.Interfaces
{
    public interface IStorageInterface
    {
        double GetFreeSpace(); //Not sure about the return type, try long, or double

    }
}

在您的 iOS 项目中:

[assembly: Xamarin.Forms.Dependency(typeof(Your.Namespace.iOS.StorageRenderer))]
namespace Your.Namespace.iOS
{
    public class StorageRenderer : IStorageInterface
    {
        public double GetFreeSpace()
        {
            return NSFileManager.DefaultManager.GetFileSystemAttributes (Environment.GetFolderPath (Environment.SpecialFolder.Personal)).FreeSize;
        }

    }
}

【讨论】:

    【解决方案2】:

    我已将您的代码转换为 Xamarin.iOS,如下所示:

    private ulong GetFreeDiskspace()
        {
            ulong totalSpace = 0;
            ulong totalFreeSpace = 0;
            NSError error = null;
            string[] paths = NSSearchPath.GetDirectories(NSSearchPathDirectory.UserDirectory, NSSearchPathDomain.All);
            var defManager = NSFileManager.DefaultManager;
            var dicAttributes = defManager.
                GetFileSystemAttributes(paths.First()
                , out error);
            totalSpace = dicAttributes.Size;
            totalFreeSpace = dicAttributes.FreeSize;
            return totalFreeSpace;
        } 
    

    祝你好运!

    如果查询恢复!

    【讨论】:

    • 感谢哈基姆的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 2020-11-03
    相关资源
    最近更新 更多