Xamarin Forms + Mvvmcross绑定命令不起作用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Xamarin Forms + Mvvmcross绑定命令不起作用相关的知识,希望对你有一定的参考价值。
我是一个新的Xamarin表格。我用mvvmcross创建了一个简单的xamarin表单项目(Hello World非常简单,适合开始),但是当我实现绑定命令时,并没有影响标签的更改文本。我的Xaml代码和ViewModel如下。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:vm="clr-namespace:MvvmCross.ViewModels;assembly=MvvmCross"
x:Class="MvvmCross.Views.HelloView">
<StackLayout>
<StackLayout.BindingContext>
<vm:HelloViewModel />
</StackLayout.BindingContext>
<Entry HorizontalOptions="Fill" VerticalOptions="Center" Text="{Binding Name, Mode=TwoWay }"/>
<Button Text="Hello" HorizontalOptions="Center" VerticalOptions="Center" Command="{Binding HelloCommand}" />
<Label HorizontalOptions="Fill" VerticalOptions="Center" FontSize="15" Text="{Binding Hello, Mode=TwoWay}" />
</StackLayout>
using MvvmCross.Core.ViewModels;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace MvvmCross.ViewModels
{
public class HelloViewModel: Core.ViewModels.MvxViewModel
{
private string _name;
public HelloViewModel()
{
Hello = "Your name";
}
public string Name
{
get { return _name; }
set { _name = value; RaisePropertyChanged(() => Name); }
}
private string _hello;
public string Hello
{
get { return _hello; }
set { _hello = value; RaisePropertyChanged(() => Hello); }
}
private ICommand _helloCommand;
public ICommand HelloCommand
{
get { _helloCommand = _helloCommand ?? new MvxCommand(ShowHello); return _helloCommand; }
}
private void ShowHello()
{
// not change label text so sadly
Hello = Name.ToString();
Debug.WriteLine(Hello);
}
}
}
感谢所有帮助
答案
即使迟到,它也可以帮助别人。
如果您在Xamarin Forms项目上正确设置了MvvmCross(查看[MvvmCross入门] [1]),则无需在视图模型和视图模型中专门设置BindigContext。
关于这个问题,使用按钮的命令绑定的简单示例:
- 视图
<views:MvxContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:views="clr- namespace:MvvmCross.Forms.Views;assembly=MvvmCross.Forms" x:Class="TestProject.Pages.TestPage"> <ContentView> <StackLayout> <Button Text="Test first command!" Command="{Binding TestFirstCommand}"/> <Button Text="Test second command!" Command="{Binding TestSecondCommand}"/> <Label Text="{Binding AnyText}"/> </StackLayout> </ContentView>
- 查看模型
namespace TestProject.ViewModels { public class TestViewModel : MvxNavigationViewModel { private string _AnyTest; public TestViewModel() { AnyText = ""; } public string AnyText { get => _AnyTest; set => SetProperty(ref _AnyTest, value); } public Command TestFirstCommand => new Command(TestFirstCommandMethod); public Command TestSecondCommand => new Command(TestSecondCommandMethod); private void TestFirstCommandMethod() { AnyText = "Hello!"; } private void TestSecondCommandMethod() { AnyText = "How are you?"; } } }
另一答案
你有没有设置BindingContext?
在HelloView.xaml.cs中:
public HelloView() {
BindingContext = new HelloViewModel();
}
我在移动,很难打字..
以上是关于Xamarin Forms + Mvvmcross绑定命令不起作用的主要内容,如果未能解决你的问题,请参考以下文章
如何将 Xamarin Forms Shell 集成到 MvvmCross 设置中
如何将 Xamarin Forms Shell 集成到 MvvmCross 设置中
在 Xamarin.Forms MvvmCross 中存储视图
MvvmCross 与 Xamarin.Forms 和自定义 iOS 渲染器 - 防止导航滑动 iOS - MasterDetail