Xamarin Forms + Mvvmcross绑定命令不起作用

问题描述 投票:0回答:2

我是一个新的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 xamarin.forms mvvmcross
2个回答
0
投票

即使迟到,它也可以帮助别人。

如果您在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?"; } } }

-1
投票

你有没有设置BindingContext?

在HelloView.xaml.cs中:

public HelloView() {
    BindingContext = new HelloViewModel();
}

我在移动,很难打字..

© www.soinside.com 2019 - 2024. All rights reserved.