【问题标题】:Problems with removable storage Hololens 2, unity and xamarin uwp可移动存储 Hololens 2、unity 和 xamarin uwp 的问题
【发布时间】:2021-09-20 17:03:49
【问题描述】:

我对 Hololens 2 的软件开发相对较新,并且遇到了一个相当大的问题,我已经搞砸了很长时间,而且我的想法正在慢慢枯竭。

我的项目看起来像这样。我编写了一个 Unity 应用程序来捕获数据并将其存储在数据库 (sqlite) 中。 Xamarin.Forms UWP 应用程序应该从数据库中获取数据并使用它来绘制图表以实现更好的可视化。最大的问题是,两个应用程序都需要能够访问 Hololens 2 上的同一个数据库。我认为我可以是 U 盘上的数据库,两个应用程序都可以访问 U 盘。在 Xamarin 应用程序和 Unity 应用程序中启用了“可移动存储”。在 Xamarin 应用程序中,我在 Appmanifest 声明下使文件扩展名已知。我正在尝试使用以下命令建立连接:

namespace ARScoliosis.XamarinApp.UWP.Services
{
public class DatabasePath : IDatabasePath
{
    public async Task<string> GetLocalFilePath()
    {

        var messageDialog = new MessageDialog("No Usb connection has been found.");

        StorageFolder externalDevices = KnownFolders.RemovableDevices;

        if(externalDevices == null)
        {
            messageDialog.Commands.Add(new UICommand("No Devices", null));

        }

        StorageFolder usbStick = (await externalDevices.GetFoldersAsync()).FirstOrDefault(); <---According to debugging it stops here and jumps to optionsBuilder.UseSqlite($"Filename={databasePath}");
        
        if (usbStick == null)
        {
            messageDialog.Commands.Add(new UICommand("No UsbStick", null));
        }

        var usbStickFolder = await usbStick.CreateFolderAsync("DB", CreationCollisionOption.OpenIfExists);

        if (usbStickFolder == null)
        {
            messageDialog.Commands.Add(new UICommand("No Folder", null));
        }
    
        var file = await usbStickFolder.CreateFileAsync("Database.db", CreationCollisionOption.OpenIfExists);

        if(file == null)
        {
            messageDialog.Commands.Add(new UICommand("No File", null));
        }
        //var success = await Launcher.LaunchFileAsync(file);

        return file.ToString();
       
    }

我的 dbcontext 文件如下所示:

      namespace XamarinApp.Authentication
      {
      public partial class DBContext : DbContext
      {
      public DBContext()
    {     
       this.Database.EnsureCreated(); <--- Microsoft.Data.Sqlite.SqliteException: "SQLite Error 14: 'unable to open database file'."
       this.Database.Migrate();
    }

    public virtual DbSet<ItemData> ItemDatas { get; set; }

     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            var databasePath = DependencyService.Get<IDatabasePath>().GetLocalFilePath();
            optionsBuilder.UseSqlite($"Filename={databasePath}");
        }
    }

  namespace XamarinApp.Helpers
  {
     public interface IDatabasePath
    {
    Task<string> GetLocalFilePath();
    }
  }

不幸的是,我认为这段代码在 Hololens 上找不到 USB 棒。当我查看文件资源管理器时,我看到了包含所有数据的棒。在Unity App中也找不到棒子,虽然我这里用的是同样的代码,只是稍微修改了一下。

有谁知道我的错误在哪里,为什么我无法使用这两个应用程序中的任何一个访问 U 盘?有没有人尝试过类似的方法并且知道怎么做?

在此先感谢您的帮助。

非常感谢。

【问题讨论】:

    标签: c# unity3d hololens xamarin.uwp usb-flash-drive


    【解决方案1】:

    ****嗨 Hernando - MSFT,

    请原谅我迟到的回复。我不知何故忘记了。我找到了一种可以在 U 盘上找到我的数据库的方法。

    public static async Task<string> GetUsbStick()
        {
            StorageFolder UsbDrive = (await Windows.Storage.KnownFolders.RemovableDevices.GetFoldersAsync()).FirstOrDefault();  
    
            if (UsbDrive == null)
            {
                throw new InvalidOperationException("Usb Drive Not Found");
            }
            else
            {
                IReadOnlyList<StorageFile> FileList = await UsbDrive.GetFilesAsync();
                var Path = UsbDrive.Path.Replace('\\','/');
    
                foreach (StorageFile File in FileList)
                {
                   
                    var DBFound = File.Name.Contains("test.db"); 
    
                    if (DBFound == true)
                    {
                        return Path + File.Name;
                    }
                }
                throw new InvalidOperationException("DataBaseNotFound");
    
            }
    
        }
    

    在那里我得到了数据库输出的确切路径。只有不知何故没有带来任何东西。我无法在下一步中打开它。 “Sqlite 无法打开数据库”它说。

    public static async Task<int> Init()
        {
            try
            {
                DatabasePath = await GetUsbStick();
               
                StrConnDatabase = "Data Source" + "=" + DatabasePath + ";Mode=ReadWrite;";
    
            }
            catch (Exception io)
            {
                IsInit = false;
                throw new InvalidOperationException(io.Message);
            }
            finally
            {
                //Try to Open Database to Read
                using (var db = new Microsoft.Data.Sqlite.SqliteConnection(StrConnDatabase))
                {
                    try
                    {
                        db.Open(); //<- here it breaks off
                        db.Close();
                        IsInit = true;
                    }
                    catch (Exception io)
                    {
                       throw new InvalidOperationException(io.Message);
                    }
                }
            }
            return 1; // Succes 
    
        }
    

    这不起作用的原因可能是什么?

    有没有办法在应用程序中创建工作副本,然后将其写回 U 盘?

    【讨论】:

    • 关于 SQLite 的后续问题应该是它自己的问题,而不是隐藏在你的答案中。它不是 HoloLens 特定的,因此您可以查看一般的 UWP 答案。快速总结是 SQLite 使用的 API 无法访问应用安装和应用数据文件夹之外的文件。简单的解决方案是将数据库复制到应用程序的本地存储中以供使用。否则,您可以重建 SQLite 或重定向它的调用。见stackoverflow.com/questions/59135054/…
    【解决方案2】:

    HoloLens 不支持KnownFolders.RemovableDevices,有关详细信息,请参阅:Known folders。建议尝试File pickers手动选择一个文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-08
      • 1970-01-01
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      • 2018-01-21
      • 2020-12-21
      相关资源
      最近更新 更多