[英]Problems with removable storage Hololens 2, unity and xamarin uwp
我對 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 記憶棒? 有沒有人嘗試過類似的東西並且知道如何去做?
我想提前感謝您的幫助。
非常感謝你。
****嗨 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 盤?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.