【发布时间】:2014-02-24 17:41:39
【问题描述】:
您好,我想向您学习如何传递值。我有两组数组:矩数组和曲率数组。通过单击 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 中称为 PlotMPhi 的 OXYplot 使用曲率数组中的 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);
到目前为止,这对我有用。但是我以后需要处理很多数组,所以我正在寻找简单的方法来处理并且想知道为什么我的想法不起作用。我是编程界的新手。我不想被勺子喂食,所以任何建议或提示都会受到赞赏。如果您需要更多信息,请告诉我。感谢您的时间和帮助,
【问题讨论】: