我是Xamarin和MvvmCross的新手,我想在AnotherViewModel的MainViewModel中调用一个方法。我真的不知道这样做的最佳方法。
我在Xamarin Chat中被告知创建一个服务,从Setup as Singleton运行它并在那里保存属性以从任何地方访问它。 另外,另一位用户告诉我,有许多方法可以在视图模型之间获取信息,但我不知道它们中的任何一种。
这就是我的工作方式。这种方式准确吗?或者我应该尝试别的吗?
谢谢!
MainViewModel.cs
using System;
using System.Threading.Tasks;
using Acr.UserDialogs;
using MvvmCross.Core.Navigation;
using MvvmCross.Core.ViewModels;
using App.Core.Services.Interfaces;
namespace App.Core.ViewModels
{
public class MainViewModel : MvxViewModel, ITestService
{
#region Fields
private readonly IMvxNavigationService _navigationService;
private readonly IUserDialogs _userDialogs;
private bool _isRunning;
#endregion Fields
#region Properties
private string _description;
public string Description
{
get { return _description; }
set { _description = value; RaisePropertyChanged(() => Description); }
}
#endregion Properties
#region Constructors
public MainViewModel(IMvxNavigationService navigationService, IUserDialogs userDialogs)
{
_navigationService = navigationService;
_userDialogs = userDialogs;
}
#endregion Constructors
#region Public Methods
// Implemented Methods
public async Task<Boolean> UpdateDescription(String description)
{
Description = description;
// Do more stuff
return true;
}
#endregion Public Methods
#region Private Methods
private void SelectItem(Item item)
{
_navigationService.Navigate<AnotherViewModel, AnotherViewModelParameters>(new AnotherViewModelParameters()
{
SelectedItem = item,
Service = this
});
}
#endregion Private Methods
}
}
AnotherViewModel.cs
using Acr.UserDialogs;
using MvvmCross.Core.Navigation;
using MvvmCross.Core.ViewModels;
using App.Core.Services.Interfaces;
namespace App.Core.ViewModels
{
public class AnoterViewModel : MvxViewModel<AnotherViewModelParameters>
{
#region Fields
private ITestService _testService;
private readonly IMvxNavigationService _navigationService;
private readonly IUserDialogs _userDialogs;
#endregion Fields
#region Properties
// Class Properties
private Item _item;
public Item Item
{
get { return _item; }
set { _item = value; RaisePropertyChanged(() => Item); }
}
// MVVM Commands
public IMvxCommand UpdateDescriptionCommand { get; private set; }
#endregion Properties
#region Constructors
public AnotherViewModel(IMvxNavigationService navigationService, IUserDialogs userDialogs)
{
_navigationService = navigationService;
_userDialogs = userDialogs;
UpdateDescriptionCommand = new MvxCommand(UpdateDescription);
}
#endregion Constructors
#region Public Methods
public override void Prepare(AnotherViewModelParameters parameter)
{
_item = parameter.SelectedItem;
_testService = parameter.Service;
}
#endregion Public Methods
#region Private Methods
private async void UpdateDescription()
{
var response = await _testService.UpdateDescription(_item.Description);
if(response)
{
await _navigationService.Close(this);
}
}
#endregion Private Methods
}
}
ITestService.cs
using System;
using System.Threading.Tasks;
namespace App.Core.Services.Interfaces
{
public interface ITestService
{
Task<Boolean> UpdateDescription(String description);
}
}