Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin 如何打开对话框并停止用户单击TextCell的操作?_Xamarin_Xamarin.forms - Fatal编程技术网

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

Xamarin 如何打开对话框并停止用户单击TextCell的操作?,xamarin,xamarin.forms,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 InverseBo

我有一个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>
下面是当用户单击该行时,我用来设置新选择值的代码:

    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中实现,但不确定这是否合适。如果有人能给我一些建议,我将不胜感激。

假设
ccSelectValue
是一个事件处理程序,可以将其标记为异步


为什么不将其添加到ccSelectValue中?
if (await this.DisplayAlert(
  "Do this", "Do you want to do this", "OK", "Cancel")) { }
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;
    ....