绑定值未在来自其他 xaml 的条目控制上更新

Binding value is not updating on entry control from other xaml

我的 XAML 中有一个入口控件,我通过数据绑定设置页面显示的初始值。最初出现该值但是当我从另一个视图模型更新它时它没有在 UI.

上更新

下面是XAML代码和XAML.CS

        <ListView
        x:Name="workList"
        Grid.Row="2"
        SeparatorColor="{DynamicResource AccentColor}"
        ItemsSource="{ Binding WorkItems }"                   
        Margin="5"
        CachingStrategy="RecycleElement"
        RowHeight="440"
        SeparatorVisibility="Default"
        SelectionMode="None"
        HasUnevenRows="False">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <local:LoadItemPutawayTemplate />
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>


 <?xml version="1.0" encoding="UTF-8"?>
 <ContentView 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="Sanipex.LoadItemPutawayTemplate">

<Grid
    RowSpacing="0"
    Padding="0"
    Margin="0,10,0,0"
    >

    <Grid.RowDefinitions>
        <RowDefinition
            Height="*" />
    </Grid.RowDefinitions>


            <Entry
                x:Name="OverrideLoc"
                 Grid.Row="0"
                TextColor="Black"
                WidthRequest="110"
                Text="{Binding toLocation}"
                grial:EntryProperties.BorderCornerRadius="10"
                grial:EntryProperties.BorderStyle="RoundRect"
                grial:EntryProperties.BorderColor="Black"
                HorizontalOptions="StartAndExpand"
                VerticalOptions="Center"
                Focused="OverrideLoc_Focused"
                TextChanged="OverrideLoc_TextChanged"
                grial:EntryProperties.HorizontalPadding="5"
                FontAttributes="Bold"
                PlaceholderColor="Black"
                FontSize="20"/>


    </Grid>

public partial class ItemPutAway : ContentPage
{
    private static ItemPutAwayViewModel obj;
    public ItemPutAway()
    {
        InitializeComponent();


        obj = new ItemPutAwayViewModel();
        BindingContext = obj;
    }

    public static ItemPutAwayViewModel itemPutAwayViewModel
    {
        get
        {
            return obj;
        }
    }

    protected override async void OnAppearing()
    {
        obj.LoadData();
    }
}

下面是我的第一个视图模型代码

public class ItemPutAwayViewModel : INotifyPropertyChanged
{
  private IList<WorkItem> workItems;
  public event PropertyChangedEventHandler PropertyChanged;
  public string ltoLocation;

   public string toLocation
    {
        get => ltoLocation;
        set
        {
            ltoLocation = value;
            OnPropertyChanged(nameof(toLocation));
        }
    }

    public IList<WorkItem> WorkItems
    {
        get => workItems;
        set
        {
            workItems = value;
            OnPropertyChanged(nameof(WorkItems));
        }
    }

    public void LoadData()
    {
        WorkItems = App.dataManager.GetItemPutAwayWorks();
    }

    public void setLocation(string _location)
    {
        toLocation = _location;
    }

     protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

下面是我尝试将 toLocation 绑定值更新为与另一个 XAML 页面不同的值的代码,如下所示:

public partial class AvailableLocationsPopUp : PopupPage
{
    private static AvailableLocationViewModel obj;


    public AvailableLocationsPopUp(WorkItem _workItem)
    {
        InitializeComponent();


        obj = new AvailableLocationViewModel(gWorkItem.itemid);
        BindingContext = obj;
    }

    private void OnClose(object sender, EventArgs e)
    {
        PopupNavigation.Instance.PopAsync();
    }

    private void ListView_ItemTapped(object sender, ItemTappedEventArgs e)
    {
        Location content = e.Item as Location;

        ItemPutAway.itemPutAwayViewModel.setLocation("ABC-XYZ");

        PopupNavigation.Instance.PopAsync();

    }
}

正如我在讨论中提到的,您还必须实现 class WorkItemINotifyPropertyChanged 接口。

在 ItemPutAwayViewModel 中实施 INotifyPropertyChanged 只会帮助 WorkItems 中的更改(例如添加或删除一个 WorkItem),而不是 WorkItem.

中的更改

所以,代码应该是:

public class WorkItem : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    private string _toLocation; 

    public string toLocation 
    { 
    get => _toLocation; 
    set 
        { 
          _toLocation = value; 
          NotifyPropertyChanged(); 
        } 
    } 

    protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "") 
    { 
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 

}