这是我的代码……
imports Systemimports System.IOimports System.Netimports System.windowsimports System.windows.Controlsimports System.windows.Dataimports System.windows.Mediaimports System.windows.Media.Animationimports System.windows.NavigationPartial Public Class Crime Dim Scrollleft As Boolean = True Dim atimer As New System.Timers.Timer() Public Sub New() MyBase.New() Me.InitializeComponent() ' Insert code required on object creation below this point. ' Hook up the Elapsed event for the timer. AddHandler atimer.Elapsed,AddressOf Me.timer_Tick atimer.Interval = 100 atimer.Enabled = True End Sub 'CODE TO SCRolL SCRolLVIEWER PROGRAMATICALLY Private Sub timer_Tick(sender As Object,e As EventArgs) If Scrollleft Then svImages.ScrollToHorizontalOffset(svImages.HorizontalOffset - 1) Else svImages.ScrollToHorizontalOffset(svImages.HorizontalOffset + 1) End If End Sub Private Sub left_MouseEnter(sender As Object,e As MouseEventArgs) Scrollleft = True atimer.Start() End Sub Private Sub Right_MouseEnter(sender As Object,e As MouseEventArgs) Scrollleft = False atimer.Start() End Sub Private Sub left_MouseLeave(sender As Object,e As MouseEventArgs) atimer.Stop() Scrollleft = True End Sub Private Sub Right_MouseLeave(sender As Object,e As MouseEventArgs) atimer.Stop() End SubEnd Class
如果我从Expressions Blend运行它,项目构建正常并显示,但滚动 *** 作不起作用.
如果我从Visual Studio运行它我得到一行错误…
svImages.ScrollToHorizontalOffset(svImages.HorizontalOffset - 0.1)
说用户代码未处理InvalIDOperationException …调用线程无法访问此对象,因为另一个线程拥有它.在窗口加载之前我收到此错误.
我哪里出错了.
解决方法 错误“调用线程无法访问此对象,因为另一个线程拥有它.”是WinForms和WPF控件的标准.在WPF和WinForms中,窗口使用一个特定的线程(通常称为UI线程)在屏幕上呈现.有关控件的每个更新/更改 *** 作都应该在该线程上进行以获得成功.
通常使用WinForms的方法是创建一个特殊的委托并调用Control.Invoke,如此link所示.
从WPF开始,使用Dispatcher也可以达到同样的效果.您的代码应如下所示:
this.dispatcher.Invoke( () => svImages.ScrollToHorizontalOffset(svImages.HorizontalOffset - 0.1));
更新:
我有以下代码在VB.NET中工作:
Private Delegate Sub ScrollDelegate(ByVal offset As Double)Private Sub Scrollleft(ByVal offset As Double) svImages.ScrollToHorizontalOffset(svImages.HorizontalOffset + offset)End Sub// ... calling from background threadDim slt As ScrollDelegateslt = New ScrollDelegate(AddressOf Scrollleft)Me.dispatcher.Invoke(slt)
更新2
代码已更改为问题.
Dim Scrollleft As Boolean = TrueDim atimer As New System.Timers.Timer()Dim scrollMethod As ScrollDelegatePrivate Delegate Sub ScrollDelegate(ByVal offset As Double)// ... Me.InitializeComponent() slt = New ScrollDelegate(AddressOf DoScroll)// ... Private Sub timer_Tick(sender As Object,e As EventArgs) If Scrollleft Then Me.dispatcher.Invoke(slt,-1) Else Me.dispatcher.Invoke(slt,1) End IfEnd Sub// ...Private Sub DoScroll(ByVal offset As Double) svImages.ScrollToHorizontalOffset(svImages.HorizontalOffset + offset)End Sub总结
以上是内存溢出为你收集整理的wpf – 尝试以编程方式滚动滚动查看器全部内容,希望文章能够帮你解决wpf – 尝试以编程方式滚动滚动查看器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)