简体   繁体   English

WP7在页面之间传递数据

[英]WP7 Passing data between pages

The basic navigation functionality of my app consists of selecting an item from a listbox, determine what item was selected and get its id value. 我的应用程序的基本导航功能包括从列表框中选择一个项目,确定选择了哪个项目并获取其id值。 Pass the id value to the next page and add it to an api url for use in a webclient to pull down new data. id值传递到下一页,并将其添加到api网址中,以供网络客户端使用以提取新数据。 Here's what I have now. 这就是我现在所拥有的。

Determine item selected and navigate to secondary page. 确定选择的项目并导航到第二页。

    public void genreSelectedHandler(object sender, RoutedEventArgs e)
    {
        ResultGenre data = (sender as TextBlock).DataContext as ResultGenre;
        ListBoxItem pressedItem = this.listGenres.ItemContainerGenerator.ContainerFromItem(data) as ListBoxItem;
        if (pressedItem != null)
        {
            string genreID = "http://api.beatport.com/catalog/3/beatport/genre?id=" + data.id;

            this.NavigationService.Navigate(new Uri("/Pages/GenrePage.xaml?genreID=" + genreID, UriKind.Relative));
        }
    }

On the secondary page i can use OnNavigatedTo to get the url from the previous page, but the part i'm stuck on is plugging that url into the webclient. 在辅助页面上,我可以使用OnNavigatedTo从上一页获取url,但是我遇到的问题是将该URL插入到webclient中。

    public GenrePage()
    {
        InitializeComponent();

        // WebClient jsonRelease
        WebClient jsonGenres = new WebClient();
        Uri apiGenre = new Uri("URL from previous page goes here");
        jsonGenres.DownloadStringCompleted += new DownloadStringCompletedEventHandler(jsonGenres_GetDataCompleted);
        jsonGenres.DownloadStringAsync(apiGenre);

    }

    // Textblock data from main page
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        string genreID = "";
        if (NavigationContext.QueryString.TryGetValue("genreID", out genreID))

        base.OnNavigatedTo(e);
    }

Seems like the only thing i can do is add the url to an existing control on the secondary page. 似乎我唯一能做的就是将URL添加到辅助页面上的现有控件。 Is there a better way to accomplish what I'm trying to do? 有没有更好的方法来完成我要尝试的工作? I've read a little bit on MVVM framework. 我已经阅读了一些MVVM框架。 Would that be a better way to handle this? 那会是解决这个问题的更好方法吗? If so how would that work? 如果是这样,那将如何工作? Thanks for the help. 谢谢您的帮助。

Does this part of the Uri "http://api.beatport.com/catalog/3/beatport/genre?id=" ever change? Uri "http://api.beatport.com/catalog/3/beatport/genre?id="这一部分是否有变化? If it doesn't, instead of passing the whole thing to GenrePage consider only passing data.id in the query string. 如果不是这样, GenrePage整个内容传递给GenrePage ,而GenrePage考虑仅在查询字符串中传递data.id Then create the Uri on the destination page. 然后在目标页面上创建Uri。

If you must do it the way you've shown in your snippet, do the following: 如果您必须按照摘要中显示的方式进行操作,请执行以下操作:

string genreID = @"http://api.beatport.com/catalog/3/beatport/genre?id=" + data.id;
this.NavigationService.Navigate( new Uri( "/Pages/GenrePage.xaml?genreID=" + 
  Uri.EscapeDataString( genreID ), UriKind.Relative ) );

In GenrePage.xaml.cs 在GenrePage.xaml.cs中

public partial class GenrePage : PhoneApplicationPage
{
  public GenrePage()
  {
    InitializeComponent();
  }

  protected override void OnNavigatedTo( NavigationEventArgs e )
  {
    base.OnNavigatedTo( e );

    string genreID = String.Empty;
    if( NavigationContext.QueryString.TryGetValue( "genreID", out genreID ) ) {
      LaunchWebClient( genreID );
    }
  }

  private void LaunchWebClient( string genreID )
  {
    WebClient jsonGenres = new WebClient();
    Uri apiGenre = new Uri( genreID );

    jsonGenres.DownloadStringCompleted += new 
      DownloadStringCompletedEventHandler( jsonGenres_GetDataCompleted );
    jsonGenres.DownloadStringAsync( apiGenre );
  }
}

Uri.EscapeDataString will insert the appropriate escape characters into the query string. Uri.EscapeDataString将适当的转义字符插入查询字符串。 The query string is automatically unescaped on the destination page, so you don't need to do anything special there. 查询字符串将自动在目标页面上转义,因此您无需在此进行任何特殊操作。

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

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