如何为我的WPF应用程序做背景音乐? [英] How to do background music for my WPF-Application?

查看:410
本文介绍了如何为我的WPF应用程序做背景音乐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MainWindow中有一个带有3个不同UserControl的WPF应用程序,当时只有其中一个可见.就像有3个不同的页面,您可以根据需要从一个页面切换到另一页面.第一页就像一个开始屏幕.其次就像一般观点".第三页显示详细信息.使用Storyboard可以在它们之间进行切换.我只是使可见页面不可见(不透明度为零),然后将其移出可见窗口区域,然后将新页面移至可见窗口区域并使之可见.到目前为止一切顺利...(希望您理解我想告诉的内容^^)

I have an WPF-Application with 3 different UserControls in the MainWindow and only one of these is visible at the time. It's like having 3 different pages and you able to switch from one page to another when you like it. First page is like a start-screen. Second is like the "general view". And the third page shows details. Switching between them works fine with Storyboard. I just make the visible page invisible (opacity to zero) and move it out of the visible window- area and move the new page into the visible window-area and make it visible. So far so good... (Hope you understood what I wanted to tell^^)

现在,我想在显示详细信息页面时播放背景音乐.只有这样.当用户在普通视图中查看开始屏幕时,情况并非如此.

Now I want to have background-music that plays, just when the detail-page is displayed. And only then. Not when the user looks at the startscreen ir the general view.

我认为我必须在调解上有所作为.并通过我的故事板开始或停止调解.

I think I have to make something with a mediaelement. And start or stop the mediaelement with my storyboards.

但是如何处理情节提要中的媒体元素?

But how do I handle the mediaelements in my Storyboards?

推荐答案

您有正确的主意:

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

  • Use a MediaElement in whatever layout container (grid, border, whatever) your details page is put in

<MediaElement x:Name="myMediaElement" />

制作包含MediaTimeline的情节提要资源

Make your storyboard resource that has a MediaTimeline in it

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

我知道按钮和事情都可以使用事件触发器来启动和停止故事板:

I know with buttons and things you can use event triggers to start and stop the story board:

    <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中无法执行的参数(据我所知).您可能需要使用后面的代码来触发动画.

But I don't know how you would do this for the visibility of the container. IsVisibleChanged would require you to look at the argument which you can't do in XAML (to my knowledge). You may need to the trigger the animation using a code behind.

希望这对您有帮助...

Hope this helps...

这篇关于如何为我的WPF应用程序做背景音乐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆