2017-08-25 78 views

回答

1

可以使用IMvxNavigationService來傳遞和返回對象。完整的文檔是:https://www.mvvmcross.com/documentation/fundamentals/navigation?scroll=26

在您的視圖模型,這可能是這樣的:

public class MyViewModel : MvxViewModel 
{ 
    private readonly IMvxNavigationService _navigationService; 
    public MyViewModel(IMvxNavigationService navigationService) 
    { 
     _navigationService = navigationService; 
    } 

    public override void Prepare() 
    { 
     //Do anything before navigating to the view 
    } 

    public async Task SomeMethod() 
    { 
     _navigationService.Navigate<NextViewModel, MyObject>(new MyObject()); 
    } 
} 

public class NextViewModel : MvxViewModel<MyObject> 
{ 
    public override void Prepare(MyObject parameter) 
    { 
     //Do anything before navigating to the view 
     //Save the parameter to a property if you want to use it later 
    } 

    public override async Task Initialize() 
    { 
     //Do heavy work and data loading here 
    } 
} 

使用IMvxMessenger沒有連接到網絡,你可以發送值:https://www.mvvmcross.com/documentation/plugins/messenger?scroll=1446

public class LocationViewModel 
    : MvxViewModel 
{ 
    private readonly MvxSubscriptionToken _token; 

    public LocationViewModel(IMvxMessenger messenger) 
    { 
     _token = messenger.Subscribe<LocationMessage>(OnLocationMessage); 
    } 

    private void OnLocationMessage(LocationMessage locationMessage) 
    { 
     Lat = locationMessage.Lat; 
     Lng = locationMessage.Lng; 
    } 

    // remainder of ViewModel 
}