2013-05-07 90 views
0

我正在尝试使用Xamarin的Mono stuff(MonoTouch和MonoDroid)构建应用程序。我正在尝试编写一些代码,允许我将内容放入本地存储中。来自Windows Phone背景,我认为这也是Xamarin世界推荐的方法。在Windows Phone,我会做以下几点:在Xamarin中使用IsolatedStorage

IsolatedStorageSettings clientStorage = IsolatedStorageSettings.ApplicationSettings; 
if (clientStorage != null) 
{ 
    if (clientStorage.Contains("myKey")) 
    clientStorage["myKey"] = value; 
    else 
    clientStorage.Add("myKey", value); 
    clientStorage.Save(); 
} 

虽然Xamarin堆栈提供System.IO.IsolatedStorage,它不提供IsolatedStorageSettings类。现在,我感觉卡住了,我找不到一个例子。我的问题是,如何在Mono应用程序中将独立存储的价值?

谢谢

回答

2

IsolatedStorageSettings is not implemented in the mobile solutions。它看起来可以添加,每个帖子,但它必须是一个自定义的实现。

一般来说,您还有其他几个选项。

  • 使用原生支持的机制。 Android:SharedPreferences (example)。 iOS:NSUserSettings (example)
  • 使用JSON或XML将设置存储在序列化对象中,然后在需要访问对象时反序列化对象。有 可能是一个很好的选择,但不建议。
  • 使用SQLite数据库来存储您的设置。这将是更多的 跨平台选项,可能是最推荐的选项。
0

官方单声道项目提供了IsolatedStorageSettings的实现。

你可以在:https://github.com/mono/moon/blob/master/class/System.Windows/System.IO.IsolatedStorage/IsolatedStorageSettings.cs。找到它。

只需将该文件添加到您的项目并添加“System.Runtime.Serialization”到您的参考。

该类在app目录中创建一个文件“__LocalSettings”并使用DataContractSerializer序列化您的设置。

+1

这将是很高兴看到这个答案更多的解释。 – 2013-08-08 17:42:07