首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RelayCommand不起作用

RelayCommand不起作用
EN

Stack Overflow用户
提问于 2018-05-31 18:21:32
回答 1查看 746关注 0票数 0

我有一个使用MVVM的RelayCommand的简单代码,但是没有工作,只想按下一个按钮并显示一条消息,

我的xaml代码:

代码语言:javascript
复制
 <StackLayout
  Padding="8">
    <Button

        Command="{Binding ConvertCommand}"

        Text="Hello">
    </Button>
</StackLayout>

我的ViewModel:

代码语言:javascript
复制
public class MainViewModel
{
    public ICommand ConvertCommand { get { return new RelayCommand(ConvertMoney); } }

    public async void ConvertMoney()
    {
        await App.Current.MainPage.DisplayAlert("hello", "hello", "acept");
        return;
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-01 08:37:36

我认为您错过了将BindingContextPageStackLayout设置为ViewModel

例如

代码语言:javascript
复制
<ContentPage.BindingContext>
    <viewModels:MainViewModel/>
</ContentPage.BindingContext>

代码语言:javascript
复制
<StackLayout.BindingContext>
    <viewModels:MainViewModel/>
</StackLayout.BindingContext>

顺便说一句,您不需要将MvvmLight添加到您的Xamarin.Forms项目中,因为Xamarin.Forms有自己的ICommand实现。您可以简单地使用Command

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50630528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档