简体   繁体   中英

Xamarin forms: Command binding from viewmodel

Simply put, I got a page with its viewmodel in xamarin form and I cannot make it work.

Here's the relevant parts in the viewmodel:

public EntranceMenuViewModel(INavigation navigation)
    {
        Navigation = navigation;
        Title = "Entrées";

        btnInventoryCommand = new Command(async () => await btnInventoryCommandExe());
        btnMoveGoodsCommand = new Command(async () => await btnMoveGoodsCommandExe());

    }



    ICommand btnInventoryCommand { get; set; }
    ICommand btnMoveGoodsCommand { get; set; }
    async Task btnInventoryCommandExe()
    {
        await Navigation.PushAsync(new Inv2Page());
    }
    async Task btnMoveGoodsCommandExe()
    {
        await Navigation.PushAsync(new Inv2Page(true));
    }

the relevant CS in the page:

    EntranceMenuViewModel viewModel;
    

    public EntranceMenuPage()
    {
        InitializeComponent();

        BindingContext = viewModel = new EntranceMenuViewModel(Navigation);

    }

and finally the relevant Xaml.

<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="60"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <Button Grid.Column="0" Text="{Binding test}" Command="{Binding btnInventoryCommand}"></Button>
        <Button Grid.Column="1" Text="Move Goods" Command="{Binding btnMoveGoodsCommand}"></Button>
    </Grid>

It does bind, since the text binding is working. I double checked the names, and I didn't see any issue with that. I feel like I'm missing something fundamental, but after going through my tutorial videos twice I can't understand. I tried putting breaker within the function, it doesn't seem to enter them.

Thank you for the help

Make the commands public. By default they are private and the xaml can't actually "see" them in order to execute them.

public ICommand btnInventoryCommand { get; set; }
public ICommand btnMoveGoodsCommand { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM