简体   繁体   English

ResourceDictionary Windows 8 Metro应用程序中的MediaElement

[英]MediaElement in a ResourceDictionary Windows 8 Metro App

I'm creating a windows 8 metro styled app for the windows store. 我正在为Windows商店创建Windows 8 Metro风格的应用程序。

I've got a resource dictionary (which has "code behind") which is merged in app.xaml as 我有一个资源字典(里面有“代码”),它在app.xaml中合并为

<Application.Resources>
    <ResourceDictionary>     
        <ResourceDictionary.MergedDictionaries>
            <view:TemplatesDictionary />   
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

I'm using the dictionary to access templates/color brushes and swap them out during runtime based on properties/states changing. 我正在使用字典访问模板/颜色笔刷,并在运行时根据属性/状态更改将它们换出。

return App.Current.Resources["SomeKey"] as Windows.UI.Xaml.Media.SolidColorBrush;

or whichever type I need. 或我需要的任何类型。

This has been working fine with pretty much anything so far, however I've run into issues when trying to do the same thing with MediaElements. 到目前为止,这几乎可以正常运行,但是在尝试使用MediaElements进行相同操作时遇到了问题。

I've got 我有

<MediaElement x:Key="SFX_ButtonSound" AutoPlay="False" Source="ms-appx:///Assets/Sound/UI Click Random.wav" Volume="1" />

then in code I've tried 然后在我尝试过的代码中

(App.Current.Resources["SFX_ButtonSound"] as MediaElement).Play();

and nothing plays but it can access and find the key fine. 没有任何作用,但是它可以访问并找到正确的密钥。

However, if I copy paste the media element into a page, changed x:key to x:name then 但是,如果我将媒体元素复制粘贴到页面中,则将x:key更改为x:name,然后

SFX_ButtonSound.Play();

It will play fine, can someone explain to me why this occurs? 一切都会好起来的,有人可以向我解释为什么会这样吗? I'm assuming it doesn't function because an instance of the media element on the page hasn't been "created" or something like that. 我假设它不起作用,因为尚未“创建”页面上的media元素实例或类似的东西。

I've not done much WPF/Silver-light development/UI dev. 我没有做太多的WPF / Silver-light开发/ UI开发。 I mainly focus in just backend dev. 我主要专注于后端开发。

Edit : So in case anyone was wondering, the reason why I attempted to approach it this way was so that i could, in code, manage whether or not volume was muted via in-app control as well as other various things and so that everything could be in one centralized "place" and so in code, when needed, i could simply call something like 编辑 :因此,如果有人想知道的话,我尝试以这种方式进行处理的原因是,我可以在代码中管理是否通过应用内控件以及其他各种方式使音量静音,从而使一切可以放在一个集中的“地方”,因此在代码中,当需要时,我可以简单地调用类似

Media.PlaySFX(string)

and it would apply the current settings/variables I needed at the time. 它将应用我当时需要的当前设置/变量。 What i ended up doing to achieve this was create a User control which simply contained all my generic media elements (button clicks, ambient sounds, etc). 我最终要做的是创建一个User控件,其中仅包含我所有的通用媒体元素(单击按钮,环境声音等)。

Then in each of my pages which required the sounds, I just created the control in the .resources area. 然后,在需要声音的每个页面中,我只是在.resources区域中创建了控件。

For the C# file for the user control it has a few functions to just play/stop/test logic. 对于用于用户控件的C#文件,它具有一些仅用于播放/停止/测试逻辑的功能。

I believe a MediaElement needs to be a part of visual tree to work, so indeed you need to add it somewhere. 我相信MediaElement必须成为视觉树的一部分才能起作用,因此确实需要将其添加到某个地方。 Maybe a Popup would work? 也许Popup会起作用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM