Search code examples
c#xamarininterfaceicommand

ICommand Interface with Parameters doesn´t work


i´m trying to get warm with ICommand-Data Binding. Therefor i made an app which should work like this: i´ve got 2 buttons. One is "+1", it just counts up. The second one is "Multipy", that should multiply the value with itself. So for example: i click the first butten 3 times. now i press the second button: it makes 3*3 and we got 9 as the new value. The first butten is wurking, and i guess the second one is not that bad aswell, but i cant give the parameter to it when it s executed. have a look:

public class CounterViewModel : BaseViewModel
{

    public ObservableCollection<NumberViewModel> Nummer { get; private set; } = new ObservableCollection<NumberViewModel>();
    int current = 0;

    public ICommand CountUpCommand { get; private set; }
    public ICommand MultiplyCommand { get; private set; }
    public ICommand DelCommand { get; private set; }

    Number Zahl = new Number();

    public CounterViewModel()
    {
        CountUpCommand = new Command(CountUp);
        DelCommand = new Command(SetZero);
        //MultiplyCommand = new Command<int>(Multiply).Execute(current); 
        //MultiplyCommand = new Command<int>(current => Multiply(current));
        // Both doesen´t work
    }


    public void CountUp()
    {
        // current = Nummer.Count + 1;
        current = current + 1;
        Nummer.Add(new NumberViewModel { Num = current });
    }

    public void Multiply(int _multiply)
    {
        current = _multiply * _multiply;
        Nummer.Add(new NumberViewModel { Num = current });
    }

also here my "Number.cs":

public class Number
{

    public int Num { get; set;}

} 

and for the interessed one my xaml file:

<StackLayout>
        <Button Text="+1" Command="{Binding CountUpCommand}" />
        <Button Text="Erg x Erg" Command="{Binding MultiplyCommand}"/>
        <Button Text="DEL" Command="{Binding DelCommand}" />
    </StackLayout>
<Label Text="--------------" />
<StackLayout>
    <ListView ItemsSource="{Binding Nummer}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextCell 
                Text="{Binding Num}" 
                />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</StackLayout>

but i dont know if this is nesecerry. can you help me?


Solution

  • Your command binding doesn't specify any command parameter, that's why it will not work.

    You need to specifiy it in your xaml file like this.

     <Button Text="Erg x Erg" Command="{Binding MultiplyCommand}" CommandParameter="{Binding CurrentNumber}"/>
    

    For this to work you also need to update you viewmodel with a correct Number property :

    private int _currentNumber;
    public int CurrentNumber
    {
        get
        {
            return _currentNumber;
        }
        set
        {
            _currentNumber = value;
            OnPropertyChanged(nameof(CurrentNumber));
            // or (depending on if the Method uses the [CallerMemberName] attribute)
            OnPropertyChanged();
        }
    }
    
    
    public void CountUp()
    {
        // current = Nummer.Count + 1;
        CurrentNumber += Current + 1;
        Nummer.Add(new NumberViewModel { Num = CurrentNumber });
    }
    
    public void Multiply(int multiplyParameter)
    {
        CurrentNumber = multiplyParameter * multiplyParameter;
        Nummer.Add(new NumberViewModel { Num = CurrentNumber});
    }
    

    The RaisePropertyChanged syntax might change depending on what MVVM framework you are using, but that's the idea.