如何在MvvmCross中传递特定于平台的字符串(路径)

时间:2014-03-25 06:14:27

标签: c# mvvmcross

我有一个数据库可以存储在不同的地方,具体取决于平台。例如,对于Xamarin.Mac,数据库存储在@executable_path /../ Resources / my.db中。现在,ViewModel处理初始化数据库,我很乐意将其留在那里。但是,我需要传递一条路径,我所看到的只是

var startup = Mvx.Resolve<IMvxAppStart>();
startup.Start ();
AppDelegate中的

。在Mvx中我一直在使用RegisterSingleton和Resolve,但我不确定我是否应该将它用于一个简单的字符串(我应该有IMyDbPath和MyDbPath接口和类吗?)寻找一个优雅的解决方案。谢谢!

1 个答案:

答案 0 :(得分:1)

如果您正在使用MvvmCross-SQLite-Net插件。一旦注册,手动或通过Bootstrap,您将可以解决两个接口:

ISQLiteConnectionFactoryISQLiteConnectionFactoryEx

解决ISQLiteConnectionFactory后,您将能够呼叫Create(string address)将路径传递到您的数据库。您提供的路径Create不是特定于平台的,这意味着,Create将为您找出特定于平台的基本路径。

 private ISQLiteConnection CreateFileDb(SQLiteConnectionOptions options)
        {
            if (string.IsNullOrWhiteSpace(options.Address))
                throw new ArgumentException(Properties.Resources.CreateFileDbInvalidAddress);
            var path = options.BasePath ?? GetDefaultBasePath();
            string filePath = LocalPathCombine(path, options.Address);
            return CreateSQLiteConnection(filePath, options.StoreDateTimeAsTicks);
        }

因此,您可以在支持的平台上传递MyDatabase.dbdata/MyDatabase.db,它应该为您解析基本路径。