c# - 在 Windows C# 之间传递对象

标签 c# xaml

我的任务是必须使用 的项目创建从文件中提取对象列表的表单,然后能够将列表传递给另一个窗口。

public class Food
{
    public string Name;
    public string Category;
    public int Price;
}

public class Ingredient
{
    public string Name;
    public string Category;
    public decimal PricePerUnit;
    public decimal Quantity;

    public Ingredient(string pName, string pCategory, decimal pPricePerUnit, decimal pQuantity)
    {
        Name = pName;
        Category = pCategory;
        PricePerUnit = pPricePerUnit;
        Quantity = pQuantity;
    }
}

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        List<Ingredient> Inventory = CallInventoryFile();
    }



    private void inventoryButton_Click(object sender, RoutedEventArgs e)
    {
        InventoryWindow wnd = new InventoryWindow();
        wnd.ShowDialog();
    }

    public List<Ingredient> CallInventoryFile()
    {

        List<Ingredient> ProcessedInventory = new List<Ingredient>();

        try
        {
            string[] fileLines = File.ReadAllLines("Inventory.txt");


            //Reading in the file
            for (int i = 0; i < fileLines.Length; i++)
            {
                string[] CurrentLine = fileLines[i].Split(',');
                string Name = CurrentLine[0].Trim();
                string Category = CurrentLine[1].Trim();
                decimal PricePerUnit = decimal.Parse(CurrentLine[2].Trim());
                decimal Quantity = decimal.Parse(CurrentLine[3].Trim());
                Ingredient IngredientToAdd = new Ingredient(Name, Category, PricePerUnit, Quantity);
                ProcessedInventory.Add(IngredientToAdd);
            }
            return ProcessedInventory;
        }
        catch
        {
            //if we get here read in failed
            MessageBox.Show("There was an error reading in the file");
            return ProcessedInventory;
        }
    }

然后我必须移动到这个窗口

    public InventoryWindow()
    {
        InitializeComponent();

        categoryComboBox.Items.Add("All");
        categoryComboBox.Items.Add("Pizza");
        categoryComboBox.Items.Add("Burger");
        categoryComboBox.Items.Add("Sundry");
        categoryComboBox.SelectedValue = "All";
    }

    private void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }

    private void categoryComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }
}

我的问题是如何将 Inventory 的结果从 MainWindow 传递到 InventoryWindow

最佳答案

你可以直接传入构造函数,

  InventoryWindow wnd = new InventoryWindow(Inventory);
  wnd.ShowDialog();

然后,

public InventoryWindow(List<Ingredient> inputList)
{
}

关于c# - 在 Windows C# 之间传递对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43908775/

相关文章:

c# - Moq - 如何在循环中调用具有不同参数的相同设置

c# - RibbonGallery 项目命令单击

c# - 将背景设置为 Canvas

c# - 将 id 值传递给 gridview 控件中的 LinkBut​​ton 服务器端事件

c# - UWP 动画重置为 From 状态

c# - 在 VS17 中模拟时,无法让简单的主/详细模式 Xamarin.Forms 应用程序工作

c# - 在mef中卸载一个dll文件

c# - MVVM使用代码隐藏或viewmodel操作 View

c# - 我们可以在 WPF XAML 中为多个按钮创建单个 Storyboard吗?

c# - 转换器不能应用于需要 IValueConverter 类型的属性