问题描述
我对 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中,也没有找到stick,虽然我这里使用的代码相同,只是稍作修改。
有谁知道我的错误在哪里,为什么我无法使用两个应用程序中的任何一个访问 USB 记忆棒?有没有人尝试过类似的东西并且知道怎么做?
我想提前感谢您的帮助。
非常感谢。
解决方法
KnownFolders.RemovableDevices 在 HoloLens 上不受支持,有关详细信息,请参阅:Known folders。建议尝试在 File pickers 手动选择一个文件。
,****嗨 Hernando - MSFT,
请原谅我迟到的回复。我不知何故忘记了。我找到了一种可以在 U 盘上找到我的数据库的方法。
var def = this._rpc({
model: 'res.partner',method: 'search_read',domain: [
['id','=',this.recordData.partner_id.data.id]
],fields: ['id','vat'],}).then(function (result) {
partner_data = result[0];
});
在那里我得到了数据库输出的确切路径。只是不知何故不会带来任何东西。我无法在下一步中打开它。 “Sqlite 无法打开数据库”它说。
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");
}
}
这不起作用的原因是什么?
有没有办法在应用程序中创建一个工作副本,然后将其写回 U 盘?