代码之家  ›  专栏  ›  技术社区  ›  user370446

如何为我的WPF应用程序制作背景音乐?

  •  1
  • user370446  · 技术社区  · 16 年前

    我想我得用媒介元素做点什么。用我的故事板启动或停止mediaelement。

    但是如何处理我的故事板中的媒体元素呢?

    1 回复  |  直到 7 年前
        1
  •  1
  •   astonish    16 年前

    你的想法是对的:

    • 在详细信息页放入的任何布局容器(网格、边框等)中使用MediaElement

      <MediaElement x:Name="myMediaElement" />

    • 让你的故事板资源中有一个MediaTimeline

      <Window.Resources>
      <Storyboard x:Key="PlaySoundStoryboard">
          <MediaTimeline Storyboard.TargetName="myMediaElement" Source="whatever.mp3" />
      </Storyboard>
      

    • 我知道通过按钮和其他东西,你可以使用事件触发器来启动和停止故事板:

          <Grid x:Name="LayoutRoot">
          <Grid.Triggers>
              <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="playbutton">
                  <BeginStoryboard Storyboard="{StaticResource PlaySoundStoryboard}" Name="theStoryboard" />
              </EventTrigger>
              <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="stopbutton">
                  <StopStoryboard BeginStoryboardName="theStoryboard" />
              </EventTrigger>
          </Grid.Triggers>
          <MediaElement x:Name="myMediaElement" />
          <Button Name="playbutton">play</Button>
          <Button Name="stopbutton">stop</Button>
      </Grid>
      

    但我不知道你会怎么做才能看到集装箱。IsVisibleChanged将要求您查看在XAML中无法执行的参数(据我所知)。您可能需要使用代码隐藏来触发动画。