简体   繁体   English

Xamarin forms:来自视图模型的命令绑定

[英]Xamarin forms: Command binding from viewmodel

Simply put, I got a page with its viewmodel in xamarin form and I cannot make it work.简而言之,我得到了一个带有 xamarin 形式的视图模型的页面,但我无法让它工作。

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:页面中的相关CS:

    EntranceMenuViewModel viewModel;
    

    public EntranceMenuPage()
    {
        InitializeComponent();

        BindingContext = viewModel = new EntranceMenuViewModel(Navigation);

    }

and finally the relevant Xaml.最后是相关的 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.我尝试在 function 中放置断路器,它似乎没有进入它们。

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.默认情况下,它们是私有的,xaml 实际上无法“看到”它们以执行它们。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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