如何打开对话框并停止用户单击TextCell的操作?

时间:2017-10-01 13:19:42

标签: xamarin xamarin.forms

我有XAML,用户可以选择三种不同的方式来查看数据:

<TableSection Title="Selector" x:Name="ccSection">
   <local:CustomTextCell Text="{Binding [0].Name}" IsChecked="{Binding [0].IsSelected}" IsEnabled="{Binding [0].IsSelected, Converter={StaticResource InverseBoolConverter} }" Tapped="ccSelectValue" />
   <local:CustomTextCell Text="{Binding [1].Name}" IsChecked="{Binding [1].IsSelected}" IsEnabled="{Binding [1].IsSelected, Converter={StaticResource InverseBoolConverter} }" Tapped="ccSelectValue" />
   <local:CustomTextCell Text="{Binding [2].Name}" IsChecked="{Binding [2].IsSelected}" IsEnabled="{Binding [2].IsSelected, Converter={StaticResource InverseBoolConverter} }" Tapped="ccSelectValue" />
</TableSection>

在我的后端C#中我有:

SSVViewModel[] CardChoice = new[]{
   new SSVViewModel { Id = (int)CC.CategoryGroup, Name = "Category Group", IsSelected = false },
   new SSVViewModel { Id = (int)CC.Category, Name = "Category", IsSelected = false },
   new SSVViewModel { Id = (int)CC.All, Name = "All", IsSelected = false },
};

以下是用户在点击该行时设置新选择值的代码:

    void ccSelectValue(object sender, EventArgs e)
    {
        var cell = sender as TextCell;
        if (cell == null)
            return;

        var selected = cell.Text;

        foreach (var setting in CardChoice)
        {
            if (setting.Name == selected)
            {
                setting.IsSelected = true;
                // Do something
            }
            else
                setting.IsSelected = false;
        }

    }

    void setSelectValue(SSVViewModel[] settings, string val)
    {
        foreach (var setting in settings)
        {
            if (setting.Name == val)
                setting.IsSelected = true;
            else
                setting.IsSelected = false;
        }
    }

在我自定义的渲染器中:

public class CustomTextCellRenderer : TextCellRenderer
{
    UITableViewCell _nativeCell;

    public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
    {
        _nativeCell = base.GetCell(item, reusableCell, tv);
        var formsCell = item as CustomTextCell;

        if (formsCell != null)
        {
            formsCell.PropertyChanged -= OnPropertyChanged;
            formsCell.PropertyChanged += OnPropertyChanged;
        }

        SetCheckmark(formsCell);
        SetTap(formsCell);

        return _nativeCell;
    }

    void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        var formsCell = sender as CustomTextCell;
        if (formsCell == null)
            return;

        if (e.PropertyName == CustomTextCell.IsCheckedProperty.PropertyName)
        {
            SetCheckmark(formsCell);
        }

        if (e.PropertyName == CustomTextCell.NoTapProperty.PropertyName)
        {
            SetTap(formsCell);
        }
    }

    private void SetCheckmark(CustomTextCell formsCell)
    {
        if (formsCell.IsChecked)
            _nativeCell.Accessory = UITableViewCellAccessory.Checkmark;
        else
            _nativeCell.Accessory = UITableViewCellAccessory.None;
    }

    private void SetTap(CustomTextCell formsCell)
    {
        if (formsCell.NoTap)
            _nativeCell.SelectionStyle = UITableViewCellSelectionStyle.None;
        else
            _nativeCell.SelectionStyle = UITableViewCellSelectionStyle.Default;
    }

我想要做的是在执行单击customTextCell的操作之前执行确认。

这样的事情:

if (await this.DisplayAlert(
  "Do this", "Do you want to do this", "OK", "Cancel")) { }

所以我的问题是如何实现此检查以显示警报。我想过在customRenderer中实现但不确定它是否合适。我很感激任何人都可以给我的建议。

1 个答案:

答案 0 :(得分:1)

假设ccSelectValue是事件处理程序 - 可以将其标记为异步。 This is OK even if the return type is void

async void ccSelectValue(object sender, EventArgs e)
{
    var cell = sender as TextCell;
    if (cell == null)
        return;

    //display alert to user, and continue only if user says yes.
    var canContinue = await this.DisplayAlert("Do this", "Do you want to do this", "OK", "Cancel");
    if (canContinue == false)
        return;
    ....