如何使用命令中的参数调用函数

时间:2019-04-19 15:01:10

标签: mvvm xamarin.forms binding command

我正在尝试调用在手势中可绑定的命令... 为什么TabTappedCommand不做任何事情...

IrrigNetPage.xaml

        <StackLayout BackgroundColor="{Binding SelectedTabColor}"
                     x:Name="StackService">
            <StackLayout.GestureRecognizers>
                <TapGestureRecognizer Command="{Binding TabTappedCommand}" CommandParameter="service"/>
            </StackLayout.GestureRecognizers>
            <Image Source="{Binding ServiceTabIcon}"
                   HorizontalOptions="Start"
                   VerticalOptions="Center" 
                   WidthRequest="16"
                   HeightRequest="16"
                   Aspect="AspectFit"                       
                   x:Name="ServiceIcon"/>
            <Label Text="{i18n:Translate Service}"
               VerticalOptions="FillAndExpand" 
               HorizontalOptions="FillAndExpand"
               VerticalTextAlignment="Center"
               HorizontalTextAlignment="Start"
               TextColor="{Binding SelectedTabTextColor}"
               x:Name="ServiceText"/>
        </StackLayout>

IrrigNetViewModel.cs

public string ServiceTabIcon { get; set; } = "services_sel.png";
public string LocationTabIcon { get; set; } = "location.png";
public string MapTabIcon { get; set; } = "location.png";
public string ListHederIcon { get; set; } = "service_irrig.png";

public ICommand TabTappedCommand { get; }

 public IrrigNetViewModel()
 {
     TabTappedCommand = new Command((tabName) => OnTapClicked(tabName.ToString()));
 }

    private void OnTapClicked(string tabName)
    {
        string serviceBlackIcon = "services.png";
        string locationWhiteIcon = "location_sel.png";
        string mapWhiteIcon = "location_sel.png";

        if (tabName == "service")
        {
            ServiceTabIcon = "services_sel.png";
            LocationTabIcon = "location.png";
            MapTabIcon = "location.png";
        }

        else if (tabName == "location")
        {
            ServiceTabIcon = "services.png";
            LocationTabIcon = "location_sel.png";
            MapTabIcon = "location.png";
        }
        else
        {
            ServiceTabIcon = "services.png";
            LocationTabIcon = "location.png";
            MapTabIcon = "location_sel.png";
        }
    }

因此,重点是更改选定stackLayout的图标(未选择时又返回默认图标),但由于某种原因TabTappedCommand不起作用...我所缺少的是...?

2 个答案:

答案 0 :(得分:0)

您缺少传递参数所需的Command的通用类型。

public ICommand TabTappedCommand<string> { get; }

public IrrigNetViewModel()
{
   TabTappedCommand = new Command<string>(OnTapClicked);
}

答案 1 :(得分:0)

解决方案:

请参阅以下代码

public class IrrigNetViewModel  : INotifyPropertyChanged
{


    public event PropertyChangedEventHandler PropertyChanged;
    private string serviceTabIcon = "services.png"; 
    public string ServiceTabIcon
    {
        get { return serviceTabIcon; }
        set
        {
            serviceTabIcon = value;
            PropertyChanged(this, new PropertyChangedEventArgs("ServiceTabIcon"));
        }
    }

    private string locationTabIcon = "services.png";
    public string LocationTabIcon
    {
        get { return locationTabIcon; }
        set
        {
            locationTabIcon = value;
            PropertyChanged(this, new PropertyChangedEventArgs("LocationTabIcon"));
        }
    }

    private string mapTabIcon = "location.png";
    public string MapTabIcon
    {
        get { return mapTabIcon; }
        set
        {
            mapTabIcon = value;
            PropertyChanged(this, new PropertyChangedEventArgs("MapTabIcon"));
        }
    }

    private string listHederIcon = "services.png";
    public string ListHederIcon
    {
        get { return listHederIcon; }
        set
        {
            listHederIcon = value;
            PropertyChanged(this, new PropertyChangedEventArgs("ListHederIcon"));
        }
    }




    public ICommand TabTappedCommand { get; }

    public IrrigNetViewModel()
    {
        TabTappedCommand = new Command((tabName) => OnTapClicked(tabName.ToString()));



    }

    private void OnTapClicked(string tabName)
    {

        if (tabName == "service")
        {
            ServiceTabIcon = "location.png";
            LocationTabIcon = "location.png";
            MapTabIcon = "location.png";
        }

        else if (tabName == "location")
        {
            ServiceTabIcon = "services.png";
            LocationTabIcon = "location_sel.png";
            MapTabIcon = "location.png";
        }
        else
        {
            ServiceTabIcon = "services.png";
            LocationTabIcon = "location.png";
            MapTabIcon = "location_sel.png";
        }
    }
}