在UWP页面之间传递一些参数 [英] Pass some parameters between pages in UWP

查看:114
本文介绍了在UWP页面之间传递一些参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将一些Windows Phone 8项目移植到当前的UWP,并陷入我在旧项目中使用的摘要代码中。

I try to port some Windows Phone 8 projects to current UWP, and get stucked in this snippet code that I've used in old project.

private void Restaurant_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    string types = "restaurant";
    string title = "restaurant";
    string url = string.Format("/NearbyPlaces.xaml?latitude={0}&longitude={1}&types={2}&title={3}", LocationLatitude.Text, LocationLangitude.Text, types, title);
    NavigationService.Navigate(new Uri(url, UriKind.Relative));
}

在该代码中,我使用NavigationService将一些参数传递给另一个页面。我无法再使用NaigationService,因为UWP不支持它。我曾在UWP项目中尝试使用此功能,但我认为它仅支持传递一个参数CMIIW。

In that code, I used NavigationService to pass some parameters to another page. I couldn't use NaigationService anymore because UWP doesn't support it. I've tried using this in my UWP project, but I think it only supported for passing one parameter, CMIIW.

private void restaurant_tapped(object sender, TappedRoutedEventArgs e)
{
    string types = "restaurant";
    string title = "restaurant";
    Frame.Navigate(typeof(placeResult), latLoc.Text, longLoc.Text, types, title);
}

该代码给我一个错误,因为它需要5个参数,即+ 2个重载。我的问题是如何以适当的方式在UWP项目中传递一些参数?

That code give me an error, because it takes 5 arguments, which is +2 overloads. My question is how to do in proper way to passing some parameters in UWP project?

推荐答案

您在Windows(电话)中传递的内容8只是一个简单的 string ,其中包含您的所有参数。您必须在目标页面的 OnNavigatedTo()方法中对其进行解析。当然,您仍然可以这样做,并将字符串传递给 Frame.Navigate()方法。

What you passed in Windows (Phone) 8 has just been a simple string that included all your parameters. You had to parse them in the OnNavigatedTo() method of your target page. Of course you can still do that and pass a string to the Frame.Navigate() method.

UWP可以将完整的对象传递给其他页面。那么,为什么不创建一个包含所有参数的小类并传递其实例呢?

But since UWP you can pass complete objects to other pages. So why don't you create a small class that includes all your parameters and pass an instance of that?

您的类可能看起来像:

public class RestaurantParams
{
    public RestaurantParams(){}
    public string Name { get; set; }
    public string Text { get; set; }
    // ...
}

,然后通过:

var parameters = new RestaurantParams();
parameters.Name = "Lorem ipsum";
parameters.Text = "Dolor sit amet.";
// ...

Frame.Navigate(typeof(PageTwo), parameters);

在下一页上,您现在可以通过以下方式访问它们:

On your next page you can now access them via:

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

    var parameters = (RestaurantParams)e.Parameter;

    // parameters.Name
    // parameters.Text
    // ...
}

其中Parameter是检索参数的函数。

Where Parameter is the function that retrieves the arguments.

希望有帮助。

这篇关于在UWP页面之间传递一些参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆