0

帮助!现在已经一个星期了,我开始尝试在 VB.NET 中构建一个 MVVM 应用程序。不幸的是,98% 的文档都是面向 C# 的。我找到了一些线索,但我仍然没有所有的答案......

我想要做什么: 一个有 3 个视图的应用程序:loginView、View1 和 View2。如果没有用户连接,则会显示 loginView(没关系)。当用户连接时,我应该加载 View1 或 View2 关于他的权限。

它应该很简单,但由于我几乎找不到这个拼图的碎片,我几乎失去了所有的头发......

第一个问题: 在 loginView 中如何检查用户凭据?由于我无法将任何属性绑定到 PasswordBox,我发现一些文档说我应该这样做:

<Button x:Name="btnLogin" Content="Log in"
                Command="{Binding Path=AuthenticateUser}"
                CommandParameter="{Binding ElementName=txtPassword}"/>

但是,如果我设法执行不带参数的命令,我将找不到如何执行带参数的命令。有什么简单的想法吗?

要从按钮运行命令,我使用了那里的 relayCommand 类:Implementing RelayCommand (MVVM) in VB.NET: Syntax questions

所以我定义了一个这样的属性:

    Dim _relayCmd As New RelayCommand(AddressOf Authentication)
    Public ReadOnly Property AuthenticateUser As ICommand
        Get
            Return _relayCmd
        End Get
    End Property

    ' Authentication method
    Private Sub Authentication(ByVal _passwordBox As PasswordBox)

        'do something...
    End Sub

第二个问题: 一旦用户连接,我如何切换到 View1 或 View2?我读了一些文档说我应该使用我的观点的 observablecollection 并浏览它。但我应该更改 MainWindowView 的数据上下文。在VB中仍然没有找到正确的方法。

我不希望你为我做所有事情,而是我想找到一个简单的教程,可以清楚地解释它在 VB 中是如何工作的。

谢谢你的帮助!

4

1 回答 1

1

您可以将命令参数强制转换为PasswordBox

Dim _relayCmd As New RelayCommand(AddressOf Authentication, Function(obj As Object)
Return True
End Function)
Public ReadOnly Property AuthenticateUser As ICommand
    Get
        Return _relayCmd
    End Get
End Property

Private Sub Authentication(ByVal _passwordBox As Object)
    Dim passwordBox = TryCast(_passwordBox, PasswordBox)
    'do something...
End Sub

至于你的第二个问题,你没有提供足够的细节。但是,如果您有其他问题,请提出另一个问题。

于 2017-06-30T13:11:46.713 回答