简体   繁体   English

可移动存储 Hololens 2、unity 和 xamarin uwp 的问题

[英]Problems with removable storage Hololens 2, unity and xamarin uwp

I'm relatively new to software development for the Hololens 2 and have a pretty big problem I've been messing around with for a long time and I'm slowly running out of ideas.我对 Hololens 2 的软件开发相对较新,并且有一个相当大的问题,我已经困扰了很长时间,而且我的想法正在慢慢耗尽。

My project looks like this.我的项目看起来像这样。 I've written a Unity application to capture data and store it in a database (sqlite).我编写了一个 Unity 应用程序来捕获数据并将其存储在数据库 (sqlite) 中。 A Xamarin.Forms UWP application is supposed to take the data from the database and use it to paint charts for better visualisation. Xamarin.Forms UWP 应用程序应该从数据库中获取数据并使用它来绘制图表以获得更好的可视化效果。 The big problem is, both apps need to be able to access the same database on the Hololens 2. I thought that I could be the database on a usb stick and both apps could access the usb stick.最大的问题是,两个应用程序都需要能够访问 Hololens 2 上的同一个数据库。我认为我可以成为 U 盘上的数据库,两个应用程序都可以访问 U 盘。 In the Xamarin app and in the Unity app "removable storage" is enabled.在 Xamarin 应用程序和 Unity 应用程序中,“可移动存储”已启用。 In the Xamarin App I have made the file extension known under Appmanifest declaration.在 Xamarin 应用程序中,我已经在 Appmanifest 声明下公开了文件扩展名。 I am trying to get the connection with the following commands:我正在尝试使用以下命令建立连接:

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();
       
    }

My dbcontext file looks something like this:我的 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();
    }
  }

Unfortunately this code does not find the Usb stick on the Hololens, i think.不幸的是,我认为这段代码在 Hololens 上找不到 Usb 棒。 When I look in file explorer, I see the stick with all its data.当我查看文件资源管理器时,我看到了所有数据的棒。 In the Unity App, the stick is also not found, although I use the same code here, only slightly modified.在Unity App中,也没有找到stick,虽然我在这里使用了相同的代码,只是稍作修改。

Does anyone know where my error lies, why I can't access the USB stick with either of the two apps?有谁知道我的错误在哪里,为什么我无法使用两个应用程序中的任何一个访问 USB 记忆棒? Has anyone tried something similar and knows how to do it?有没有人尝试过类似的东西并且知道如何去做?

i would like to thank you in advance for your help.我想提前感谢您的帮助。

Thank you very much.非常感谢你。

KnownFolders.RemovableDevices doesn't be supported on the HoloLens, for more information please see: Known folders . HoloLens 不支持KnownFolders.RemovableDevices,有关详细信息,请参阅: 已知文件夹 It is recommended to take a try at File pickers to pick one file manually.建议尝试使用文件选择器手动选择一个文件。

****Hi Hernando - MSFT, ****嗨 Hernando - MSFT,

Please excuse my late reply.请原谅我迟到的回复。 i had somehow forgotten.我不知何故忘记了。 I have found a way where I can find my database on the usb stick.我找到了一种方法,可以在 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");

        }

    }

There I get the exact path for the database output.在那里我得到了数据库输出的确切路径。 Only that somehow brings nothing.只是不知何故不会带来任何东西。 I cannot open it in the next step.我无法在下一步中打开它。 "Sqlite cant open database" it says. “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 

    }

What could be the reason that this does not work?这不起作用的原因是什么?

is there a way to create a working copy within the app, which is then written back to the usb stick?有没有办法在应用程序中创建一个工作副本,然后将其写回 U 盘?

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

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