关于C# wpf Frame 一次加载多个Page 的问题
问题描述:
首先,本人C#,wpf是初学者,也不是专门搞编程的,只是一直很感兴趣,恰好公司又有相关项目,所以就负责编程的工作。
项目中需要同时加载3个Page到Frame中,一开始我用Frame.Navigate,逐个加载,发现只有最后一个page加载上了,之前的导航里没有。经过一天的上网查询、思考,终于找到了解决方法。直接上代码吧:
主窗体代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
Page1 a;
Page2 b;
Page3 c;
ArrayList abc;
int i;
public MainWindow()
{
InitializeComponent();
a=new Page1();
b = new Page2();
c = new Page3();
abc = new ArrayList();
i = 0;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void button1_Click(object sender, RoutedEventArgs e)
{
abc.Add(a);
abc.Add(b);
abc.Add(c);
frame1.Navigate(abc[0]);
frame1.NavigationService.LoadCompleted += this.aaaa;
}
private void aaaa(object sender, NavigationEventArgs e)
{
i++;
if (i == abc.Count)
{
frame1.NavigationService.LoadCompleted -= this.aaaa;
while (frame1.NavigationService.CanGoBack)
{
frame1.NavigationService.GoBack();
}
return;
}
frame1.Navigate(abc[i]);
}
}
}