同步图表不显示数据

时间:2018-05-02 15:58:04

标签: mvvm xamarin.ios

我添加了SFchart,它没有错误并且编译。它显示一个空的chartview。 我在Xamarin.IOS中使用MVVMcross

我请求的数据在那里,它包含大约200行,我的api使用方法 //Initialize the Chart with required frame. This frame can be any rectangle, which bounds inside the view. SFChart chart = new SFChart(); chart.Frame = this.headerView.Frame; //Adding Primary Axis for the Chart. SFCategoryAxis primaryAxis = new SFCategoryAxis(); chart.PrimaryAxis = primaryAxis; //Adding Secondary Axis for the Chart. SFNumericalAxis secondaryAxis = new SFNumericalAxis(); chart.SecondaryAxis = secondaryAxis; chart.Series.Add(new SFColumnSeries() { ItemsSource = (this.ViewModel as UserCoinViewModel).CoinHistory, XBindingPath = "price_btc", YBindingPath = "timestamp" }); this.View.AddSubview(chart); 请求数据。

我在viewdidload中的观点:

private List<CoinHistoryModel> _CoinHistory;

    public List<CoinHistoryModel> CoinHistory
    {
        get
        {
            return _CoinHistory;
        }
        set
        {
            _CoinHistory = value;
            RaisePropertyChanged(() => CoinHistory);
        }
    }

视图模型:

{{1}}

1 个答案:

答案 0 :(得分:1)

因为您使用的是MVVMcross,所以应使用bind方法设置Series的{​​{1}}。您只需将ItemsSource设置为viewModel实例的属性,当值更改时,它将不会通知View。所以它似乎显示一个空图表。

将代码修改为绑定,如:

ItemsSource

然后在ViewModel中创建一个命令,如:

SFColumnSeries series = new SFColumnSeries()
{
    XBindingPath = "price_btc",

    YBindingPath = "timestamp"
};
chart.Series.Add(series);

var set = this.CreateBindingSet<YourView, UserCoinViewModel>();
...
        set.Bind(series).For(s => s.ItemsSource).To(vm => vm.CoinHistory);
...
set.Apply();

最后在private MvxCommand loadDataCommand; public ICommand LoadDataCommand { get { return loadDataCommand ?? (loadDataCommand = new MvxCommand(ExecuteloadDataCommand)); } } private void ExecuteloadDataCommand() { CoinHistory = new List<CoinHistoryModel>() { new CoinHistoryModel{ price_btc = "First", timestamp = 10 }, new CoinHistoryModel{ price_btc = "Second", timestamp = 20 }, new CoinHistoryModel{ price_btc = "Third", timestamp = 30 } }; // Change it to your data request here } 事件中触发此命令:

ViewWillAppear()