如何访问 Windows Phone 的日历数据

厚渺酥1685
2013-10-14 · TA获得超过4289个赞
知道小有建树答主
回答量:944
采纳率:0%
帮助的人:1725万
展开全部
可以采用不同的方式使用结果,但本主题演示如何将结果绑定到用户界面或枚举结果。本主题讨论只读访问用户的联系人数据。有关为提供读和写访问的应用创建自定义联系人存储的信息,请参见 Windows Phone 的自定义联系人存储。本主题包含以下各节。访问日历数据对约会数据结果进行数据绑定枚举约会数据结果LINQ相关主题访问日历数据在此过程中,您将代码放在仅用于测试的按钮单击事件中。在您自己的应用程序中,只要需要,您就可以访问日历数据。以下过程假定您拥有一个 Windows�0�2Phone 应用程序,该应用程序包含一个带有名为 ButtonAppointments 的按钮的页面。在此过程中,您搜索在接下来的 7 天中的所有约会。重要说明:Windows Phone 模拟器不包含示例约会。您应该使用物理设备测试这些过程。访问日历数据的步骤在您页面的代码隐藏文件顶部,添加以下语句。using Microsoft.Phone.UserData;Imports Microsoft.Phone.UserData添加以下代码。该代码包含按钮单击事件以及处理已完成的异步搜索事件的方法。private void ButtonAppointments_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 = start.AddDays(7); int max = 20; //Start the asynchronous search. appts.SearchAsync(start, end, max, "Appointments Test #1"); } void Appointments_SearchCompleted(object sender, AppointmentsSearchEventArgs e) { //Do something with the results. MessageBox.Show(e.Results.Count().ToString()); }Private Sub ButtonAppointments_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Dim appts As Appointments = New Appointments() 'Identify the method that runs after the asynchronous search completes. AddHandler appts.SearchCompleted, AddressOf Appointments_SearchCompleted Dim start as DateTime = DateTime.Now Dim endTime as DateTime = start.AddDays(7) Dim max as integer = 20 'Start the asynchronous search. appts.SearchAsync(start, endTime, max, "Appointments Test #1") End Sub Private Sub Appointments_SearchCompleted(sender As Object, e As AppointmentsSearchEventArgs) 'Do something with the results. MessageBox.Show(e.Results.Count().ToString()) End Sub对约会数据结果进行数据绑定在此过程中,将约会搜索的结果数据直接绑定到用户界面。此过程假定您已完成前面部分的“访问日历数据”过程。对约会数据进行数据绑定的步骤为您的页面打开 XAML 编辑器并添加以下代码。可以将代码添加到 Content Panel 或 Layout Root GRID 元素。此 XAML 创建一个您绑定到约会数据的列表框。该列表框中每一行的数据模板都包含约会的主题。在您的应用程序中,可以创建所需的任何数据模板。有关更多信息,请参见 Windows Phone 的数据绑定和 数据模板概述。<StackPanel Height="Auto" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" > <TextBlock Name="AppointmentResultsLabel" Text="results are loading..." Style="{StaticResource PhoneTextLargeStyle}" TextWrapping="Wrap" /> <ListBox Name="AppointmentResultsData" ItemsSource="{Binding}" Height="200" Margin="24,0,0,0" > <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Path=Subject, Mode=OneWay}" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </StackPanel>在您页面的代码隐藏文件中,将现有事件处理程序替换为以下代码。该代码通过将列表框的数据上下文设置为等于搜索结果,将约会数据绑定到用户界面。如果没有结果,则该代码包含错误处理。该代码还根据是否有结果更改列表框标签的文本。void Appointments_SearchCompleted(object sender, AppointmentsSearchEventArgs e) { try { //Bind the results to the user interface. AppointmentResultsData.DataContext = e.Results; } catch (System.Exception) { //No results } if (AppointmentResultsData.Items.Any()) { AppointmentResultsLabel.Text = "results"; } else { AppointmentResultsLabel.Text = "no results"; } }Private Sub Appointments_SearchCompleted(sender As Object, e As AppointmentsSearchEventArgs) Try 'Bind the results to the user interface. AppointmentResultsData.DataContext = e.Results Catch ex As System.Exception 'No results End Try If AppointmentResultsData.Items.Any() Then AppointmentResultsLabel.Text = "results" Else AppointmentResultsLabel.Text = "no results" End If End Sub保存并生成您的解决方案。启动您的应用程序并单击该按钮。数据将出现在列表框中。枚举约会数据结果在此过程中,枚举约会搜索的结果并将结果显示在一个消息框中。此过程假定您已完成本主题前面部分的“访问日历数据”过程。枚举约会数据的步骤在您页面的代码隐藏文件中,将现有事件处理程序替换为以下代码。该代码通过使用 for-each 循环枚举约会数据。void Appointments_SearchCompleted(object sender, AppointmentsSearchEventArgs e) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (Appointment appt in e.Results) { sb.AppendLine(appt.Subject); } MessageBox.Show(sb.ToString()); }Private Sub Appointments_SearchCompleted(sender As Object, e As AppointmentsSearchEventArgs) Dim sb As System.Text.StringBuilder = new System.Text.StringBuilder() For Each appt As Appointment In e.Results sb.AppendLine(appt.Subject) Next MessageBox.Show(sb.ToString()) End Sub保存并生成您的解决方案。启动您的应用程序并单击该按钮。显示消息框。LINQ在此过程中,使用 LINQ 优化约会搜索的结果。将结果数据直接绑定到用户界面。此过程假定您已完成本主题前面部分的“访问约会数据”过程。使用LINQ 的步骤在您页面的代码隐藏文件中,将现有事件处理程序替换为以下代码。该代码查看非全天约会的约会结果。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) { //No results } }Private Sub Appointments_SearchCompleted(sender As Object, e As AppointmentsSearchEventArgs) Try 'Bind the results to the user interface. AppointmentResultsDataLINQ.DataContext = _ From appt As Appointment In e.Results _ Where appt.IsAllDayEvent = false _ Select appt Catch ex As System.Exception 'No results End Try End Sub 为您的页面打开 XAML 编辑器并添加以下代码。可以将代码添加到 Content Panel 或 Layout Root GRID 元素。此 XAML 创建一个您绑定到约会数据的列表框。列表框的每一行都包含约会的主题。<ListBox Name="AppointmentResultsDataLINQ" ItemsSource="{Binding}" Height="200" Margin="24,0,0,0" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock Text="{Binding Path=Subject, Mode=OneWay}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>保存并生成您的解决方案。启动您的应用程序并单击该按钮。筛选后的数据将出现在列表框中。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式