我对OxyPlot
有困难。当我在图表上加载我的数据时,它是非常好的,但是当我导航到细节活动,然后导航回主活动时,我仍然有图表,但没有绘图数据,它只是空的。
当我导航回主活动时,我看到PlotView
变为null,但是Lineseries
仍然保存数据。
ViewModel
为了简单起见,我刚刚创建了一个简单的ObservableCollection
,它包含重复几次的相同的绘图点组(图形形状)。
public class MyViewModel : BaseViewModel
{
PlotModel GeneratePlotPoints()
{
var mo = new PlotModel();
var s1 = new LineSeries()
{
Color = OxyColors.SkyBlue,
MarkerType = MarkerType.Circle,
MarkerSize = 6,
MarkerStroke = OxyColors.White,
MarkerFill = OxyColors.SkyBlue,
MarkerStrokeThickness = 1.5
};
s1.Points.Add(new DataPoint(0, 10));
s1.Points.Add(new DataPoint(10, 40));
s1.Points.Add(new DataPoint(40, 20));
s1.Points.Add(new DataPoint(60, 30));
mo.Series.Add(s1);
return mo;
}
List<PlotModel> GenerateGraph()
{
var graphPlots = new List<PlotModel>();
int counter = 0;
while (counter < 10)
{
graphPlots.Add(GeneratePlotPoints());
counter++;
}
return graphPlots;
}
public List<PlotModel> GraphPlots => GenerateGraph();
}
布局
您的主要布局与RecyclerView。
<MvxRecyclerView
android:id="@+id/myRecyclerView"
android:layout_marginTop="10dp"
android:scrollbars="vertical"
android:divider="@null"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="ItemsSource GraphPlots"
local:MvxItemTemplate="@layout/mycardview" />
mycardview
布局模板。注意,使用这个点是为了告诉Mvx绑定到整个模型(在本例中是PlotModel)。
<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<oxyplot.xamarin.android.PlotView
android:id="@+id/Plot"
android:layout_width="match_parent"
android:layout_height="match_parent"
local:MvxBind="Model ."/>
</RelativeLayout>
视图
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
var view = this.BindingInflate(Resource.Layout.MainView, null);
HasOptionsMenu = true;
var cardRecyclerView = view.FindViewById<MvxRecyclerView>(Resource.Id.myRecyclerView);
if (cardRecyclerView != null)
{
cardRecyclerView.HasFixedSize = false;
var layoutManager = new LinearLayoutManager(Activity);
cardRecyclerView.SetLayoutManager(layoutManager);
}
return view;
}
发布于 2017-02-12 21:43:57
在ViewModel生命周期的某个地方,在创建模型之前,将模型设置为null。
protected override void Load()
{
plotModel = null;
plotModel = GeneratePlotPoints();
}
https://stackoverflow.com/questions/42032737
复制相似问题