积极答复者
后台音频代理的问题

问题
-
WP平台的好多音乐播放器的音乐列表都是动态改变的,比如说用户可以随时向播放列表中添加音乐。而且当用户选择播放其中的一首时,我猜测此时前台一定和后台音频代理取得了联系,更新了播放列表(或者是初始化了播放列表),请问这是如何做到的呢?
MSND上给的例子中播放列表是载入程序时就已经初始化好的,我尝试修改但是都无济于事,前台根本和后台无法产生联系,请问怎么办?
这是MSDN的例子
msdn.microsoft.com/zh-cn/library/hh202978- 已编辑 孟云帆 2014年8月12日 0:11
2014年8月12日 0:08
答案
-
你指的后台应该是指你应用程序的后台吧,并不是手机运行时候的后台吧。如果是这样你查找的资料就应该不是你想要的。
我写了个简单的例子希望对你有帮助,在后台你需要使用两个东西 一个是ObservableCollection 另一个是 INotifyPropertyChanged
在前台 你需要使用DataTemplate
其中ObservableCollection是指在容器元素改变时提供通知的容器 这个东西东西你可以查看
http://msdn.microsoft.com/zh-cn/library/ms668604(v=vs.110).aspx
其中INotifyPropertyChanged 是 元素属性内容改变时提供通知,可以查看
http://msdn.microsoft.com/zh-cn/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx
首先目录结构是这样的
其中SamleData类是自己实现的例子类
代码如下
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace Test_List.DataModel { public class SampleData:INotifyPropertyChanged { public SampleData() { Id = 0; title = "what"; } public int Id { get; set; } private string title; public string Title { get { return title; } set { if (value != title) { title = value; NotifyPropertyChanged("Title"); } } } /********************** Implementation of INotifyPropertyChanged ***********************************************/ /* below this line is the Implementation of the INotifyPropertyChanged */ public event PropertyChangedEventHandler PropertyChanged; //this is an helper method private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } }
MainPage.xaml 代码如下
<Page x:Class="Test_List.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Test_List" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" DataContext="{Binding TestData, RelativeSource={RelativeSource Self}}" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel VerticalAlignment="Top" HorizontalAlignment="Center"> <Button x:Name="myButton" Content="ChangeTitle" Click="myButton_Click" /> <Button x:Name="add" Content="addData" Click="add_Click" /> <ListView Width="100" Margin="10" ItemsSource="{Binding}" > <ListView.ItemTemplate> <DataTemplate> <StackPanel Margin="0,0,0,27.5"> <TextBlock x:Name="box" Text="{Binding Title}" Width="100" Height="40" /> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackPanel> </Page>
MainPage.XAML.cs代码如下
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; using Test_List.DataModel; using System.Diagnostics; using System.Collections.ObjectModel; // “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=391641 上有介绍 namespace Test_List { /// <summary> /// 可用于自身或导航至 Frame 内部的空白页。 /// </summary> public sealed partial class MainPage : Page { private ObservableCollection<SampleData> testData = new ObservableCollection<SampleData>(); public ObservableCollection<SampleData> TestData { get { return testData; } set { testData = value; } } public MainPage() { this.InitializeComponent(); this.NavigationCacheMode = NavigationCacheMode.Required; } /// <summary> /// 在此页将要在 Frame 中显示时进行调用。 /// </summary> /// <param name="e">描述如何访问此页的事件数据。 /// 此参数通常用于配置页。</param> protected override void OnNavigatedTo(NavigationEventArgs e) { // TODO: 准备此处显示的页面。 // testData.Title = "123123"; for (int i = 0; i < 5; i++) { SampleData tmp = new SampleData(); tmp.Title = "1" + i.ToString(); Debug.WriteLine(tmp.Title); testData.Add(tmp); } // TODO: 如果您的应用程序包含多个页面,请确保 // 通过注册以下事件来处理硬件“后退”按钮: // Windows.Phone.UI.Input.HardwareButtons.BackPressed 事件。 // 如果使用由某些模板提供的 NavigationHelper, // 则系统会为您处理该事件。 } private void myButton_Click(object sender, RoutedEventArgs e) { testData[0].Title = "yes"; } private void add_Click(object sender, RoutedEventArgs e) { testData.Add(new SampleData()); } } }
运行效果如下
解释下代码,其中
2014年8月12日 5:10
全部回复
-
你指的后台应该是指你应用程序的后台吧,并不是手机运行时候的后台吧。如果是这样你查找的资料就应该不是你想要的。
我写了个简单的例子希望对你有帮助,在后台你需要使用两个东西 一个是ObservableCollection 另一个是 INotifyPropertyChanged
在前台 你需要使用DataTemplate
其中ObservableCollection是指在容器元素改变时提供通知的容器 这个东西东西你可以查看
http://msdn.microsoft.com/zh-cn/library/ms668604(v=vs.110).aspx
其中INotifyPropertyChanged 是 元素属性内容改变时提供通知,可以查看
http://msdn.microsoft.com/zh-cn/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx
首先目录结构是这样的
其中SamleData类是自己实现的例子类
代码如下
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; namespace Test_List.DataModel { public class SampleData:INotifyPropertyChanged { public SampleData() { Id = 0; title = "what"; } public int Id { get; set; } private string title; public string Title { get { return title; } set { if (value != title) { title = value; NotifyPropertyChanged("Title"); } } } /********************** Implementation of INotifyPropertyChanged ***********************************************/ /* below this line is the Implementation of the INotifyPropertyChanged */ public event PropertyChangedEventHandler PropertyChanged; //this is an helper method private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } }
MainPage.xaml 代码如下
<Page x:Class="Test_List.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Test_List" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" DataContext="{Binding TestData, RelativeSource={RelativeSource Self}}" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <StackPanel VerticalAlignment="Top" HorizontalAlignment="Center"> <Button x:Name="myButton" Content="ChangeTitle" Click="myButton_Click" /> <Button x:Name="add" Content="addData" Click="add_Click" /> <ListView Width="100" Margin="10" ItemsSource="{Binding}" > <ListView.ItemTemplate> <DataTemplate> <StackPanel Margin="0,0,0,27.5"> <TextBlock x:Name="box" Text="{Binding Title}" Width="100" Height="40" /> </StackPanel> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackPanel> </Page>
MainPage.XAML.cs代码如下
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; using Test_List.DataModel; using System.Diagnostics; using System.Collections.ObjectModel; // “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=391641 上有介绍 namespace Test_List { /// <summary> /// 可用于自身或导航至 Frame 内部的空白页。 /// </summary> public sealed partial class MainPage : Page { private ObservableCollection<SampleData> testData = new ObservableCollection<SampleData>(); public ObservableCollection<SampleData> TestData { get { return testData; } set { testData = value; } } public MainPage() { this.InitializeComponent(); this.NavigationCacheMode = NavigationCacheMode.Required; } /// <summary> /// 在此页将要在 Frame 中显示时进行调用。 /// </summary> /// <param name="e">描述如何访问此页的事件数据。 /// 此参数通常用于配置页。</param> protected override void OnNavigatedTo(NavigationEventArgs e) { // TODO: 准备此处显示的页面。 // testData.Title = "123123"; for (int i = 0; i < 5; i++) { SampleData tmp = new SampleData(); tmp.Title = "1" + i.ToString(); Debug.WriteLine(tmp.Title); testData.Add(tmp); } // TODO: 如果您的应用程序包含多个页面,请确保 // 通过注册以下事件来处理硬件“后退”按钮: // Windows.Phone.UI.Input.HardwareButtons.BackPressed 事件。 // 如果使用由某些模板提供的 NavigationHelper, // 则系统会为您处理该事件。 } private void myButton_Click(object sender, RoutedEventArgs e) { testData[0].Title = "yes"; } private void add_Click(object sender, RoutedEventArgs e) { testData.Add(new SampleData()); } } }
运行效果如下
解释下代码,其中
2014年8月12日 5:10