我有一个SlideListViewModel:
public class SlideListViewModel : MvxViewModel
{
private readonly IServerClient _serverClient;
private ICollection<SlideListItemViewModel> _slides;
private IMapperService _mapper;
public ICollection<SlideListItemViewModel> Slides { get => _slides; set => SetProperty(ref _slides, value); }
public SlideListViewModel(IServerClient serverClient, IMapperService mapper)
{
_serverClient = serverClient;
_mapper = mapper;
}
public override async Task Initialize()
{
await base.Initialize();
Slides = _mapper.Map<ICollection<SlideListItemViewModel>>(await _serverClient.GetSlideList());
}
}
我有一个SlideListItemViewModel:
public class SlideListItemViewModel : MvxViewModel
{
private long _id;
private string _title;
public long Id { get => _id; set => SetProperty(ref _id, value); }
public string Title { get => _title; set => SetProperty(ref _title, value); }
public IMvxCommand DeleteCommand => new MvxCommand<IServerClient>(DeleteCommandHandler);
public async void DeleteCommandHandler(IServerClient serverClient)
{
var delete = await UserDialogs.Instance.ConfirmAsync(new ConfirmConfig
{
Title = "Delete slide",
Message = "Are you sure you want to delete this slide?",
OkText = "Yes",
CancelText = "No"
});
if (!delete)
return;
//Here I should user my serverClient
}
}
是否可以从_serverClient
传递命令接口SlideListViewModel
?我尝试在slidelistitemviewmodel.axml中绑定,但没有发生任何事情......
<Button
local:MvxBind="Click DeleteCommand, CommandParameter=_serverClient"
style="@style/ButtonSlide"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Delete" />
有人可以帮助我吗???
答案 0 :(得分:1)
您需要在SlideListItemViewModel中提供它。 SlideListItemViewModel.xaml无权访问SlideListViewModel。 不要把它变成命令参数,只需在创建项目时从SlideListViewModel传入它。在SlideListItemViewModel.cs中添加属性:
public class SlideListItemViewModel : MvxViewModel
{
private long _id;
private string _title;
private IServerClient _serverClient;
public long Id { get => _id; set => SetProperty(ref _id, value); }
public string Title { get => _title; set => SetProperty(ref _title, value); }
public IServerClient ServerClient {get => _serverClient; set => SetProperty(ref _serverClient, value; }
public IMvxCommand DeleteCommand => new MvxCommand(DeleteCommandHandler);
public async void DeleteCommandHandler()
{
var delete = await UserDialogs.Instance.ConfirmAsync(new ConfirmConfig
{
Title = "Delete slide",
Message = "Are you sure you want to delete this slide?",
OkText = "Yes",
CancelText = "No"
});
if (!delete)
return;
//Here I should user my serverClient
}
}
在SlideListViewModel.cs中创建时传递它:
public override async Task Initialize()
{
await base.Initialize();
Slides = _mapper.Map<ICollection<SlideListItemViewModel>>(await _serverClient.GetSlideList());
foreach(var slide in Slides)
{
slide.ServerClient = _serverClient;
}
}
并将其从SlideListItemViewModel.xaml:
中的绑定中取出local:MvxBind="Click DeleteCommand"