0

我是 VB.net 的新手,所以请理解。我有一个MainWindow, Frame1, Frame2 (AdFrame1), Page1.

MainWindowPage1Frame1启动加载。在MainWindow.vb class我有一个Frame2可见的公共子。

我将如何共享该公共子,以便我可以使用 Page_Mouseup 事件来显示 Frame2 (AdFrame1)

我尝试了 Public Shared Sub,但出现此错误:“如果没有类的显式实例,则无法从共享方法或共享成员初始化程序中引用类的实例成员”

MainWindow.vb 中的代码:

Private Sub MainWindow2_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseUp


        AdClick()

    End Sub



    Public Sub AdClick()



        Try


            clicks += 1

            If clicks >= 10 Then

                'After 5 seconds Adframe CloseLink and TextBlock1 will hide.
                AdFrame1.Visibility = Windows.Visibility.Visible
                CloseMainButton.Visibility = Windows.Visibility.Visible
                InitializeComponent()
                dpTimer = New DispatcherTimer
                dpTimer.Interval = TimeSpan.FromMilliseconds(10000)
                AddHandler dpTimer.Tick, AddressOf TickMe
                dpTimer.Start()

                clicks = 0

            End If
        Catch ex As Exception
            MessageBox.Show("Oops! Error X0123A1. Please contact us with error ASAP!", "Error!", MessageBoxButton.OK)
        End Try
    End Sub


    'After 10 sesonds auto close
    Private Sub TickMe()

        AdFrame1.Visibility = Windows.Visibility.Hidden

    End Sub

代码页1.vb

  Private Sub Page1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseUp


            AdClick() (inaccessible due to protection)

        End Sub

那么如何与 Page1 共享 AdClick()

4

1 回答 1

3

首先,请注意关键字与可见性Shared无关- 例如,成员可以是或。成员与 C# 中的成员相同- 它对类的所有实例都是通用的(请参阅:在 VB.NET 中共享变量的用途是什么?Public SharedPrivate SharedSharedstatic

对于实际问题,我建议只连接已经存在的事件:

Class MainWindow2
    Private WithEvents _Page1 as New Page1

    Private Sub MainWindow2_MouseUp(sender As Object, e As MouseEventArgs) _
                                            Handles Me.MouseUp, _Page1.MouseUp 
        AdClick()
    End Sub

    Private Sub AdClick()
         'Do something
    End Sub
End Class

公开方法通常是错误的方法——如果你想MainWindow做某事,最好让它自己负责做,而不是让其他类试图在别处做。有了事件,Page1只负责让MainWindow知道它被点击了;这留给它MainWindow来决定如何处理它,并且Page1不需要对包含在MainWindow.

您甚至可以添加自定义事件 - 如果您有一个按钮Page1,例如:

Class Page1
    'make your own event
    Public Event Button1Click(sender As Object, e As RoutedEventArgs)

    Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) _
                                                         Handles Button1.Click
        RaiseEvent Button1Click(sender, e)
    End Sub
End Class

然后在MainWindow

Private Sub ClickHandle(sender As Object, e As RoutedEventArgs) _
                                                   Handles _Page1.Button1Click
    AdClick()
End Sub

这将使页面内的按钮单击传播到主窗口。

如果您的页面是在运行时创建的,则您无法使用WithEventsHandles但您可以:

Dim newPage as New Page1
AddHandler newPage.MouseUp, AddressOf MainWindow2_MouseUp

只需确保在完成页面超出范围之前删除处理程序(处理程序保持强引用,因此newPage永远不会在仍然连接处理程序的情况下收集垃圾!-->内存泄漏)。

RemoveHandler newPage.MouseUp, AddressOf MainWindow2_MouseUp
于 2013-05-28T16:21:02.383 回答