从视图模型访问并打开 DisplayActionSheet

Hun*_*unt 3 mvvm xamarin xamarin.forms

我的内容页面中有一个工具栏,其中有一个名为 add 的项目,单击添加我想打开 DisplayActionSheet

我已经建立ContentPage Toolbarxaml和连接ICommand到它在视图模型。现在DisplayActionSheet只能在视图中访问,因此我不确定如何访问它并从视图模型中呈现它。

xaml 文件

<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>
Run Code Online (Sandbox Code Playgroud)

查看模型

public ICommand OnContactSearchCommand => new Command(OnContactSearch);
public ICommand OnAddContactCommand => new Command(OnAddContactSearch);
Run Code Online (Sandbox Code Playgroud)

事件

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");
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*aro 6

try with

Application.Current.MainPage.DisplayActionSheet();
Run Code Online (Sandbox Code Playgroud)


Kay*_*Kay 6

就像@Alessandro 所说的那样,也Application.Current.MainPage适用于行动表和警报。为了从视图模型中隐藏视图特定的东西,我创建了一个IMessageBoxService注入到需要它的视图模型的构造函数中。请注意,我使用的是Autofac IoC 容器。对于 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);
}
Run Code Online (Sandbox Code Playgroud)

消息框服务.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;
    }
}
Run Code Online (Sandbox Code Playgroud)

AppSetup.cs

    protected void RegisterDependencies(ContainerBuilder cb)
    {
        // ...
        cb.RegisterType<MessageBoxService>().As<IMessageBoxService>().SingleInstance();
    }
Run Code Online (Sandbox Code Playgroud)

用法

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);
    }
Run Code Online (Sandbox Code Playgroud)

...

    private async void DeleteItem()
    {
        var action = await _messageBoxService.ShowActionSheet(TextResources.MenuTitleDeleteProduct,
            TextResources.ButtonCancel, TextResources.ButtonDelete);
        if (action == TextResources.ButtonDelete)
        { } // delete
Run Code Online (Sandbox Code Playgroud)

如果您正在执行视图模型优先导航(s. XamarinJonathan Yates 的博客),您可以选择将此部分作为导航器服务的一部分。这是一个品味问题