我有一个使用MVVM的RelayCommand的简单代码,但是没有工作,只想按下一个按钮并显示一条消息,
我的xaml代码:
<StackLayout
Padding="8">
<Button
Command="{Binding ConvertCommand}"
Text="Hello">
</Button>
</StackLayout>我的ViewModel:
public class MainViewModel
{
public ICommand ConvertCommand { get { return new RelayCommand(ConvertMoney); } }
public async void ConvertMoney()
{
await App.Current.MainPage.DisplayAlert("hello", "hello", "acept");
return;
}
}发布于 2018-06-01 08:37:36
我认为您错过了将BindingContext的Page或StackLayout设置为ViewModel。
例如
<ContentPage.BindingContext>
<viewModels:MainViewModel/>
</ContentPage.BindingContext>或
<StackLayout.BindingContext>
<viewModels:MainViewModel/>
</StackLayout.BindingContext>顺便说一句,您不需要将MvvmLight添加到您的Xamarin.Forms项目中,因为Xamarin.Forms有自己的ICommand实现。您可以简单地使用Command。
https://stackoverflow.com/questions/50630528
复制相似问题