您好我想向您学习如何传递价值观。我有两组数组:矩阵列和曲率数组。通过单击MainWindow类中的按钮从文本文件中读取每个数组,如下所示。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Input ip = new Input();
double[] curvature = new double[40];
double[] moment = new double[40];
string path;
path = "C:\\Desktop\\48co.out";
string[] records = File.ReadAllLines(path);
int linenumber = 0;
string[][] rows = new string[40][];
for (int i = 0; i < records.Length; i++)
{
if (records[i] == "step epscmax in. Tens. Comp. Comp. Tens. force force rad/in (K-ft)")
{
linenumber = i + 1;
}
}
for (int i = linenumber; i < linenumber + 40; i++)
{
rows[i - linenumber] = records[i].Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
}
for (int i = 0; i < 40; i++)
{
curvature[i] = Double.Parse(rows[i][9]);
moment[i] = Double.Parse(rows[i][10]);
}
ip.curvature = curvature;
ip.moment = moment;
PlotMPhi plotmphi = new PlotMPhi();
plotmphi.Show();
}
}
这两个数组被传递给另一个名为“Input”的类。
class Input
{
public Input()
{
double[] curvature = new double[40];
double[] moment = new double[40];
}
public double[] curvature { get; set; }
public double[] moment { get; set; }
}
我的想法是将所有输入参数存储在Input类中,如果其他类中的任何方法需要它们,则可以将它们传递给。在这种情况下,我想使用WPF中的OXYplot称为PlotMPhi,使用曲率数组中的x点和来自矩阵列的y点绘制图表。
public partial class PlotMPhi : Window
{
public PlotMPhi()
{
var vm = new MVMMPhi();
this.DataContext = vm;
InitializeComponent();
}
}
class MVMMPhi
{
public Collection<MeaMPhi> MeaMPhis { get; private set; }
public MVMMPhi()
{
MeaMPhis = new Collection<MeaMPhi>();
Input ip = new Input();
for (int i = 0; i < 40; i++)
{
MeaMPhis.Add(new MeaMPhi { Curvature = ip.curvature[i],Moment = ip.moment[i]});
}
}
}
public class MeaMPhi
{
public double Curvature { get; set; }
public double Moment { get; set; }
}
这是用于绘制图表的xaml文件。
<Window x:Class="XsectionWIN.PlotMPhi"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:oxy="clr-namespace:OxyPlot.Wpf;assembly=OxyPlot.Wpf"
Title="Moment-Curvature" Height="480" Width="640">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="Save plot..." Click="SavePlot_Click"/>
<Separator />
<MenuItem Header="_Exit" Click="exit_Click" />
</MenuItem>
</Menu>
<oxy:Plot Grid.Row="1" x:Name="plot" Title="Moment-Curvature" Subtitle="{Binding Subtitle}" >
<oxy:Plot.Axes>
<oxy:LinearAxis Position="Bottom" Title="Curvature (rad/in)" TitleFont="Arial" TitleFontSize="12" TitleColor="Black"/>
<oxy:LinearAxis Position="Left" Title="Momennt (kips-in)" TitleFont="Arial" TitleFontSize="12" TitleColor="Black"/>
</oxy:Plot.Axes>
<oxy:Plot.Series>
<oxy:LineSeries Title="M-curvature" DataFieldX="Period_t" DataFieldY="Acc_t" Color="Red" StrokeThickness="3" ItemsSource="{Binding MeaMPhis}"/>
</oxy:Plot.Series>
</oxy:Plot>
</Grid>
</Window>
问题是这两个数组没有传递给MVMMPhi类。我按步骤单击F11键检查了该过程。似乎这些数组在到达
之前传递给Input类PlotMPhi plotmphi = new PlotMPhi();
plotmphi.Show();
进入此步骤后,Inout中的矩和曲率数组变为NULL。我之前遇到过这种情况所以我所做的就是将这些数组直接放在方法中,即
PlotMPhi plotmphi = new PlotMPhi(moment,curvature);
直到现在,这对我有用。但我需要稍后处理很多数组,所以我正在寻找简单的方法来处理,并想知道为什么我的想法不起作用。我是编程世界的新手。我不想得到勺子喂,所以任何建议或提示将不胜感激。如果您需要其他信息,请告诉我。感谢您的时间和帮助,
答案 0 :(得分:1)
可以使DataContext类的构造函数采用Input类型的参数,这样就可以更改代码
public MVMMPhi(Input data)
{
MeaMPhis = new Collection<MeaMPhi>();
for (int i = 0; i < 40; i++)
{
MeaMPhis.Add(new MeaMPhi
{
Curvature = data.curvature[i],
Moment = data.moment[i]
});
}
}
这可能无法解决您的所有问题,但可能会使思考更加直接。
答案 1 :(得分:1)
我在下面包含的内容包含3个主要更改: 1)我将文件中的初始数据直接构建到MeaMPhis MeaMPhi集合中。 2)我将集合(即对MeaMPhis实例的引用)传递给窗口PlotMPhi,然后将其传递给同一个链到MVMMPhi。 3)感谢(1)+(2),MVMMPhi类不再需要从曲率[],时刻[]复制到MeaMPhi的集合。它自己的MeaMPhis直接指向这个集合,它已被Button_click填充,并且只是通过引用传递给了MeaMPhi构造函数。
这是稍微修改过的Button_Click
private void Button_Click(object sender, RoutedEventArgs e)
{
Collection<MeaMPhi> MeaMPhis = new Collection<MeaMPhi>();
//Input ip = new Input();
//double[] curvature = new double[40];
//double[] moment = new double[40];
string path;
path = "C:\\Desktop\\48co.out";
string[] records = File.ReadAllLines(path);
int linenumber = 0;
string[][] rows = new string[40][];
for (int i = 0; i < records.Length; i++)
{
if (records[i] == "step epscmax in. Tens. Comp. Comp. Tens. force force rad/in (K-ft)")
{ linenumber = i + 1; }
}
for (int i = linenumber; i < linenumber + 40; i++)
{
rows[i - linenumber] = records[i].Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
}
for (int i = 0; i < 40; i++)
{
MeaMPhis.Add(new MeaMPhi
{
Curvature = Double.Parse(rows[i][9]),
Moment = Double.Parse(rows[i][10])
});
//curvature[i] = Double.Parse(rows[i][9]);
//moment[i] = Double.Parse(rows[i][10]);
}
//ip.curvature = curvature;
//ip.moment = moment;
PlotMPhi plotmphi = new PlotMPhi(MeaMPhis);
plotmphi.Show();
}
}
以下是窗口和其他类。
public partial class PlotMPhi : Window
{
public PlotMPhi(Collection<MeaMPhi> MeaMPhis)//double[] moment,double [] curvature)
{
var vm = new MVMMPhi(MeaMPhis);
this.DataContext = vm;
InitializeComponent();
}
private void SavePlot_Click(object sender, RoutedEventArgs e)
{
}
private void exit_Click(object sender, RoutedEventArgs e)
{
}
}
class MVMMPhi
{
public Collection<MeaMPhi> MeaMPhis { get; private set; }
public MVMMPhi(Collection<MeaMPhi> Meamphis)
{
MeaMPhis = Meamphis;
//Input ip = new Input();
/*
for (int i = 0; i < 40; i++)
{
MeaMPhis.Add(new MeaMPhi
{
Curvature = curvature[i],
Moment = moment[i]
});
}
*/
}
}
public class MeaMPhi
{
public double Curvature { get; set; }
public double Moment { get; set; }
}
感谢您提供完整的代码,帮助我了解一下OxyPlot。 我希望上面的完整工作代码能够澄清我的解释。 这里的主要观点是避免不必要地复制数据,并避免创建Input()的2“ip”实例。顺便说一下,在提出的解决方案中,不再使用Input()类。