Xamarin(WPF)中未刷新数据绑定

时间:2019-02-14 11:35:08

标签: wpf xamarin data-binding

在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是我的单例管理器对象。

现在有另一个视图,它能够从组合框中选择当前的t。该视图的viewModel绑定到管理器的ObservableCollection。如果更改所选对象,则使用与上述相同的代码来完成。管理器的属性是以下代码:

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 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();
    }
}

视图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的视图:

<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中的数据。我可以在调试器中看到通过视图B更改了t的值,但是当我返回视图A时,旧的价值。在调试器中,我可以看到该值已更新。

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

1 个答案:

答案 0 :(得分:0)

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

在视图模型的Messenger订阅中,只需调用属性的RaiseNotifyPropertyChanged,就可以了