从视图模型访问并打开 DisplayActionSheet [英] access and open DisplayActionSheet from view model

查看:23
本文介绍了从视图模型访问并打开 DisplayActionSheet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have a toolbar in my content page where there is one item called add , on clicking over add i want to open DisplayActionSheet

我在 xaml 中创建了 ContentPage Toolbar 并在视图模型中将 ICommand 附加到它.现在 DisplayActionSheet 只能在视图中访问,因此我不确定如何访问它并从视图模型中呈现它.

i have created ContentPage Toolbar in xaml and attached ICommand to it in view model. Now DisplayActionSheet is accessible only in View hence i am not sure how will i able to access it and render it from view model.

.xml 文件

<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>

查看模型

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");
}

推荐答案

就像@Alessandro 所说 Application.Current.MainPage 也适用于操作表和警报.为了从视图模型中隐藏视图特定的东西,我创建了一个 IMessageBoxService,它被注入到需要它的视图模型的构造函数中.请注意,我使用的是 Autofac IoC 容器.对于 Xamarin 的 DependencyService,您已更改构造函数并在代码中查找服务.

Like @Alessandro said Application.Current.MainPage works fine for action sheets and alerts as well. To hide view specific stuff from view model I created an IMessageBoxService which is injected into the view models' contructors that need it. Note that I am using the Autofac IoC container. For Xamarin's DependencyService you have change the constructors and look up the service in code.

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

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

If you are doing viewmodel first navigation (s. Xamarin or Jonathan Yates' blog) you may chose to make this part of the Navigator service. It's a matter of taste

这篇关于从视图模型访问并打开 DisplayActionSheet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆