Silverlight 和 WPF 中事件处理的差异 - 线程关联问题

发布于 2024-11-18 10:38:38 字数 597 浏览 3 评论 0原文

我一直在 Silverlight 中开发 Lync Silverlight 应用程序,现在我尝试将其转移到 WPF。

但是,我面临一些线程关联问题。例如,我在页面上的文本块中显示 Lync 客户端的状态,因此在后面的代码中连接了状态更改事件处理程序,每当 Lync 客户端的状态发生更改时,该处理程序就会将新状态写入文本块。

现在,这在 silverlight 中完美运行,但在 WPF 中似乎不允许。 现在我的问题是:

  1. 为什么它在 Silverlight bt 中工作而不是在 WPF 中工作,即使 Silverlight 应该是 WPF 的子集?

  2. 线程亲和力是一个重要的概念,我知道我们可以使用调用调度程序,但它不是以事件处理程序和回调的形式击败了异步编程的概念吗?

  3. 我在 XAML 页面中定义了一个按钮,并且在其上定义的单击事件处理程序可以访问其他 UI 元素,它不会遇到上述问题。

    但是,如果我在代码隐藏中定义 LyncClient 实例,则在其上定义的事件处理程序将无法访问 UI 元素。为什么会这样,我在 Silverlight 中检测到 UIElements 和其他对象之间没有这样的区别?

I have been developing a Lync Silverlight application in Silverlight and now I am trying to shift it to WPF.

However, I am facing some thread affinity issues. For example I display the Lync client's state on my page in a textblock, and so in my code behind have wired a state changed event handler, that writes the new state into the textblock whenever the state of Lync client changes.

Now, this worked perfectly in silverlight but seemingly is not allowed in WPF.
Now my questions are:

  1. How come it works in Silverlight bt not in WPF, even though Silverlight is supposed to be a subset of WPF?

  2. Thread affinity is an important concept and I know we can use invoke dispatcher, but doesn't it just beat the concept of asynchronous programming in form of event handlers and callbacks?

  3. I have a button defined in my XAML page, and the click event handler defined on it can access other UI elements, it does not suffer the problem outlined above.

    But if I define a LyncClient instance in my code-behind, event handlers defined on it cannot access the UI elements. Why so, I detected no such difference between UIElements and other objects in Silverlight?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看透却不说透 2024-11-25 10:38:38

根据上面的评论,我将建议以下“答案”...

我猜想 SL API 的编写方式与 WPF api 的编写方式很可能存在某种不同。这可以解释 API 发出回调时使用的线程的差异。要验证这一点,您可以:

  1. 直接询问 MS
  2. 在回调方法中放入一些诊断代码以记录线程 ID 并将其与应用程序的主线程进行比较。对 SL 和 WPF 执行此操作,看看它们是相同还是不同的线程。
  3. 在 Reflector 中打开程序集来检查每个 API 的编写方式。

在处理这种特定情况方面,在回调中,您可以:

  1. 获取调度程序对象(SL 与 WPF 不同)并始终通过 Dispatcher.Invoke 发出 UI 更新。
  2. 使用数据绑定和 INotifyPropertyChanged 将 UI 与属性隔离。您可以删除 ViewModel 上或后面的代码中的属性。然后将 UI 的文本框绑定到该属性。数据绑定有一些智能,可以自动将属性更改编组到正确的线程(无论如何在大多数情况下)。

希望有帮助。

Based on above comments, I'll suggest the following "answer"...

I would guess it is more likely than not that there is some sort of different in the way that the SL API was written than that of the WPF api. That could explain the difference in the thread that is used when the API issues the callback. To verify this, you could:

  1. Ask MS directly
  2. Put some diagnostics code in your callback method to log the thread ID and compare that to the main thread of the application. Do this for both SL and WPF to see if they are the same or different threads.
  3. Open the assemblies in Reflector to inspect how each API was written.

In terms of handling this specific situation, in your callback, you could:

  1. Get the dispatcher object (different for SL than WPF) and always issue UI updates through Dispatcher.Invoke.
  2. Use databinding and INotifyPropertyChanged to insulate the UI from the property. You could delcare a property on a ViewModel or in the code behind. Then bind the UI's textbox to that property. Databinding has some smarts in it that will automatically marshal property changes to the correct thread (in most cases anyway).

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文