views:

341

answers:

3

I have one main page in my Silverlight project. I have two frame inside this page. The one called Contents and the other called Footer. What I wonder is, how can I change the variables in the content in the Content of whether that's a click event in the Footer?

In Mainpage.xaml:

<UserControl
    x:Class="SilverlightApplication10.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation" 
    xmlns:uriMapper="clr-namespace:System.Windows.Navigation;assembly=System.Windows.Controls.Navigation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">



        <Grid x:Name="NavigationGrid" >

            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
        <navigation:Frame Grid.Row="1" x:Name="Contents" 
                              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>

        <StackPanel Grid.Row="2" Background="Silver" Width="528">
            <navigation:Frame Grid.Row="1" x:Name="Footer" 
                              Source="/Home" Navigated="ContentFrame_Navigated" NavigationFailed="ContentFrame_NavigationFailed">
                <navigation:Frame.UriMapper>
                    <uriMapper:UriMapper>
                        <uriMapper:UriMapping Uri="" MappedUri="/Footer/Home.xaml"/>
                        <uriMapper:UriMapping  Uri="/{pageName}" MappedUri="/Footer/{pageName}.xaml"/>
                    </uriMapper:UriMapper>
                </navigation:Frame.UriMapper>
            </navigation:Frame>
        </StackPanel>
    </Grid>

</UserControl>

In /Views/Page2.xaml:

<navigation:Page x:Class="PodcastPlayer.Page2"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable="d"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           d:DesignWidth="640" d:DesignHeight="480"
           Title="Page2 Page">
  <Grid x:Name="LayoutRoot">
        <MediaElement x:Name="player"   />
    </Grid>
</navigation:Page>

In /Footer/Page2.xaml:

<navigation:Page x:Class="PodcastPlayer.Page2Fotter"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
           mc:Ignorable="d"
           xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
           d:DesignWidth="640" d:DesignHeight="480"
           Title="Page2 Page">
  <Grid x:Name="LayoutRoot">
        <StackPanel  Orientation="Horizontal" >
            <StackPanel Height="30" Width="60" Background="Red">
                <TextBlock Padding="10" Foreground="White">Back</TextBlock>
            </StackPanel>

            <StackPanel Height="30" Width="60" Margin="10,0,0,0" Background="DarkGreen">
                <TextBlock x:Name="playtext" Padding="10" Foreground="White">Play</TextBlock>
            </StackPanel>

            <StackPanel Height="30" Width="60"  Margin="10,0,0,0" Background="Red">
                <TextBlock Padding="10" Foreground="White">Next</TextBlock>
             </StackPanel>   
        </StackPanel>
    </Grid>
</navigation:Page>

What I want is that when you click the Play button in the footer to player elimente that Media Element play in Contents.

player.Play()

The program I write is in VB.NET. But examples can with c# are also accepted with great thanks!

+1  A: 

The hard part about handling this through the navigation framework is getting the appropriate handle to the various user controls that are being navigated to. My recommendation would be to handle the Navigated event on the various navigation controls, and then to grab the reference to the control you want from the Content property of the NavigationEventArgs. That way, you can call whatever method you need on the appropriate instance of the user control in question.

I took a similar approach in my answer to this question.

Of course, you may also want to consider whether you really need the additional complexities of the navigation framework for this particular solution. At least at first glance, I'm not clear why you actually need to navigate to these particular controls. It may be possible to just host them directly within the MainPage.xaml, and then you can get them pretty easily with FindName().

Ken Smith
What I could have done had the course and use the URI to set a hash, for example #play=true. And so began the player in the second frame to play = true and not false. But I do not want it to be visible in the URI.
sv88erik
It appears that the Find Control (). Is not in the Silverlight runtime
sv88erik
Ooops, sorry, that should have been "FindName()". Updated above.
Ken Smith
+1  A: 

You could store player in some static field like that:

public class DataClass
{
    public static MediaElement player;
}

This way you would be able to start the playback from anywhere you like:

DataClass.player.Play();
Fedor
Thank you! Smart and good solution!
sv88erik
+1  A: 

Take a look at this. Hope this can help

Hameds