zoukankan      html  css  js  c++  java
  • 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;

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

  • 相关阅读:
    安装MySQL56时,停止在start service这一步
    网站主机、域名购买和部署的方法
    sql修改数据库中表的结构
    放大电路的焊制实验
    win7下添加库文件出现“file is not regcognized”问题
    .net 网站中如何动态播放音乐,页面如何播放音乐
    LYF电子书制作工具(CHM格式)
    MVC创建
    ASP_Login
    智能搜索
  • 原文地址:https://www.cnblogs.com/wz2cool/p/2032150.html
Copyright © 2011-2022 走看看