mvvm - syncfusion 图表不显示数据

标签 mvvm xamarin.ios

我添加了 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/

相关文章:

c# - 以下设置不匹配,使用 Moq 进行 WPF MVVM 单元测试

安卓架构组件 : ViewModel keeps getting re-initialised

c# - 将 DataGrid 可见性与其自己的 (ItemSource.Count > 0) Silverlight MVVM 绑定(bind)

xamarin.ios - 使用MvxImageViewLoader加载资源图片,可以吗?

ios - Xamarin IOS 中的自定义 CIColorKernel

uiwebview - 使用 "loadHtmlString"或 "loadRequest"将本地数据加载到 WebView 中?

angular - 在 Angular 6 组件中声明模型错误

ios - 带有导航 Controller 的不同页面上的 Xamarin.SideBar

ios - iPhone 中的 UIDatePicker 大小调整

javascript - 如何将 ViewModel Store 绑定(bind)到 View?