2014-01-13 69 views
0

我試圖使用fileplugin和JSON序列爲此保留程序設置。 我在覈心項目中有以下DcsSetup類。 現在我正在與機器人合作。我似乎無法保存文件。 json序列化是可以的。 WriteFile似乎沒問題,但下次嘗試使用TryReadTextFile讀取文件時,它失敗。 我找不到設備上的文件,所以我認爲WriteFile的東西是錯誤的。 在Android上保存和閱讀我的Settings類的正確方法是什麼?mvvmcross使用文件的插件和JSON序列化/反序列化

公共類DcsSetup {

public class Settings 
{ 
    //Server 
    public string Server; 
    public int Port; 

    public int Device; 

    public string EncodingFromClient; 
    public string EncodingToClient; 

    public int FontCorrectionPixelsWidth; //Pixels to add or subtract i Y dimension to get the right font size 
    public int FontCorrectionPixelsHeight; //Pixels to add or subtract i Y dimension to get the right font size 

    public float XPct;//Pct to add to vertical placement of textBox and Buttons. 
    public float YPct;//Pct to add to horisontal placement of textBox and Buttons. 
    public float SizePct;//Pct to add to horisontal size of textBox and Buttons. 

    public bool FullScreen; 
    public bool DontSleep; 


    //Diverse 
    public bool AutoSendEnter; 
} 
public Settings Setting; 

public DcsSetup() 
{ 
    var setupFound=true; 
    var fileService = Mvx.Resolve<IMvxFileStore>(); 
    var jsonConvert = Mvx.Resolve<IMvxJsonConverter>(); 
    var path = fileService.PathCombine("Setting", "Settings.txt"); 

    Setting = new Settings(); 

    try { 
    string settingFile; 
    if (fileService.TryReadTextFile(path, out settingFile)){ 
     Setting = jsonConvert.DeserializeObject<Settings>(settingFile); 
    } else{ 
     setupFound = false; 
    } 
    } 
    catch(Exception e) { 
    AppTrace.Error("Failed to read settings: {0}", e.Message); 
    setupFound=false; 
    } 
    if(setupFound==false){ 
     Setting.Server = "192.168.1.100"; 
     Setting.Port = 1650; 

     Setting.Device = 1; 

     Setting.EncodingFromClient = "CP1252"; 
     Setting.EncodingToClient = "CP1252"; 

     Setting.FontCorrectionPixelsWidth = 0; 
     Setting.FontCorrectionPixelsHeight = 0; 

     Setting.XPct = 97.0f; 
     Setting.YPct = 100.0f; 
     Setting.SizePct = 98.0f; 

     Setting.FullScreen = false; 
     Setting.DontSleep = true; 
     Setting.AutoSendEnter = true; 

     try { 
      //json 
      var json = jsonConvert.SerializeObject(Setting); 
      fileService.EnsureFolderExists("Setting"); 

      fileService.WriteFile(path, json); 
     } 
     catch (Exception e) { 
      AppTrace.Error("Failed to save settings: {0}", e.Message); 
     } 
    } 
} 

}}

回答

1

我剛剛創建使用3.1.1-β2包MvvmCross

然後我添加的文件在VS2012的項目,並Json插件包

我將核心FirstViewModel更改爲:

public class FirstViewModel 
    : MvxViewModel 
{ 
    private readonly IMvxFileStore _fileStore; 
    private readonly IMvxJsonConverter _jsonConverter; 
    private readonly string _filePath; 

    public class ToStore 
    { 
     public string Foo { get; set; } 
    } 

    public ICommand SaveCommand 
    { 
     get 
     { 
      return new MvxCommand(() => 
       { 
        var toStore = new ToStore() {Foo = Hello}; 
        var json = _jsonConverter.SerializeObject(toStore); 
        _fileStore.WriteFile(_filePath, json); 
       }); 
     } 
    } 

    public ICommand LoadCommand 
    { 
     get 
     { 
      return new MvxCommand(() => 
       { 
        string txt; 
        if (_fileStore.TryReadTextFile(_filePath, out txt)) 
        { 
         Mvx.Trace("Loaded {0}", txt); 
         var stored = _jsonConverter.DeserializeObject<ToStore>(txt); 
         Hello = stored.Foo; 
        } 
       }); 
     } 
    } 

    private string _hello = "Hello MvvmCross"; 

    public FirstViewModel(IMvxFileStore fileStore, IMvxJsonConverter jsonConverter) 
    { 
     _fileStore = fileStore; 
     _jsonConverter = jsonConverter; 
     _filePath = _fileStore.PathCombine("SubDir", "MyFile.txt"); 
     _fileStore.EnsureFolderExists("SubDir"); 
    } 

    public string Hello 
    { 
     get { return _hello; } 
     set { _hello = value; RaisePropertyChanged(() => Hello); } 
    } 
} 

我稱這種從測試的Android UI:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <EditText 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="40dp" 
    local:MvxBind="Text Hello" 
    /> 
    <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="40dp" 
    local:MvxBind="Text Hello" 
    /> 
    <Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="40dp" 
    android:text="Load" 
    local:MvxBind="Click LoadCommand" 
    /> 
    <Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="40dp" 
    android:text="Save" 
    local:MvxBind="Click SaveCommand" 
    /> 
</LinearLayout> 

這似乎工作確定 - 它救了內部和測試運行之間加載的JSON罰款。

基於此,我唯一的猜測是您是否在兩次運行之間重新部署應用程序 - 如果您這樣做,並且您沒有選中MonoDroid的Preserve application data/cache on device between deploys,那麼您將看不到保存的設置。

+0

感謝您的快速回答。這是我必須在Visual Studio中設置的Preserve應用程序數據。工具\選項\ Xamarin \ Android設置。 – user2831196