繁体   English   中英

WinPhone上SQLiteDB(或副本)的路径错误。 Xamarin.Forms PCL

[英]Wrong path to SQLiteDB (or copy) on WinPhone. Xamarin.Forms PCL

为PCL的Xamarin.Forms安装了NUGet软件包。 我有4个Droid,iOS,Win 8.1和WinPhone 8.1项目。 试图连接我的数据库,但是遇到了麻烦:我的Win和WinPhone项目看不到它,或者返回了错误的路径。 关注了官方Xamarin.Forms论坛。

接口:

public interface ISQLite
{
    string GetDatabasePath(string filename);
}

WInPhone类别:

using System.IO;
using Windows.Storage;
using Baumizer.WinPhone;
using Xamarin.Forms;

[assembly: Dependency(typeof(SQLite_WinPhone))]
namespace Baumizer.WinPhone
{
    public class SQLite_WinPhone:ISQLite
{
    public SQLite_WinPhone() { }
    public string GetDatabasePath(string filename)
    {
        return Path.Combine(ApplicationData.Current.LocalFolder.Path, filename);
    }
}}

此类用于选择信息:

public class DataBase
{
    SQLiteConnection connection;

    public DataBase()
    {
        connection= new SQLiteConnection(DependencyService.Get<ISQLite>().GetDatabasePath("Database.db"));
    }

    public IEnumerable<Group> GetGroups()
    {
        return connection.Query<Group>("SELECT * FROM [Scedule] WHERE [facultyName] =" + "'" + Data.CurrentFaculty + "'");
    }
}

它在Android上运行良好。 在WinPhone上,我得到了SQLite的例外- 没有这样的表:Scedule 我打开了db所在的仿真器的本地目录-0Kb。 我把db放到Assets并将BuildInAction设置为Content 怎么了 需要帮忙

在论坛上找到此代码, 并将其放在WinPhone App.cs中的OnLaunched(...)上:

if(await ApplicationData.Current.LocalFolder.GetItemAsync(Data.DataBaseName) == null)
        {
            StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync($"Assets\\{Data.DataBaseName}");
            await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);
        }

如果数据库不存在,它将复制数据库,但它确实存在。 可能是我需要删除DB 0Kb ,但是我不知道该怎么做。

在工作,OnLaunched():

try
        {
            await ApplicationData.Current.LocalFolder.GetItemAsync("Scedule.db");
        }
        catch (System.IO.FileNotFoundException)
        {
            StorageFile databaseFile =
                    await Package.Current.InstalledLocation.GetFileAsync($"Assets\\{"Scedule.db"}");
            await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);
        }

数据库复制不正确。 可能还有其他方法。

暂无
暂无

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

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