【问题标题】:Silverlight - Sending variables between the frameSilverlight - 在帧之间发送变量
【发布时间】:2010-01-07 03:20:00
【问题描述】:

我的 Silverlight 项目中有一个主页。我在这个页面中有两个框架。一个称为内容,另一个称为页脚。我想知道的是,如何更改内容中的内容变量是否是页脚中的点击事件?

在 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>

在 /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>

在 /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>

我想要的是,当您单击页脚中的“播放”按钮来播放器时,媒体元素会在“内容”中播放。

player.Play()

我编写的程序是用 VB.NET 编写的。但是可以用c#的例子也被接受了,非常感谢!

【问题讨论】:

    标签: .net vb.net silverlight frame


    【解决方案1】:

    您可以将播放器存储在这样的静态字段中:

    public class DataClass
    {
        public static MediaElement player;
    }
    

    这样您就可以从任何您喜欢的地方开始播放:

    DataClass.player.Play();
    

    【讨论】:

      【解决方案2】:

      通过导航框架处理这个问题的难点在于为被导航到的各种用户控件获取适当的句柄。我的建议是处理各种导航控件上的 Navigated 事件,然后从 NavigationEventArgs 的 Content 属性中获取对所需控件的引用。这样,您就可以在相关用户控件的适当实例上调用所需的任何方法。

      我在回复this question 时采用了类似的方法。

      当然,您可能还需要考虑您是否真的需要针对此特定解决方案的导航框架的额外复杂性。至少乍一看,我不清楚为什么您实际上需要导航到这些特定控件。可以直接在 MainPage.xaml 中托管它们,然后您可以使用 FindName() 轻松获取它们。

      【讨论】:

      • 我本可以在课程中使用 URI 设置哈希,例如 #play=true。所以开始播放器在第二帧播放 = true 而不是 false。但我不希望它在 URI 中可见。
      • 看来是Find Control()。不在 Silverlight 运行时中
      • 糟糕,抱歉,应该是“FindName()”。以上更新。
      猜你喜欢
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      相关资源
      最近更新 更多