【问题标题】:Databinding not refreshed in Xamarin (WPF)Xamarin (WPF) 中未刷新数据绑定
【发布时间】:2019-02-14 11:35:08
【问题描述】:

在 Xamarin 中使用 MVVM 时,我遇到了一些数据绑定问题。让我解释一下我的架构:

我有一个 Manager 类,它包含一个 ObservableCollection 类型为 t 的类,t 是我的模型类。 Manager 类还包含一个名为 activeT 的属性,它是我的模型类的当前选定对象。有 2 个 UI,一个显示 t 的当前数据。 viewModel 像这样绑定到我的 Manager 类的属性 t:

public t CurrentT
    {
        get
        {
            return _mgr.CurrentT;
        }
        set
        {
            _mgr.CurrentT = value;
            OnPropertyChanged();
        }
    } 

_mgr 是我的单例 Manager-Object。

现在有另一个视图,它可以从组合框中选择当前的 t。该视图的 viewModel 绑定到管理器的 ObservableCollection。如果我更改选定的对象,我会像上面一样使用相同的代码。 manager的Property如下代码:

public t CurrentT
    {
        get
        {
            return _currentT;
        }
        set
        {
            _currentT= value;
            OnPropertyChanged());

        }
    }

现在的问题是,查看当前选定 t 的第一个视图没有刷新,尽管我可以在调试器中看到当前 t 被另一个视图更改。

有人可以帮我吗?

编辑:

我提供更多代码:

经理级:

public class Manager : INotifyPropertyChanged
{
    private t _currentConstructionSite;
    private ObservableCollection<t> _constructionSites = null;

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string name)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

    public t CurrentConstructionSite
    {
        get
        {
            return _currentConstructionSite;
        }
        set
        {
            _currentConstructionSite = value;
            OnPropertyChanged("CurrentConstructionSite");
        }
    }

    public ObservableCollection<t> ConstructionSites
     {
        get
        {
            return _constructionSites;
        }
        set
        {
            _constructionSites = value;
        }
    }


    private Manager()
    {
        ConstructionSites = DataRepository.GenConstructionSites();
        _currentConstructionSite = ConstructionSites[0];
    }
}

ViewModels Class A(这是视图的视图模型,显示一些数据):

public class DashboardViewModel : ViewModelBase
{
    private Manager _mgr;

    public t CurrentConstructionSite
    {
        get
        {
            return _mgr.CurrentConstructionSite;
        }
        set
        {
            _mgr.CurrentConstructionSite = value;
            OnPropertyChanged();
        }
    }

    public DashboardViewModel()
    {
        _mgr = Manager.getInstance();
    }
}

View A 显示一些数据:

来自 XAML 的绑定设置:

<ContentPage.BindingContext>
    <local:DashboardViewModel x:Name="viewModel"/>
</ContentPage.BindingContext>

绑定标签显示数据:

<Label Text="{Binding CurrentConstructionSite.ConstructionSiteName, Mode=TwoWay}" HorizontalOptions="Center" Font="Bold" FontSize="Large"/>

ViewModel B选择当前t:

public class ChooseConstructionSiteViewModel : ViewModelBase
{
    Manager _mgr = null;

    public ObservableCollection<t> ConstructionSites
    {
        get
        {
            return _mgr.ConstructionSites;
        }
    }

    public t CurrentConstructionSite
    {
        get
        {
            return _mgr.CurrentConstructionSite;
        }
        set
        {
            _mgr.CurrentConstructionSite = value;
            OnPropertyChanged();
        }
    }

    public ChooseConstructionSiteViewModel()
    {
        _mgr = Manager.getInstance();
    }
}

选择当前t的View:

<combobox:SfComboBox x:Name="combobox" Grid.Row="0" Grid.Column="1" Margin="8,0,20,0" VerticalOptions="Center" HeightRequest="40" DataSource="{Binding ConstructionSites}" DisplayMemberPath="ConstructionSiteName" SelectionChanged="Handle_SelectionChanged"/>

如果组合框中的选择发生了变化:

void Handle_SelectionChanged(object sender, Syncfusion.XForms.ComboBox.SelectionChangedEventArgs e)
    {
        t selectedItem = e.Value as t;
        _viewModel.CurrentConstructionSite = selectedItem;
    }

这两个视图作为 contetPages 包含在 tabbedPage 中。它通常可以工作,但是在视图 B 中更改选定的 t 不会更新视图 A 中的数据。我可以在调试器中看到 t 的值是通过视图 B 更改的,但是当我返回视图 A 时,旧值。在调试器中,我可以看到值已更新。

顺便说一句:ViewModelBase 是实现 INotifyPropertyChanged 的​​类

【问题讨论】:

  • 您需要提供更多详细信息。你在实现INotifyPropertyChanged 接口吗?你如何设置DataContext
  • 我编辑我的帖子。

标签: wpf xamarin data-binding


【解决方案1】:

从您的第二个视图模型中,您需要“通知”第一个视图模型数据已更改。 其中一种方法是使用 Messenger(MvvmLight 有一个,MvvmCross 也有)。然后,您可以使用 Manager 的信使通知所有需要该信息的视图模型,即 CurrentT 已更改。

在您的视图模型中的信使订阅中,只需调用您的属性的 RaiseNotifyPropertyChanged,您就可以开始了

【讨论】:

  • 如何在没有 MvvmLight 或 MvvmCross 之类的框架的情况下手动提高它?
  • 通过引发您必须在某处实现的 NotifyPropertyChanged 事件
猜你喜欢
  • 2019-07-13
  • 2011-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-05
  • 2010-10-13
  • 1970-01-01
相关资源
最近更新 更多