Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#跨页面共享listview项目_C#_List_Class_Model View Controller_Uwp - Fatal编程技术网

C#跨页面共享listview项目

C#跨页面共享listview项目,c#,list,class,model-view-controller,uwp,C#,List,Class,Model View Controller,Uwp,我是初学者 我在主页上有一个列表,通过在文本框中键入内容来添加项目。创建listview项目后,我希望能够单击项目并在另一个页面中查看其值 我为此创建了一个DetailsPage,但我不知道在这些页面上共享数据的最佳方式是什么 这是我目前在主页上看到的内容: 公共密封部分类主页面:第页 { 公共项目计数器 public MainPage() { this.InitializeComponent(); //This is the button that

我是初学者

我在主页上有一个列表,通过在文本框中键入内容来添加项目。创建listview项目后,我希望能够单击项目并在另一个页面中查看其值

我为此创建了一个DetailsPage,但我不知道在这些页面上共享数据的最佳方式是什么

这是我目前在主页上看到的内容: 公共密封部分类主页面:第页 { 公共项目计数器

    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实际上只有一个标签,我正试图用我在主页中单击的listview项目中的任何值填充该标签

最好的方法是什么? 希望这是有意义的


非常感谢。

您有三种方法:

  • 声明静态变量并将其设置为第一种形式,并在第二种形式(或任何地方)中使用
  • 在第二个表单类中创建公共字段/propeey,并从第一个表单设置它
  • 将值作为参数传递给第二类的构造函数/函数
  • 我在这里添加了第二个表单类:

    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我在上面添加了完整的代码,希望你能使用它