简体   繁体   English

在页面之间传递数据

[英]Passing data between pages

I'm sorry if this sounds like a completely stupid question but it really has me stumped!! 很抱歉,这听起来像是一个完全愚蠢的问题,但确实让我难过!

So application is made up of numerous pages that navigate from one to the other allowing users to manipulate data. 因此,应用程序由许多页面组成,这些页面从一个页面导航到另一个页面,从而允许用户操纵数据。 On the first page (PivotPage) the data is loading into a List that is defined in a class (bay9). 在第一页(PivotPage)上,数据正在加载到在类(bay9)中定义的列表中。 All this works well and I'm easily able to bind the data and manipulate it when its just on the one page. 所有这些都很好,并且我可以轻松地绑定数据并仅在一页上对其进行操作。 The issue is that i want the user to be able to manipulate or search against this 'list' from any other page. 问题是我希望用户能够从任何其他页面操纵或搜索此“列表”。

What i have is the basic class; 我所拥有的是基础课;

class bay9
{
    public override string ToString()
    {
        return "Name: " + SetZone + "   ID: " + SetThermocouple;
    }



    public string setzone;

    public string SetZone
    {
        get { return setzone; }
        set { setzone = value; }
    }

    public string setthermocouple;

    public string SetThermocouple
    {
        get { return setthermocouple; }
        set { setthermocouple = value; }
    }


    public string setdata;

    public string SetData
    {
        get { return setdata; }
        set { setdata = value; }
    }

}

The basic page that the data is entered in; 数据输入的基本页面;

public sealed partial class PivotPage : Page
{
    private NavigationHelper navigationHelper;
    private ObservableDictionary defaultViewModel = new ObservableDictionary();

    public PivotPage()
    {
        this.InitializeComponent();

        this.navigationHelper = new NavigationHelper(this);
        this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
        this.navigationHelper.SaveState += this.NavigationHelper_SaveState;

        Bay9 = new List<bay9>();


        AddBay9();
    }

    List<bay9> Bay9;
    public void AddBay9()
    {

        Bay9.Add(new bay9 { SetZone = "ZONE1C" });
        Bay9.Add(new bay9 { SetZone = "ZONE1A" });
        Bay9.Add(new bay9 { SetZone = "ZONE1R" });


        cvs9.Source = Bay9;
    }
}

and a page where i want to access the data that was inputted in the previous page but cant seem to do it? 和一个页面,我想访问上一页中输入的数据,但似乎无法做到这一点?

public sealed partial class RecorderPage : Page
{
    private NavigationHelper navigationHelper;
    private ObservableDictionary defaultViewModel = new ObservableDictionary();

    public RecorderPage()
    {
        this.InitializeComponent();

        this.navigationHelper = new NavigationHelper(this);
        this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
        this.navigationHelper.SaveState += this.NavigationHelper_SaveState;

        Bay9 = new List<bay9>();

        LoadThermocouples();
    }

    List<bay9> Bay9;



    private void NextPageButton(object sender, RoutedEventArgs e)
    {

        this.Frame.Navigate(typeof(MainPage));

    }





    void LoadThermocouples()
    {

        int i = 0;

        foreach (bay9 aPart in Bay9)
        {


            var textBlock = new TextBlock { Text = "header ", FontSize = 55 };

            var textBox = new TextBox { Name = i.ToString(), };

            ThermocoupleStackPanel.Children.Add(textBlock);

            ThermocoupleStackPanel.Children.Add(textBox);

            i++;
        }
    }
}

I believe the issue is with declaring Bay9 = new List<bay9>(); 我相信问题在于声明Bay9 = new List<bay9>(); again but don.t know how else i need to do it. 再次但不知道我还需要怎么做。 with it how it is now the list is empty. 现在列表是空的。

Please couple you help me out on how to access this List from this page :) 请帮助您帮助我了解如何从此页面访问此列表:)

Many Thanks 非常感谢

I think the typical way of dealing with this is storing Bay9 in a ViewModel class that is accessible from everywhere in the app. 我认为处理此问题的典型方法是将Bay9存储在ViewModel类中,该类可从应用程序中的任何位置访问。 See this Stack Overflow question and this article . 请参阅此堆栈溢出问题本文

In my own apps, I've used the technique in the linked Stack Overflow question - making it a static property on the App class, and that's worked fine for me. 在我自己的应用程序中,我在链接的堆栈溢出问题中使用了该技术-使它成为App类的静态属性,对我来说很好。

Try changing class bay9 to public static class bay9 . 尝试将class bay9更改为public static class bay9 That way it will become accessible from everywhere in the app. 这样,它将可以从应用程序中的任何位置进行访问。

Your best bet is to take a look at the design pattern that Microsoft recommends for developing Windows Phone applications called MVVM (model-view-viewmodel) which will allow you to create better binding with data across your application. 最好的选择是看看Microsoft为开发Windows Phone应用程序而建议的设计模式,称为MVVM(模型-视图-视图模型),这将使您能够与应用程序中的数据创建更好的绑定。

Here is a link to a tutorial provided through the MSDN: Implementing the Model-View-ViewModel pattern for Windows Phone 8 这是通过MSDN提供的教程的链接: 为Windows Phone 8实现Model-View-ViewModel模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM