Silverlight 和 WPF 中事件处理的差异 - 线程关联问题
我一直在 Silverlight 中开发 Lync Silverlight 应用程序,现在我尝试将其转移到 WPF。
但是,我面临一些线程关联问题。例如,我在页面上的文本块中显示 Lync 客户端的状态,因此在后面的代码中连接了状态更改事件处理程序,每当 Lync 客户端的状态发生更改时,该处理程序就会将新状态写入文本块。
现在,这在 silverlight 中完美运行,但在 WPF 中似乎不允许。 现在我的问题是:
为什么它在 Silverlight bt 中工作而不是在 WPF 中工作,即使 Silverlight 应该是 WPF 的子集?
线程亲和力是一个重要的概念,我知道我们可以使用调用调度程序,但它不是以事件处理程序和回调的形式击败了异步编程的概念吗?
我在 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:
How come it works in Silverlight bt not in WPF, even though Silverlight is supposed to be a subset of WPF?
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?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据上面的评论,我将建议以下“答案”...
我猜想 SL API 的编写方式与 WPF api 的编写方式很可能存在某种不同。这可以解释 API 发出回调时使用的线程的差异。要验证这一点,您可以:
在处理这种特定情况方面,在回调中,您可以:
希望有帮助。
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:
In terms of handling this specific situation, in your callback, you could:
Hope that helps.