Passing objects between classes in Windows Phone/C

2020-07-30 00:34发布

I'm new to Windows Phone and C#, enjoying the change from Objective-C and Java.

I cant find the way to pass an object from one class to another. I came across some sample code looking on MSDN but I tink that maybe its not applicable for what I need.

    private void meetingList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (meetingList.SelectedIndex != -1)
        {
            Meeting aMeeting = (Meeting)meetingList.SelectedItem;
            this.NavigationService.Navigate(new Uri("/MeetDetails.xaml", UriKind.Relative));
            ApplicationBar.IsVisible = true;
        }
    }

How can I pass my Meeting Object 'aMeeting' into my MeetDetails class so that I can display all the details to the user.

I know I can break it down, and pass in all the vars from the 'aMeeting' by using something like this:

this.NavigationService.Navigate(new Uri("/MeetDetails.xaml?Meeting=" +
            aMeeting.meetName + "&TheDate=" +
            aMeeting.meetDate, UriKind.Relative));

Is there something I've missed? Are there alternative ways you guys would recommend?

Many Thanks, -Code

3条回答
【Aperson】
2楼-- · 2020-07-30 00:48
         var t1 = App.Current as App;
         t1.SSIDToken = stData1SSID;
         t1.CSRFToken = stData1CSRF;

this works real good, just make the members u need in the app.cs file (here it was :

public string SSIDToken {get; set;} public string CSRFToken {get; set;}

Then create the top code to create a var to serve as temp buffer. If you want to get back the values use the same code :

     var t1 = App.Current as App;
     thisisatextbox.Text = t1.SSIDToken;
     thisisalsoatextbox.Text = t1.CSRFToken;

Further info ; http://www.eugenedotnet.com/2011/07/passing-values-between-windows-phone-7-pages-current-context-of-application/

EDIT: After a couple of months of experience, noticed you can add

public static new App Current
{
    get { return Application.Current as App; }
}

In the App.xaml (In the public class App) to be able to call upon App.Current without having to declare it every single time!

Now you can use App.Current.CSRFToken = "" || string CSRFTk = App.Current.CSRFToken;

查看更多
We Are One
3楼-- · 2020-07-30 01:02

What you've posted is a good way of transferring simple data about the place. However it becomes a pain when you have to pass a complex object between pages.

The recommended way is to use the MVVM pattern (from wikipedia and MSDN). This gives you a way to separate the View from everything else by making use of data binding. The best tutorials I have seen is to watch the videos on MSDN.

查看更多
爷的心禁止访问
4楼-- · 2020-07-30 01:06

You might want to consider a manager class with properties which could store your current Meeting object. This would then be set in your SelectionChanged event handler and then accessed in your MeetDetails page. The manager class is defined externally to your pages so that it can be accessed from all your pages.

查看更多
登录 后发表回答