使用SetBinding时,Xamarin元素无法加载

时间:2016-07-04 09:20:51

标签: c# xamarin oxyplot

这有效(图表已正确加载):

var oxyPlotView = new OxyPlotView{ Model = GetPlotModelSynch() };

这不是(图表保持为空):

var oxyPlotView = new OxyPlotView();
// Here PlotModel will be loaded asynchronously from the BindingContext:
oxyPlotView.SetBinding(OxyPlotView.ModelProperty, new Binding(nameof(GraphViewModel.PlotModel)));

我已经进行了适当的隔离测试,以确保INotifyPropertyChanged与我的ViewModel一起正常工作。所以问题似乎是OxyPlotView只有在它有一开始的信息(?)时才能正确构建。这甚至可能吗?

这是完整的ViewModel。 INotifyPropertyChanged有效,因为Title的行为符合预期(Title在同一视图中绑定到Label

class GraphViewModel : INotifyPropertyChanged
{
    IGraphSeriesGroupRepository _graphSeriesGroupRepository;
    private GraphSeriesGroup _graphSeriesGroup;
    private ulong _sensorId;

    public event PropertyChangedEventHandler PropertyChanged;

    private PlotModel _plotModel;
    public PlotModel PlotModel
    {
        get { return _plotModel; }
        set
        {
            if (_plotModel != value)
            {
                _plotModel = value;
                OnPropertyChanged(nameof(PlotModel));
            }
        }
    }

    private string _title;
    public string Title
    {
        get { return _title; }
        set
        {
            if (_title != value)
            {
                _title = value;
                OnPropertyChanged(nameof(Title));
            }
        }
    }

    private bool _isLoading;
    public bool IsLoading
    {
        get { return _isLoading; }
        set
        {
            _isLoading = value;
            OnPropertyChanged(nameof(IsLoading));
        }
    }

    public GraphViewModel(IGraphSeriesGroupRepository graphSeriesGroupRepository, ulong sensorId)
    {
        _graphSeriesGroupRepository = graphSeriesGroupRepository;
        _sensorId = sensorId;

        Load();
    }

    public PlotModel GetPlotModelSynch()
    {
        _graphSeriesGroup = _graphSeriesGroupRepository.GetGraphSeriesGroup(_sensorId);
        return GetPlotModel(_graphSeriesGroup);
    }

    private async void Load()
    {
        IsLoading = true;
        await Task.Delay(5000);
        _graphSeriesGroup = await _graphSeriesGroupRepository.GetGraphSeriesGroupAsync(_sensorId);
        ApplyChanges();
        IsLoading = false;
    }

    private void ApplyChanges()
    {
        // ---
        Title = _graphSeriesGroup.Title;
        PlotModel = GetPlotModel(_graphSeriesGroup);
    }

    private PlotModel GetPlotModel(GraphSeriesGroup graphSeriesGroup)
    {
        ...
    }

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

}

更新:我发现让它发挥作用的唯一方法是:

    private void chatter_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if(e.PropertyName == nameof(GraphViewModel.PlotModel))
        {
            _oxyPlotView = new OxyPlotView
            {
                Model = _graphViewModel.PlotModel
            };
            _stackLayout.Children.Add(_oxyPlotView);
        }
    }

...即使更新_oxyPlotView(已添加到StackLayout)并致电_oxyPlotView.InvalidateDisplay()也无效。

0 个答案:

没有答案