【问题标题】:Passing value to another class将值传递给另一个类
【发布时间】: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);

到目前为止,这对我有用。但是我以后需要处理很多数组,所以我正在寻找简单的方法来处理并且想知道为什么我的想法不起作用。我是编程界的新手。我不想被勺子喂食,所以任何建议或提示都会受到赞赏。如果您需要更多信息,请告诉我。感谢您的时间和帮助,

【问题讨论】:

    标签: c# wpf passwords


    【解决方案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]
                                     });
    
        }
    }
    

    这可能无法解决你所有的问题,但它可能会让你的想法更直接。

    【讨论】:

    • 我厌倦了这个并且仍然为空。任何想法?谢谢,
    • 哪一部分是空的?您需要确保使用的 Input 是用数据初始化的完全相同的对象(不仅仅是相同的类型)。我还会更改构建 PlotMPhi 的部分。
    【解决方案2】:

    在您的代码中,您混淆了输入类的 2 个“ip”实例。 Input 中的矩和曲率数组不会变为 NULL。发生的情况是有 2 个不同的对象称为 ip:1 在 Button_click 内部(一旦此事件完成,它将被销毁),另一个 ip 是在窗口“PlotMPhi”实例化类“MVMMPhi”时创建的,该类本身实例化 2 个东西:一个 MeaMPhis 集合,它将包含许多数据对“MeaMPhi”和一个新的 Input 实例“ip”,其中包含 2 个空数组。 然后你的解决方法是(麻烦)将 2 个数组传递给 PlotMPhi,然后它必须将它们传递给 MVMMPhi,以便它可以使用它们来填充集合 MeaMPhis 中的数据。然后需要遍历数组,当然成本很高。

    我在下面包含的内容包含 3 个主要更改: 1) 我将文件中的初始数据直接构建到 MeaMPhi 的 MeaMPhi 集合中。 2)我将集合(即对实例 MeaMPhis 的引用)传递给窗口 PlotMPhi,然后将其沿同一链传递给 MVMMPhi。 3) 由于 (1)+(2),MVMMPhi 类不再需要从曲率 [],moment[] 复制到 MeaMPhi 的集合。它自己的 MeaMPhis 直接指向(引用)该集合,该集合已由 Button_click 填充,并通过 ref 简单地传递给 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() 类。

    【讨论】:

    • 感谢您的回答,我刚刚尝试过,它适用于这种情况。我的想法是将所有输入参数存储在一个类中,因为其他类稍后会使用它们。再次感谢,
    • @user3170073。最后我得出一个结论,没有不需要的层。当我尝试删除一层(通过将 MeaMphis 集合直接包含在 PlotMPhi 中)时,我无法让数据绑定/数据上下文工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 2013-07-24
    相关资源
    最近更新 更多