首页 > 解决方案 > Xamarin 数据绑定未更新

问题描述

我对这个练习应用程序的目标是打开一个“编辑联系人页面”,然后返回要在主页上显示的数据。

目前,我可以将绑定上下文传递给 ContactEdit 页面,但更改并未反映在主页中。即使在 ContactEdit 页面中输入文本并导航到主页后,我也可以返回到 ContactEdit 页面并查看更改的数据。我只是不明白在主页上更新/显示此信息时缺少什么。

这是我的代码的绘制方式

对于我的模型,我有 Contact.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace DulyNoted.Models
{
    public class Contact
    {
        public string Name //automatic properties
        {
            get;
            set;
        }

        public string Position
        {
            get;
            set;
        }

        public string PhoneNumber
        {
            get;
            set;
        }

        public string Email
        {
            get;
            set;
        }
    }
}

然后我让我的 ContactServices.cs 作用于模型

using System;
using System.Collections.Generic;
using System.Text;
using DulyNoted.Models;

namespace DulyNoted.Services
{
    public class ContactServices
    {
        public ContactServices()
        {

        }

        public Contact GetContacts()
        {
            Contact contact = new Contact();
            contact.Name = "Jane Doe";
            contact.Position = "Staff Member";
            contact.PhoneNumber = "6175550181";
            contact.Email = "JaneDoe@gmail.com";
            return contact;
        }
    }
}

然后我有一个 ViewModelBase.cs 的实现,它实现了 INotifyPropertychanged。这是由 ContactViewModel.cs 继承的,如下所示。

using System; 
using System.Collections.Generic; 
using System.Text; using DulyNoted.Models; 
using DulyNoted.Services;

namespace DulyNoted.ViewModels 
{
    public class ContactViewModel : ViewModelBase
    {
        private Contact contactList;

        public ContactViewModel()
        {
            var service = new ContactServices();

            ContactList = service.GetContacts();
        }

        public Contact ContactList
        {
            set { SetProperty(ref contactList, value); }
            get { return contactList; }
        }
    } 
}

我的 MainPage.xaml.cs 我创建了一个属性,因此我的按钮将引用与主页相同的 ContactViewModel

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using DulyNoted.Models;
using DulyNoted.ViewModels;
using DulyNoted.Views;

namespace DulyNoted
{
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public ContactViewModel contactViewModel;
        public MainPage()
        {
            InitializeComponent();
            contactViewModel = new ContactViewModel();
            this.BindingContext = contactViewModel;

        }

        async void OnButtonClicked(object sender, EventArgs args)
        {
            var contactEditPage = new ContactEdit();
            contactEditPage.BindingContext = contactViewModel;
            await Navigation.PushAsync(contactEditPage);
        }
    }
}

初始绑定有效,因为我的标签显示了联系人的属性 (MainPage.xaml)

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="DulyNoted.MainPage">
    <StackLayout>
        <Label Text="{Binding ContactList.Name, Mode=TwoWay}"/>
        <Label Text="{Binding ContactList.Position, Mode=TwoWay}"/>
        <Label Text="{Binding ContactList.PhoneNumber, Mode=TwoWay}"/>
        <Label Text="{Binding ContactList.Email, Mode=TwoWay}"/>
        <Button x:Name="EditContact" Text="Click to Edit Contact" Clicked="OnButtonClicked"/>
    </StackLayout>
</ContentPage>

当我导航到 ContactEdit.xaml;我看到我的初始绑定填充,这表明我的另一个视图正在从视图模型中获取信息。

For reference my ContactEdit.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="DulyNoted.Views.ContactEdit">
    <ContentPage.Content>
        <StackLayout>
            <Entry x:Name="NameEntry" Placeholder="Name" Text="{Binding ContactList.Name, Mode=TwoWay}"/>
            <Entry x:Name="PositionEntry" Placeholder="Position" Text="{Binding ContactList.Position, Mode=TwoWay}"/>
            <Entry x:Name="PhoneNumberEntry" Placeholder="PhoneNumberEntry" Text="{Binding ContactList.PhoneNumber, Mode=TwoWay}"/>
            <Entry x:Name="EmailEntry" Placeholder="EmailEntry" Text="{Binding ContactList.Email, Mode=TwoWay}"/>
            <!--<Button x:Name="SaveContact" Text="Save Contact" Clicked="OnSaveButtonClicked"/>-->
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

然后在这里用户将“更改有关联系人的信息”。完成后,他们会导航回上一页。不幸的是,我的主页仍然显示标签上的旧信息。我什至可以再次导航到 ContactEdit 页面并查看我更改的值。我不明白为什么我的主页上的绑定没有更新。

我假设我犯了一个非常明显的错误,因为我对 MVVM 还是很陌生,等等。我可能对“数据绑定”有错误的想法,只是在视图之间传递信息的时间很长。

感谢您提供的任何建议或指点!

标签: c#xamarindata-binding

解决方案


推荐阅读