有没有办法让当前视图在另一个视图(不是现在打开的视图)中执行一个方法?

时间:2019-06-13 12:21:13

标签: vb.net

我有一个具有多个屏幕/子菜单的应用程序。每个屏幕上都有一个翻译按钮,可根据当前选择在英语和法语之间切换。问题是,当我用英语打开主菜单然后打开子菜单并将语言更改为法语时,主菜单的某些元素无法翻译。要解决此问题,我需要通过子菜单在主菜单的类中执行一个方法。我不能只是将方法复制到子菜单的类中。有什么方法可以从子菜单调用方法来更改主菜单的元素?

这是更改语言的按钮,位于名为wpfUserSearch.xaml.vb的子菜单中。理想情况下,应该使用方法调用从屏幕wpfUser.xaml.vb执行名为subLoadComboBoxesToFrench()和subLoadComboBoxesToEnglish()的方法。

Private Sub btnChangeLanguage_Click(sender As Object, e As RoutedEventArgs) Handles btnChangeLanguage.Click
        Try
            If CurrentLanguageSelection = enumLanguage.English Then
                CurrentLanguageSelection = enumLanguage.French
                Dim newCulture As CultureInfo = New CultureInfo("fr-CA")
                Thread.CurrentThread.CurrentCulture = newCulture
                gCurrentStrLanguage = "FR"

            ElseIf CurrentLanguageSelection = enumLanguage.French Then
                CurrentLanguageSelection = enumLanguage.English
                Dim newCulture As CultureInfo = New CultureInfo("en-CA")
                Thread.CurrentThread.CurrentCulture = newCulture
                gCurrentStrLanguage = "EN"

            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

这些是我希望更改语言按钮调用的方法。它们在主菜单中,名为wpfUser.xaml.vb。

Public Sub subLoadComboBoxesToFrench()
        comboLanguagePreference.ItemsSource = ActiveLanguages
        comboLanguagePreference.DisplayMemberPath = "Name_French"
        comboLanguagePreference.SelectedValuePath = "Language_Abbr"

        Me.comboUserRole.ItemsSource = ActiveUserRoles
        comboUserRole.DisplayMemberPath = "User_Role_Name_French"
        comboUserRole.SelectedValuePath = "User_Role_ID"
End Sub

Public Sub subLoadComboBoxesToEnglish()
        comboLanguagePreference.ItemsSource = ActiveLanguages
        comboLanguagePreference.DisplayMemberPath = "Name_English"
        comboLanguagePreference.SelectedValuePath = "Language_Abbr"

        Me.comboUserRole.ItemsSource = ActiveUserRoles
        comboUserRole.DisplayMemberPath = "User_Role_Name_English"
        comboUserRole.SelectedValuePath = "User_Role_ID"
    End Sub

1 个答案:

答案 0 :(得分:0)

从主菜单中共享两个Sub,这样您就可以得到

Public Shared Sub subLoadComboBoxesToFrench()
        comboLanguagePreference.ItemsSource = ActiveLanguages
        comboLanguagePreference.DisplayMemberPath = "Name_French"
        comboLanguagePreference.SelectedValuePath = "Language_Abbr"

        Me.comboUserRole.ItemsSource = ActiveUserRoles
        comboUserRole.DisplayMemberPath = "User_Role_Name_French"
        comboUserRole.SelectedValuePath = "User_Role_ID"
End Sub

Public Shared Sub subLoadComboBoxesToEnglish()
        comboLanguagePreference.ItemsSource = ActiveLanguages
        comboLanguagePreference.DisplayMemberPath = "Name_English"
        comboLanguagePreference.SelectedValuePath = "Language_Abbr"

        Me.comboUserRole.ItemsSource = ActiveUserRoles
        comboUserRole.DisplayMemberPath = "User_Role_Name_English"
        comboUserRole.SelectedValuePath = "User_Role_ID"
End Sub

然后只需通过按钮调用它们

<Your Class Name>.subLoadComboBoxesToEnglish()
<Your Class Nmae>.subLoadComboBoxesToFrench()