如何传递的Windows Phone对象背景的项目? [英] How to pass an object to background project in Windows Phone?

查看:119
本文介绍了如何传递的Windows Phone对象背景的项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我在前台的 StorageFile 对象并希望在的 BackgroundMediaPlayer 是这样的:

My problem is that I have an StorageFile object in foreground and want to play it in BackgroundMediaPlayer like this:

mediaPlayer.SetFileSource(soundStorageFile);



但它不可能使用 SetFileSource()在前台,你应该把它在后台任务,或者在后台初始化第三个项目,并从那里调用它。

but it is not possible to use SetFileSource() in the foreground, you should call it in the background task, or initialize a third project in the background and call it from there.

所以,我怎么能传递反对背景的项目?

So how can I pass an object to the background project?

(这是一款Windows Phone运行的应用程序)

推荐答案

之间的 UI 的通信的 BackgroundMediaPlayer 的可以做的通过发送邮件

Communication between UI and BackgroundMediaPlayer can be done by sending messages:

一个简单的通信机制引发事件在前台和后台进程两者。每个SendMessageToForeground和SendMessageToBackground方法调用中的相应任务事件。 。数据可以作为参数传递给在接收任务的事件处理程序传递

A simple communication mechanism raises events in both the foreground and background processes. The SendMessageToForeground and SendMessageToBackground methods each invoke events in the corresponding task. Data can be passed as an argument to the event handler in the receiving task.

您使用的 SendMessageToBackground 通过的一个简单的目的是通过使用ValueSet 。一旦你把它发送到您的 BMP实例的,那么的 MessageReceivedFromForeground事件抬起并可以读取通过的对象的距离的 MediaPlayerDataReceivedEventArgs

You use SendMessageToBackground to pass a simple object by using ValueSet. Once you send it to your BMP Instance, then the MessageReceivedFromForeground event is raised and you can read your passed object from MediaPlayerDataReceivedEventArgs.

在你的情况,例如,你可以通过一个字符串的文件路径,您的播放器:

In your case, you can for example pass a string with a file path to your Player:

// the UI code - send from Foreground to Background
ValueSet message = new ValueSet();
message.Add("SetTrack", yourStorageFile.Path); // send path (string)
BackgroundMediaPlayer.SendMessageToBackground(message);



然后,正如我已经说过 - 相应的事件是Player实例提出的(应该是):

Then as I've said - the appropriate event is (should be) raised by Player instance:

private async void BMP_MessageReceivedFromForeground(object sender, MediaPlayerDataReceivedEventArgs e)
{
    foreach (string key in e.Data.Keys)
    {
        switch (key)
        {
            case "SetTrack":
                string passedPath = (string)e.Data.Values.FirstOrDefault();
                //here code you want to perform - change track/stop other 
                // that depends on your needs
                break;
     // rest of the code



我强烈建议阅读的提到在MSDN 概述,调试程序,看看它是如何工作的。

I strongly recommend to read the mentioned overview at MSDN, debug your program and see how it works.

在另一方面,如果你只想设置从文件中记录,你可以尝试这样的(你可以不设置FileSource在用户界面 - 这是真的,但你可以使用SetUriSource):

On the other hand if you want just to set track from file you can try like this (you can't set FileSource in UI - that's true, but you can use SetUriSource):

// for example playing the first file from MusicLibrary (I assume that Capabilities are set properly)
StorageFile file = (await KnownFolders.MusicLibrary.GetFilesAsync()).FirstOrDefault();
BackgroundMediaPlayer.Current.SetUriSource(new Uri(file.Path, UriKind.RelativeOrAbsolute));

这篇关于如何传递的Windows Phone对象背景的项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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