访问 UI 线程以更改 WPF 中的依赖属性

发布于 2024-11-25 14:31:56 字数 1216 浏览 1 评论 0原文

我正在尝试更新异步回调内的 VB.Net 4.0 中的依赖属性。我觉得我这样做是正确的,但我仍然收到“调用线程无法访问该对象,因为另一个线程拥有它”。错误。有人看到在 VB.Net 4.0 中使用委托的更好方法吗?

Private WithEvents myObj as CallingObject
Private Delegate Sub MyErrorDel(ByVal strError as string)

Public Property ErrorMessage As String
    Get
       Return CStr(GetValue(ErrorMessageProperty))
    End Get
    Set(ByVal value As String)
        SetValue(ErrorMessageProperty, value)
    End Set
End Property

Private Sub MySub()
    myObj.CallFuncAsync()
End Sub

Private Sub DisplayError(ByVal strError as String)
    'Set Dependancy Property value Bound to UI Textbox
    ErrorMessage = strError
End Sub

Private Sub myObj_CallFuncCompleted(Byval sender as Object, ByVal e as CallFuncEventArgs)
    'Call delegate and pass in error string as argument
    Dim delError as MyErrorDel
    delError = New MyErrorDel(AddressOf DisplayError)
    delError("An error occured")
    Me.Dispatcher.Invoke(delError, System.Windows.Threading.DispatcherPriority.Normal, Nothing)
End Sub

每当在 DisplayError 内部设置 ErrorMessage 时,即使我使用调度程序调用 DisplayError,也会引发异常。

如果有人发现我尝试从异步回调访问依赖属性的方式有任何问题,我将非常感谢您的反馈。

感谢您的帮助!

哦,我对写关于代码问题的博客还很陌生。任何关于如何更好地提出这个问题的建议也将受到欢迎。

I'm trying to update a dependancy property in VB.Net 4.0 inside of an Async callback. I feel like I am doing this correctly but I'm still getting the "The calling thread cannot access this object because a different thread owns it." error. Does someone see a better way of using delegates in VB.Net 4.0?

Private WithEvents myObj as CallingObject
Private Delegate Sub MyErrorDel(ByVal strError as string)

Public Property ErrorMessage As String
    Get
       Return CStr(GetValue(ErrorMessageProperty))
    End Get
    Set(ByVal value As String)
        SetValue(ErrorMessageProperty, value)
    End Set
End Property

Private Sub MySub()
    myObj.CallFuncAsync()
End Sub

Private Sub DisplayError(ByVal strError as String)
    'Set Dependancy Property value Bound to UI Textbox
    ErrorMessage = strError
End Sub

Private Sub myObj_CallFuncCompleted(Byval sender as Object, ByVal e as CallFuncEventArgs)
    'Call delegate and pass in error string as argument
    Dim delError as MyErrorDel
    delError = New MyErrorDel(AddressOf DisplayError)
    delError("An error occured")
    Me.Dispatcher.Invoke(delError, System.Windows.Threading.DispatcherPriority.Normal, Nothing)
End Sub

Whenever ErrorMessage gets set inside of DisplayError an exception gets thrown, even though I am using the dispatcher to call DisplayError.

If anyone see any issues with the way I am trying to access Dependancy Properties from a async callback I would really appreciate the feedback.

Thanks for the help!

Oh and I'm pretty new at blogging about code issues. Any suggestions about how to better formulate this question would be welcome as well.

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

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

发布评论

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

评论(2

爱殇璃 2024-12-02 14:31:56

问题可能是,在调用 Me... 时,您已经在访问另一个线程拥有的对象,请尝试预先存储对调度程序的引用,或者可能使用 Application.Current。调度员

The problem might be that at the call to Me... you are already accessing an object owned by another thread, try to store a reference to the dispatcher beforehand or possibly use Application.Current.Dispatcher.

我的黑色迷你裙 2024-12-02 14:31:56

由于您没有指出有问题的行,我怀疑这里的问题是您在 delError("An error generated") 行中调用委托,而不是等到到达调度程序。考虑将 CallFuncCompeted 实现更改为

Me.Dispatcher.Invoke(AddressOf DisplayError, "An error occureed")

Since you didn't indicate the offending line, I suspect the problem here is that you're invokng your delegate in the line delError("An error occured") rather than waiting until you get to the dispatcher. Consider changing your CallFuncCompeted implementation to

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