在 UWP 中的页面之间传递一些参数 [英] Pass some parameters between pages in 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 (Phone) 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屋!