0

我需要将一些参数或参数传递给 Page 的构造函数,看来我找不到办法。

这只是打开页面。如何将参数传递给 Page1 的构造函数?

 this.Frame.Navigate(typeof(Page1));


4

3 回答 3

2

As utterly strange as your question is, here's your answer. This is not a XAML question. This is simply a C# question. Creating a custom constructor on Page3 would be a simple as this:

public sealed partial class Page2 : Page
{
    public Page2()
    {
        this.InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        var p3 = new Page3(e.Parameter);
        // do something
        base.OnNavigatedTo(e);
    }
}

public sealed partial class Page3 : Page
{
    public Page3(object parameter)
        : base()
    {
        this.NavigationCacheMode = NavigationCacheMode.Required;
        this.InitializeComponent();
    }
}

Please note two things:

  1. I have never heard of anyone doing things like this. Having said that, it doesn't make it wrong. It just makes it very strange.
  2. Page 3 has NavigationCacheMode set to Required. If you intend to navigate to Page3 someday, you will need this set in order to guarantee the proper instance. Let me explain, when you Frame.Navigate(Type) you are navigating to a type, and not to an instance. That means your local instance of Page3 (as p3 in this case) isn't the resulting page you will actually navigate to. That is, unless, you set Cache mode like this. There is no option for Frame.Navigate(p3).

Good luck.

于 2013-11-19T22:45:59.720 回答
1

You have to override OnNavigatedTo handler event and get parameters from it. Read something about NavigationService, eg. http://developer.nokia.com/Community/Wiki/Passing_parameters_while_navigating_between_pages_on_Windows_Phone

于 2013-11-13T18:55:43.517 回答
0

通常您应该将 MVVM 用于 XAML 应用程序。在这种情况下,您应该将值设置为 ViewModel 并将其设置为您的页面的上下文。

于 2013-11-13T13:24:54.667 回答