【问题标题】:WPF MVVM VB.Net multiple views applicationWPF MVVM VB.Net 多视图应用程序
【发布时间】:2017-06-30 21:11:46
【问题描述】:

帮助!现在已经一个星期了,我开始尝试在 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 problems

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

    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 中的工作原理。

感谢您的帮助!

【问题讨论】:

    标签: wpf vb.net mvvm multiple-views


    【解决方案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
    

    至于您的第二个问题,您没有提供足够的详细信息。但是,如果您有其他问题,请再问一个问题。

    【讨论】:

    • 你又一次救了我的命!既然你写了它,答案似乎很明显!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    相关资源
    最近更新 更多