1

我正在使用 silverlight 5 应用程序为 CRM 2011 创建 Webresource。在我的代码中,我使用 BeginExecute 和 EndExecute 方法来检索用于安排约会的可用时间段。在 EndExecute 方法中没有,我得到了可以提出会议请求的所有可用时间段。现在,当我得到可用时隙时,我必须检查所有时隙的开始时间以及在 UI 上选择的开始时间(在 UI 上我使用了 DateTimePicker),为此我必须从 DateTimePIcker 中读取值。为此,我尝试了 DateTimePIcker.SelectedDateTime 但它给了我错误提示“无效的跨线程访问”。

我用谷歌搜索了这个问题,发现 UI 操作不能用非 UI 线程执行。他们建议使用 Dispatcher.BeginInvoke(),

我试过以下也是我的代码片段

DateTime ScheduledDateTime = new DateTime();
    System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => ScheduledDateTime = Convert.ToDateTime(MainDateTimePicker.SelectedDateTime));

但仍然在变量 ScheduledDateTime 中,我得到了“new DateTime()”的值

你能建议我一种从非 UI 线程获取 DateTimePicker 值的方法吗?

4

1 回答 1

0

一种方法是在调用BeginExecute方法之前,将 的值保存DateTimePicker到变量中。然后你可以读取这个变量,甚至从另一个线程。

于 2013-09-25T20:15:39.437 回答