我的内容页面中有一个工具栏,其中有一个名为add的项目,点击添加后我想打开DisplayActionSheet
我在ContentPage
中创建了Toolbar
xaml
,并在视图模型中将ICommand
附加到DisplayActionSheet
。现在<ContentPage.ToolbarItems>
<ToolbarItem Name="" Icon="ic_add.png" Order="Primary" Priority="0" Command="{Binding OnAddContactCommand}"/>
<ToolbarItem Name="" Icon="ic_search.png" Order="Primary" Priority="1" Command="{Binding OnContactSearchCommand}" />
</ContentPage.ToolbarItems>
只能在View中访问,因此我不确定如何访问它并从视图模型中呈现它。
xaml文件
public ICommand OnContactSearchCommand => new Command(OnContactSearch);
public ICommand OnAddContactCommand => new Command(OnAddContactSearch);
查看模型
private async void OnAddContactSearch()
{
//var action = await DisplayActionSheet(AppResources.select_contact_source, AppResources.cancel, null, AppResources.manual, AppResources.phonebook);
}
private void OnContactSearch()
{
Debug.WriteLine("OnContactSearch");
}
活动
$html->find("tr[class='zzz'], tr[class='sss']");
foreach($items as $post) {
echo $post->children(1)->children(0)->src;
}
答案 0 :(得分:1)
尝试
Application.Current.MainPage.DisplayActionSheet();
答案 1 :(得分:1)
与@Alessandro一样,Application.Current.MainPage
也适用于行动表和警报。为了隐藏视图模型中的视图特定内容,我创建了一个IMessageBoxService
,它被注入到需要它的视图模型的构造器中。请注意,我使用的是Autofac IoC container。对于Xamarin的DependencyService,您可以更改构造函数并在代码中查找服务。
<强> IMessageBoxService.cs 强>
public interface IMessageBoxService
{
void ShowAlert(string title, string message, Action onClosed = null);
// ...
Task<string> ShowActionSheet(string title, string cancel, string destruction, string[] buttons = null);
}
<强> MessageBoxService.cs 强>
public class MessageBoxService : IMessageBoxService
{
private static Page CurrentMainPage { get { return Application.Current.MainPage; } }
public async void ShowAlert(string title, string message, Action onClosed = null)
{
await CurrentMainPage.DisplayAlert(title, message, TextResources.ButtonOK);
onClosed?.Invoke();
}
public async Task<string> ShowActionSheet(string title, string cancel, string destruction = null, string[] buttons = null)
{
var displayButtons = buttons ?? new string[] { };
var action = await CurrentMainPage.DisplayActionSheet(title, cancel, destruction, displayButtons);
return action;
}
}
<强> AppSetup.cs 强>
protected void RegisterDependencies(ContainerBuilder cb)
{
// ...
cb.RegisterType<MessageBoxService>().As<IMessageBoxService>().SingleInstance();
}
<强>用法强>
public class EditProductViewModel : AddProductViewModel
{
private IMessageBoxService _messageBoxService;
public ICommand DeleteCommand { get; set; }
public EditProductViewModel(IPageNavigator navigator, IMessenger messenger,
IMessageBoxService messageBoxService, TagDataStore tagDataStore) : base(navigator, messenger, tagDataStore)
{
_messageBoxService = messageBoxService;
DeleteCommand = new Command(DeleteItem);
}
...
private async void DeleteItem()
{
var action = await _messageBoxService.ShowActionSheet(TextResources.MenuTitleDeleteProduct,
TextResources.ButtonCancel, TextResources.ButtonDelete);
if (action == TextResources.ButtonDelete)
{ } // delete
如果您正在进行viewmodel首次导航(s。Xamarin或Jonathan Yates' blog),您可以选择将此部分作为导航器服务。这是一个品味问题