0

我正在开发一个 WP7 应用程序,如果日历上的当前事件是“会议”,我需要它来播放第一个链接中的歌曲。但是,使用当前代码,即使事件设置正确,它也会播放第二首歌曲而不是第一首歌曲。

这是我的代码:

public partial class MainPage : PhoneApplicationPage
{   
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        // Set the data context of the listbox control to the sample data
        DataContext = App.ViewModel;
        this.Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    // Load data for the ViewModel Items
    private void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        if (!App.ViewModel.IsDataLoaded)
        {
            App.ViewModel.LoadData();
        }
    }        

    private void button1_Click(object sender, RoutedEventArgs e)
    {            
        Appointments appts = new Appointments();

        //Identify the method that runs after the asynchronous search completes.
        appts.SearchCompleted += new EventHandler<AppointmentsSearchEventArgs>(Appointments_SearchCompleted);

        DateTime start = DateTime.Now;
        DateTime end = DateTime.Now;
        int max = 1;

        //Start the asynchronous search.
        appts.SearchAsync(start, end, max, "Appointments Test #1");
        textBlock3.Text = DateTime.Now.ToString("hh:mm:ss tt");
    }

    void Appointments_SearchCompleted(object sender, AppointmentsSearchEventArgs e)
    {
        try
        {
            AppointmentResultsDataLINQ.DataContext =
                from Appointment appt in e.Results
                where appt.IsAllDayEvent == false                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
                select appt;       

        }
        catch (System.Exception)
        {

        }
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {

        if ((AppointmentResultsDataLINQ.DataContext).Equals("Meeting"))
        {
             mediaElement1.Source = new Uri("http://www.opendrive.com/files/NV8zNTMwNDYwX2hxRXZR/Crystallize.mp3", UriKind.Absolute);              
        }          

        else
        {
            mediaElement1.Source = new Uri("https://www.opendrive.com/files/NV8zMjAxODY0X0VBNDJY/Hetken%20tie%20on%20kevyt%20(piano%20cover)%20-%20YouTube.mp3", UriKind.Absolute);                
        }
            mediaElement1.Play();
    }          
}
4

1 回答 1

0

问题似乎是这一行:

if ((AppointmentResultsDataLINQ.DataContext).Equals("Meeting"))

您正在将在实例上调用ToString()方法的结果Appointment与字符串“Meeting”进行比较。

你可能想要:

if ((AppointmentResultsDataLINQ.DataContext as Appointment).Subject.Equals("Meeting"))

更新
您实际上是在检查约会的可枚举。
以下是检查其中任何一个是否为“会议”的方法:

if ((AppointmentResultsDataLINQ.DataContext as IEnumerable<Appointment>).Any(app => app.Subject.Equals("Meeting")))
于 2013-01-21T12:01:38.663 回答