gpt4 book ai didi

mvvm - syncfusion 图表不显示数据

转载 作者:行者123 更新时间:2023-12-03 10:32:42 24 4
gpt4 key购买 nike

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

我请求的数据在那里,它包含大约 200 行,数据是从我的 api 使用方法 override void viewAppearing 请求的.

我在 viewdidload 中的看法:

 //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);

View 模型:
private List<CoinHistoryModel> _CoinHistory;

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

最佳答案

因为您使用的是 MVVMcross,所以您应该使用 bind设置您的Series的方法的ItemsSource .您只需设置 ItemsSource到 viewModel 实例的属性,当值改变它不会通知 View 。所以它似乎显示了一个空图表。

修改您的代码以绑定(bind)如下:

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

然后在您的 ViewModel 中,创建如下命令:
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() 中触发此命令事件:
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);

(ViewModel as UserCoinViewModel).LoadDataCommand.Execute(null);
// You can also bind a button to this command to trigger it manually.
}

关于mvvm - syncfusion 图表不显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50138855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com