更改帧源 Wpf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32066115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:20:51  来源:igfitidea点击:

Change Frame Source Wpf

c#wpfframe

提问by Kuriyama Mirai

How can I possibly change the source of a frame from other page when I click a button in an active page.

当我单击活动页面中的按钮时,我怎么可能从其他页面更改框架的来源。

What I'm doing is In page1.xaml, I have a frame with a source displaying page2.xaml.Once I click a button in page2.xaml, I want to update the source of frame in page1.xaml to page3.xaml and the frame should display page3.xaml instead of page2.xaml.

我正在做的是在 page1.xaml 中,我有一个源显示 page2.xaml 的框架。一旦我单击 page2.xaml 中的按钮,我想将 page1.xaml 中的框架源更新为 page3.xaml 和框架应显示 page3.xaml 而不是 page2.xaml。

As of now, I tried using

截至目前,我尝试使用

page1 pg1 = new page1();
pg1.frame.source = new Uri("page3.xaml",UriKind.Relative);

But it didn't display page3.xaml in the frame in page1.xaml.

但是它没有在page1.xaml的frame中显示page3.xaml。

I also tried

我也试过

page1 pg1 = new page1();
pg1.frame.Navigate(new Uri("page3.xaml",UriKind.Relative));

but didn't work as well, page2.xaml remains the display of the frame.

但效果不佳,page2.xaml 仍然是框架的显示。

回答by Liero

First of all, it is unusual to have frame inside page. Usually you have frame inside MainWindow or Usercontrol, because Page is meant to be hosted inside frame.

首先,页面内有框架是不寻常的。通常您在 MainWindow 或 Usercontrol 中有框架,因为 Page 旨在托管在框架内。

Second, you can't just create new Page1and use it's frame. This newly created Page1 exist only in memory and it is another instance that the one displayed.

其次,您不能只是创建新的Page1并使用它的框架。这个新创建的 Page1 只存在于内存中,它是显示的另一个实例。

Because it would be quite difficult to find the Frame from pages, there is NavigationServiceproperty in Page class.

因为要从页面中找到 Frame 非常困难,所以Page 类中有NavigationService属性。

NavigationService.Navigate(new Uri("page3.xaml",UriKind.Relative));