导航动态创建的页面

时间:2016-08-16 17:41:11

标签: c# xaml

我有一个小问题导航到动态创建的页面(使用C#和XAML(visual studio 2015版)。 我可以创建页面(我假设),但是当我导航到它们时,它会给我原始页面。

我想这可能与测试/ test2的GetType有关,它指向Dynamic_Page_1页面(此页面上没有任何内容),但是如何强制它使用我是的新实例改性?

            // Dynamic Page One
            Dynamic_Page_1 test = new Dynamic_Page_1();
            Button b = new Button { Content = "DYNAMIC TEST ONE", FontSize = 22, FontWeight = FontWeights.Bold };
            test.Stack().Children.Add(b);

            // Dynamic Page Two
            Dynamic_Page_1 test2 = new Dynamic_Page_1();
            TextBlock t1 = new TextBlock {Text = "DYNAMIC TEST TWO" };
            test2.Stack().Children.Add(t1);

            // Dynamically create buttons which link to both Dynamically created pages
            Button b3 = new Button { Content = "GO TO TEST ONE", };
            Button b4 = new Button { Content = "GO TO TEST TWO", };

            b3.Click += (s, e) => {
                Frame.Navigate(test.GetType());
            };
            b4.Click += (s, e) =>
            {
                Frame.Navigate(test2.GetType());
            };

            stackPanel1.Children.Add(b3);
            stackPanel1.Children.Add(b4);

1 个答案:

答案 0 :(得分:0)

我最后通过stackpanel修复了这个问题。

Frame.Navigate(typeof(Dynamic_Page_1), stackPanel1);

然后修改navigateTo部分以查找它。

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    StackPanel stack = (StackPanel) e.Parameter;
    // Do something with stackpanel.
}