【问题标题】:How to call function with parameters from command如何使用命令中的参数调用函数
【发布时间】:2019-09-09 20:12:00
【问题描述】:

我正在尝试调用可在 Tapgesture 中绑定的命令... 为什么 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 不起作用...我错过了什么...?

【问题讨论】:

    标签: mvvm xamarin.forms binding command


    【解决方案1】:

    解决方案:

    参考以下代码

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 听起来你使用的不是 Xamarin.Forms,而是同名的 .net 命令。
      猜你喜欢
      • 1970-01-01
      • 2015-08-20
      • 1970-01-01
      • 2015-05-01
      • 2017-01-08
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 2017-06-08
      相关资源
      最近更新 更多