首先定义个主的xaml,把其中默认的grid删掉。
然后在App_start中,写入 new page1();
在page1的构造中把 this.content = ?,这里的?表示的是一个UserControl的集合。
最主要的是自己定一个类,用来存放按照 key值找到对应的UserContraol的对象信息,并保存程序启动时的page1对象。
这样在页面跳转时就可以获取到最初的这些个对象信息了。
namespace myTest { public class PageDict { protected static UserControl rootPage; protected static Dictionary<string, UserControl> _pageDict = new Dictionary<string, UserControl>(); public PageDict() { } public static void setRootPage(UserControl page) { rootPage = page; } public static UserControl getRootPage() { return rootPage; } public static UserControl popPage(string pageKey) { return (UserControl)(_pageDict[pageKey]); } public static Int16 pushPage(string key, UserControl userPage) { if (_pageDict.ContainsKey(key)) { System.Windows.MessageBox.Show("Already has this key."); return -1; } _pageDict.Add(key, userPage); return 0; } } } 在从一个页面跳转到另一个页面时:
private void hylink1_Click(object sender, RoutedEventArgs e) { myPage1 newPage = new myPage1(); PageDict.pushPage(Resource1.MainPageKey, this); PageDict.getRootPage().Content = newPage; } private void myBTN_Click(object sender, RoutedEventArgs e) { MainPage newPage = (MainPage)PageDict.popPage(Resource1.MainPageKey); if (newPage == null) { System.Windows.MessageBox.Show("we get a null page."); return; } PageDict.getRootPage().Content = newPage; }