简体   繁体   中英

Running code after xaml page close

I'm writing a music player app for WP8 in XAML and C#, but coming from a winforms background I don't know how to do the following.

My main page opens a separate page to display the tracks of a music album. The user can select some of these tracks which then get added to a central playlist which is working fine, but I want the main page to call its playlist refresh function when the song selection page is closed.

If I was doing this as winforms I would do something like:

    private void ShowAlbumPage(Int16 albumId)
    {
        albumPage.albumId = albumId;
        albumPage.ShowDialog();
        RefreshPlaylist();
    }

But this will not work for XAML

I currently have this:

    private void ShowAlbumPage(Int16 albumId)
    {
        NavigationService.Navigate(new Uri("/AlbumPage.xaml?albumId=" + albumId.ToString(), UriKind.Relative));
    }

Any suggestions on how and when to call RefreshPlaylist?

Its a vague idea ,but i think if i share i can also improve. You could pass the List to the AlbumPage (Current collection) , if user add a song to playlist , add this to the collection , when returning just send back the collection and update the Home Page.

 // MainPage.Xaml
private void list_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    SongsList selectedItemData = SelectedItem as SongsList ;
    if(selectedItemData != null)
    {
       NavigationService.Navigate(new Uri(string.Format("/AlbumPage.xaml?parameter={0}",selectedItemData.ID ), UriKind.Relative));
    }
}

//AlbumPAge.Xaml

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string parameter = this.NavigationContext.QueryString["parameter"];

SongsList country = null;

 // GETS THE SONG COLLECTION HERE , UPDATE WHEN USER ADD TO PLAYLIST , AND RETURN BACK.
}


**If your doing on MVVM Way .This is just an Idea not tested.** 

There is view-model it contains Collection of Songs, Maintain the same view-model over the two pages , Update the Song collection so Mainpage UI will automatically gets updated.

ViewModel Maintain the same over the Album Page.

ObservableCollection<Songs> _songs=new ObservableCollection<Songs>();
_songs.Add(new songs{Artist="ArtistName",Album="AlbumName"});

// AlbumPage CodeBehind.

private AddSongtoPlaylist(Song currentSong)
{
  _songs.Add(currentSong);
}

Reference

Some of the ways you could accomplish this:

  • Add the newly selected songs on the Application level on the PhoneApplicationService.State Dictionary. Whenever you return to the main page, load the transient state that will include the added songs.
  • Save them on a variable on the Application level and whenever navigating to the main page load the playlist.
  • Store them in the IsolatedStorage and load them whenever you navigate to the main page.
  • Pass them back to your main page through the navigation QueryString .

The method you choose is just a matter of personal preference.

I think I have found a reasonably simple workaround - I have just put a call to RefreshPlayQueue() in the OnNavigatedTo method of MainPage.xaml. This seems to work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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