【问题标题】:C# Share listview items across pagesC# 跨页面共享列表视图项
【发布时间】:2017-05-03 02:16:36
【问题描述】:

这里是初学者。

我的 MainPage 上有一个列表,我通过在提交它的文本框中输入内容来添加项目。创建列表视图项目后,我希望能够单击一个项目并在另一个页面中查看其值。

我为此创建了一个详细信息页面,但我不知道在这些页面之间共享该数据的最佳方式是什么。

到目前为止,这是我在 MainPage 中的内容: 公共密封部分类 MainPage : Page { public int itemCounter;

    public MainPage()
    {
        this.InitializeComponent();


    //This is the button that submits the entered text

    public void btnSubmitInPopup_Click_1(object sender, RoutedEventArgs e)
    {
        //Create new listview item and textblock
        ListViewItem item = new ListViewItem();
        TextBlock txtBloodSugarValue = new TextBlock();


        //Save text input value to label and add item to list
        txtBloodSugarValue.Text = txtInput.Text;
        item.Content = txtBloodSugarValue;
        mainListView.Items.Add(item);


        //Reset text input field
        txtInput.Text = "";

        //Update counter of items in list
        itemCounter = mainListView.Items.Count();
        lblItemCounter.Text = itemCounter.ToString();


    }
}

我的 DetailPage 实际上只有一个标签,我试图用我在 MainPage 中单击的列表视图项中的任何值填充该标签。

最好的方法是什么? 希望这是有道理的。

非常感谢。

【问题讨论】:

    标签: c# list class model-view-controller uwp


    【解决方案1】:

    你有 3 种方法可以做到这一点:

    1. 声明静态变量并将其设置为第一种形式并在第二种形式(或任何地方!)中使用!
    2. 在第二个表单类中创建公共字段/propeey 并从第一个表单设置它
    3. 将值作为参数传递给第二类的构造函数/函数

    我在这里添加第二个表单类:

    public static int StaticVariable { get; set; }//First Method
    public int PublicProperty { get; set; }
    
    public Form2(int Value)
    {
        InitializeComponent();
        //Do your code here with constructor way here
    }
    public Form2()
    {
        InitializeComponent();
    }
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        //Do your code for 1,2 here
    }
    
    public void SetValueWithFunction(int value)
    {
        //Do your code for setting value with second type in Number 3
    }
    

    我希望这会有所帮助:)

    【讨论】:

    • 感谢您的回复,我用代码回复了您上面的评论。
    • @Liz4112 我已经在上面添加了完整的代码,我希望你可以使用它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多