【问题标题】:Data bindings do not see viewmodel properties数据绑定看不到 viewmodel 属性
【发布时间】:2020-01-19 06:49:10
【问题描述】:

问题

我正在尝试创建看似简单的 MVVM 视图设置。但是,无论我修改什么,我似乎都无法让 PropertyChanged 连接连接到 .xaml,反之亦然。

这是视图:

VpicInformationPage.xaml

<?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:viewModels="clr-namespace:ScanditBarcodeScanner.ViewModels"
             x:Class="ScanditBarcodeScanner.Pages.VehicleOperationPages.VpicInformationPage">
    <!--<ContentPage.BindingContext>
        <viewModels:VpicInformationPageViewModel />
    </ContentPage.BindingContext>-->
    <StackLayout VerticalOptions="CenterAndExpand" Padding="5">
        <StackLayout.BindingContext>
            <viewModels:VpicInformationPageViewModel />
        </StackLayout.BindingContext>
        <Entry x:Name="VinEntry" Placeholder="VIN (Manual Entry)" />
        <Label Text="{Binding VinType.Make}" />
        <Label Text="{Binding VinType.Model}" />
        <Label Text="{Binding VinType.ModelYear}" />
        <Label Text="{Binding VinType.BodyClass}" />
        <Label Text="{Binding VinType.ErrorCode}" />
        <Button Text="Scan/Check VIN" Clicked="ScanOrCheckVin_Clicked"/>
    </StackLayout>
</ContentPage>

模型:

VpicInformationPage.xaml.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Scandit.BarcodePicker.Unified;
using Scandit.BarcodePicker.Unified.Abstractions;
using ScanditBarcodeScanner.ViewModels;
using ScanditBarcodeScanner.ViewModels.Base;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace ScanditBarcodeScanner.Pages.VehicleOperationPages
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class VpicInformationPage : ContentPage, INotifyPropertyChanged
    {
        IBarcodePicker _picker;

        VpicInformationPageViewModel ViewModel;
        public VpicInformationPage()
        {
            InitializeComponent ();

            ViewModel = BindingContext as VpicInformationPageViewModel;

            ViewModel.VinType = VehicleApi.EmptyVinType;

            _picker = ScanditService.BarcodePicker;

            SetVinSettings();

            _picker.DidScan += OnDidScan;

            VinEntry.Text = "";
        }
    //...
    }
}

视图模型:

VpicInformationPageViewModel.cs

using ScanditBarcodeScanner.ViewModels.Base;
using System.ComponentModel;

namespace ScanditBarcodeScanner.ViewModels
{
    public class VpicInformationPageViewModel : ViewModelBase
    {
        #region VinType
        private VehicleApi.VinType _vinType;

        public VehicleApi.VinType VinType
        {
            get { return _vinType; }
            set
            {
                SetValue(ref _vinType, value, "VinType");
                Make = _vinType.Make;
                Model = _vinType.Model;
                ModelYear = _vinType.ModelYear;
                BodyClass = _vinType.BodyClass;
                ErrorCode = _vinType.ErrorCode;
            }
        }
        #endregion VinType

        #region VinType.Make
        private string _make;
        public string Make
        {
            get { return _vinType.Make; }
            private set
            {
                SetValue(ref _make, value);
            }
        }
        #endregion VinType.Make

        #region VinType.Model
        private string _model;

        public string Model
        {
            get { return _vinType.Model; }
            private set
            {
                SetValue(ref _model, value);
            }
        }
        #endregion VinType.Model

        #region VinType.ModelYear
        private string _modelYear;

        public string ModelYear
        {
            get { return _vinType.ModelYear; }
            private set
            {
                SetValue(ref _modelYear, value);
            }
        }
        #endregion VinType.ModelYear

        #region VinType.BodyClass
        private string _bodyClass;

        public string BodyClass
        {
            get { return _vinType.BodyClass; }
            private set
            {
                SetValue(ref _bodyClass, value);
            }
        }
        #endregion VinType.BodyClass

        #region VinType.ErrorCode
        private string _errorCode;

        public string ErrorCode
        {
            get { return _vinType.ErrorCode; }
            private set
            {
                SetValue(ref _errorCode, value);
            }
        }
        #endregion VinType.ErrorCode

        public VpicInformationPageViewModel()
        {
            _vinType = new VehicleApi.VinType();
        }
    }
}

ViewModelBase:

ViewModelBase.cs

using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace ScanditBarcodeScanner.ViewModels.Base
{
    public class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged([CallerMemberName] string PropertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
        }

        protected bool SetValue<T>(ref T BackingField, T Value, [CallerMemberName] string PropertyName = null)
        {
            if(EqualityComparer<T>.Default.Equals(BackingField, Value))
            {
                return false;
            }
            BackingField = Value;
            OnPropertyChanged(PropertyName);
            return true;
        }
    }

我意识到我没有包含 VehicleApi 类。这是该类的重要部分:

VehicleApi.cs

namespace ScanditBarcodeScanner
{
    public static class VehicleApi
    {
        public static VinType EmptyVinType { get; } = new VinType
        {
            Make = "Make",
            Model = "Model",
            ModelYear = "Model Year",
            BodyClass = "Body Class",
            ErrorCode = "Status/Error Code"
        };

        public class VinType
        {
            public string Make { get; set; }
            public string Model { get; set; }
            public string ModelYear { get; set; }
            public string BodyClass { get; set; }
            public string ErrorCode { get; set; }
        }
}

据我了解,我已正确实现这些文件并将它们正确链接在一起。但是,每次我运行该应用程序时,我都会得到:

[0:] Binding: 'VinType' property not found on 'ScanditBarcodeScanner.ViewModels.VpicInformationPageViewModel', target property: 'Xamarin.Forms.Label.Text'

我已将 View 绑定到 ViewModel,并实现了INotifyPropertyChanged

我尝试了许多解决方案,例如更改我设置绑定上下文的位置(在 ContentPage 或 StackLayout 中,甚至两者中),尝试不同的方法来通知视图属性已更改,以及将标签绑定到VinType 的底层成员,并允许 VinType 为它们修改和提升 PropertyChanged。我什至尝试过 PropertyChanged.Fody,但似乎问题不在于通知代码,而在于我将 View 和 ViewModel 绑定在一起的方式,或者可能与属性的定义方式有关。

问题:

绑定连接的哪一部分丢失/不正确?文档说明我应该能够使用我提供的代码访问VinType 及其成员。

Link to sample

上述项目的问题在于,虽然它没有给出我所说的同样的错误,但它仍然没有改变应该改变的字段。

【问题讨论】:

  • 我创建了一些代码来测试,但它工作正常并且没有问题,所以你能提供一个可以在github上重现你的问题的简单代码,然后在这里分享链接吗?
  • 在标签上使用双向绑定的目的是什么?
  • @Jason 我实际上不需要 TwoWay 绑定,所以我删除了它。
  • 您发布的ScanditBarcodeScanner.ViewModels.VpicInformationPageViewModel 类显然确实具有公共VinType 属性,因此错误消息唯一有意义的方法是运行时的上下文对象是一些other i> ScanditBarcodeScanner.ViewModels.VpicInformationPageViewModel 类的版本。如果没有一个好的minimal reproducible example 可以可靠地重现问题,就不可能回答这个问题,但看起来您可能只有一个旧/不同版本的 DLL,该类在运行时使用。尝试一个干净的构建,确保你有正确的版本。
  • 另外,我不熟悉 XAML 的 Xamarin 方言,但在 WPF/WinRT/etc 中。仅当 VehicleApi.VinType 类具有这些属性时,语法 VinType.MakeVinType.Model 等才是正确的。从您的视图模型中的包装器属性来看,它似乎 应该 (如果这确实是问题,那么错误在任何情况下都会有所不同)。但既然你确实有包装器属性,为什么不绑定到那些,而不是VinType 版本? VinType 类是否实现了INotifyPropertyChanged

标签: c# mvvm xamarin.forms


【解决方案1】:

所以,这个问题的解决方法其实很简单。

我只需要将标签定义从 &lt;Label Text="{Binding PropertyName.Member}" /&gt;&lt;Label Text="{Binding PropertyName.Member, StringFormat='{}{0}'}"&gt;

格式字符串中需要{} 的原因是因为在定义StringFormat 时使用单引号'' 而不是双引号"" 时出现错误。

编辑:虽然 StringFormat 问题很重要,但更大的问题是我使用的对象中的属性没有定义 getset。 只要类型是这样的:

public class TypeName
{
    public Type Property1 { get; set; }
    public Type Property2 { get; set; }
}

然后绑定引擎将能够查看和编辑成员属性。

【讨论】:

    猜你喜欢
    • 2013-06-25
    • 2011-02-01
    • 2014-01-21
    • 2011-08-26
    • 2011-03-16
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    相关资源
    最近更新 更多