Frame内容页向Frame页传值的问题。

http://bbs.silverlightchina.net/forum.php?mod=redirect&tid=7503&goto=lastpost#lastpost

问题来自于银光中国的ayurep 提出来的,因为他在其他论坛上面很久没解决,我觉得可能还是写出来做个问题解答一个小记录。

他的问题是:怎样在MainPage 的Frame包含的页面Page做的更改中值传到MainPage本身中。

      是的一般我们Page和Page 导航是用Url 或者是代理,他当时也是从这两个方面去想问题的,没有实现在当前Page做更改的值传到MainPage中。

其实解决的方法还是很容易的,我们获得MainPage不就行了么,当然这是我一个人的想法,我想可能还有其他的解决方法,欢迎大家指出。

小例子:

目标:修改MainPage中的一个Page的一个TextBox,并把这个值传到我们获取到得MainPage中一个TextBox中

      我新建了一个Navigation App在Home.xaml中如下,用TextChanged来改变值触发事件。

复制代码
public partial class Home : Page
{
public Home()
{
InitializeComponent();
}

// Executes when the user navigates to this page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}

MainPage mainPage
= (MainPage)App.Current.RootVisual;
private void textBoxInPage_TextChanged(object sender, TextChangedEventArgs e)
{
mainPage.textBoxInMainPage.Text
= ((TextBox)sender).Text;
}
}
复制代码

xaml:

复制代码
<Grid x:Name="LayoutRoot">
<ScrollViewer x:Name="PageScrollViewer" Style="{StaticResource PageScrollViewerStyle}">

<StackPanel x:Name="ContentStackPanel">

<TextBlock x:Name="HeaderText" Style="{StaticResource HeaderTextStyle}"
Text
="Home"/>
<TextBlock x:Name="ContentText" Style="{StaticResource ContentTextStyle}"
Text
="Home page content"/>
<TextBox Height="23" Name="textBoxInPage" Width="120" TextChanged="textBoxInPage_TextChanged" />
</StackPanel>

</ScrollViewer>
</Grid>
复制代码

MainPage 很简单放了一个textBox

复制代码
<Grid x:Name="LayoutRoot" Style="{StaticResource LayoutRootGridStyle}">

<Border x:Name="ContentBorder" Style="{StaticResource ContentBorderStyle}">

<navigation:Frame x:Name="ContentFrame" Style="{StaticResource ContentFrameStyle}"
Source
="/Home" Navigated="ContentFrame_Navigated" NavigationFailed="ContentFrame_NavigationFailed">
<navigation:Frame.UriMapper>
<uriMapper:UriMapper>
<uriMapper:UriMapping Uri="" MappedUri="/Views/Home.xaml"/>
<uriMapper:UriMapping Uri="/{pageName}" MappedUri="/Views/{pageName}.xaml"/>
</uriMapper:UriMapper>
</navigation:Frame.UriMapper>
</navigation:Frame>
</Border>

<Grid x:Name="NavigationGrid" Style="{StaticResource NavigationGridStyle}">

<Border x:Name="BrandingBorder" Style="{StaticResource BrandingBorderStyle}">
<StackPanel x:Name="BrandingStackPanel" Style="{StaticResource BrandingStackPanelStyle}">

<ContentControl Style="{StaticResource LogoIcon}"/>
<TextBlock x:Name="ApplicationNameTextBlock" Style="{StaticResource ApplicationNameStyle}"
Text
="Application Name"/>

</StackPanel>
</Border>

<Border x:Name="LinksBorder" Style="{StaticResource LinksBorderStyle}">
<StackPanel x:Name="LinksStackPanel" Style="{StaticResource LinksStackPanelStyle}">

<HyperlinkButton x:Name="Link1" Style="{StaticResource LinkStyle}"
NavigateUri
="/Home" TargetName="ContentFrame" Content="home"/>

<Rectangle x:Name="Divider1" Style="{StaticResource DividerStyle}"/>

<HyperlinkButton x:Name="Link2" Style="{StaticResource LinkStyle}"
NavigateUri
="/About" TargetName="ContentFrame" Content="about"/>

</StackPanel>
</Border>
<TextBox Height="23" HorizontalAlignment="Left" Margin="293,13,0,0" Name="textBoxInMainPage" VerticalAlignment="Top" Width="120" />
</Grid>

</Grid>
复制代码
MainPage mainPage = (MainPage)App.Current.RootVisual; 直接就访问到了MainPage 然后得到textBoxInMainPage然后赋值,
是不是很简单啊~

PS:这里可能要注意一下,就是其实这个要看你的App.xaml 是怎么写的 ,这里我的RootVisual就是MainPage,
但是有些是MainPage放到了
BusyIndicator 要做个转换的。
比如ayurep 的例子 我们看他的App.xaml.
复制代码
private void Application_Startup(object sender, StartupEventArgs e)
{
// 这使您可以将 XAML 文件中的控件绑定到 WebContext.Current 属性。
this.Resources.Add("WebContext", WebContext.Current);

// 如果使用 Windows 身份验证,或如果用户在上次登录尝试时选择了“使我保持登录状态”,则会自动对用户进行身份验证。
WebContext.Current.Authentication.LoadUser(this.Application_UserLoaded, null);

// 在执行 LoadUser 期间向用户显示一些 UI
//this.InitializeRootVisual();

if (!Application.Current.IsRunningOutOfBrowser)
this.InitializeRootVisual();
else
this.RootVisual = new OutofBrowserMainPage();
}
protected virtual void InitializeRootVisual()
{
AyurepSET.Controls.BusyIndicator busyIndicator
= new AyurepSET.Controls.BusyIndicator();
busyIndicator.Content
= new MainPage();
busyIndicator.HorizontalContentAlignment
= HorizontalAlignment.Stretch;
busyIndicator.VerticalContentAlignment
= VerticalAlignment.Stretch;

this.RootVisual = busyIndicator;
//this.RootVisual = new x();
}
复制代码

这里明显就是放到BusyIndicator 中,所以我们要这样获得MainPage:

var busy = (AyurepSET.Controls.BusyIndicator)Application.Current.RootVisual;
var mainPage=(MainPage)busy.Content;

就是这一点小小的修改就可以咯 ,希望对大家有所帮助。

posted @   wz2cool  阅读(1101)  评论(2编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示