據我能找到的資料,MvvmCross視圖模型的生命週期是MvvmCross視圖模型生命週期
建築 - 使用IoC的依賴注入
的init() - 導航參數
ReloadState初始化( ) - 立碑後補液
開始() - 初始化和補液完成時調用
我已經實現了我的如下:
public async Task Init(Guid ID)
{
await MPS_Mobile_Driver.Droid.DataModel.ShipmentDataSource.GetShipmentInventory(ID);
ShipmentInventory = ShipmentDataSource.CurrInventory;
ShipmentLots = await MPS_Mobile_Driver.Droid.DataModel.ShipmentDataSource.GetShipmentLotList((int)ShipmentInventory.idno, (short)ShipmentInventory.idsub);
Inv_DamageList = await ListDataSource.GetInv_Damage();
}
protected override void SaveStateToBundle(IMvxBundle bundle)
{
base.SaveStateToBundle(bundle);
bundle.Data["ShipmentInventory"] = StringSerializer.SerializeObject(ShipmentInventory);
bundle.Data["ShipmentLots"] = StringSerializer.SerializeObject(ShipmentLots);
bundle.Data["Inv_DamageList"] = StringSerializer.SerializeObject(Inv_DamageList);
}
protected override void ReloadFromBundle(IMvxBundle state)
{
base.ReloadFromBundle(state);
ShipmentInventory = StringSerializer.DeserializeObject<ShipmentInventory>(state.Data["ShipmentInventory"]);
ShipmentLots = StringSerializer.DeserializeObject<ShipmentLotList>(state.Data["ShipmentLots"]);
Inv_DamageList = StringSerializer.DeserializeObject<Inv_DamageList>(state.Data["Inv_DamageList"]);
state.Data.Clear();
}
首先,我似乎無法得到模擬器實際破壞活動時,我打的Home鍵,即使我有一個選項被選中。無論如何,這個活動似乎都在背景中。
這就是說,當我擊中主鍵時,它調用SaveStateToBundle。它永遠不會做的就是調用ReloadFromBundle。這使得很難測試實際正在發生的事情。我的擔心是,根據生命週期,如果應用程序確實從邏輯刪除中恢復,它會在調用ReloadFromBundle之前調用Init。由於Init是將參數傳遞給ViewModel的首選方法,因此它應該使用該參數來初始化數據。如果發生這種情況,它會調用ReloadFromBundle,它會覆蓋在Init中初始化的數據。這很好,但效率很低。有沒有一種方法可以在Init中知道ViewModel是由於新導航還是從墓碑中恢復而創建的?
感謝您的任何幫助。
吉姆