【问题标题】:Xamarin Forms + Mvvmcross binding command not workXamarin Forms + Mvvmcross 绑定命令不起作用
【发布时间】:2017-08-18 14:01:38
【问题描述】:

我是一个新的 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);
    }
}

}

感谢大家的帮助

【问题讨论】:

  • 文本属性有效吗?还是只是失败的命令?
  • 文本属性有效,但在 ui 中运行时无效。
  • 对我来说似乎 BindingContext 不正确。
  • 如何修复这个绑定上下文

标签: xamarin xamarin.forms mvvmcross


【解决方案1】:

即使迟到了,也能帮到别人。

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

【讨论】:

    【解决方案2】:

    你设置了 BindingContext 了吗?

    在您的 HelloView.xaml.cs 中:

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

    我在手机上,真的很难打字..

    【讨论】:

    • 我想我用 xaml 代码设置
    猜你喜欢
    • 2019-07-30
    • 2020-02-12
    • 2023-03-24
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 2015-06-21
    • 2020-08-29
    相关资源
    最近更新 更多