如何访问嵌套在namespace \ module路径中的viewmodel?

时间:2016-01-24 12:32:01

标签: wpf xaml f#

如何访问嵌套在namespace \ module路径中的viewmodel?

示例:

namespace ManageModules
module CreateModule =
   ...
    type CreationViewModel() =
        inherit ViewModelBase()

以下命名空间声明得到解决:

xmlns:manageModules="clr-namespace:ManageModules;assembly=ManageModules"

在以下XAML中:

<Window x:Class="Client.MainWindow"
        . . .
        xmlns:manageModules="clr-namespace:ManageModules;assembly=ManageModules"
        . . . />

    <Window.DataContext>
        <manageModules:CreationViewModel />
    </Window.DataContext>

问题:

请注意我的viewmodel不是直接在命名空间下,而是命名空间\ module路径。

示例:

<Window.DataContext>
    <manageModules:CreationViewModel />
</Window.DataContext>

这不起作用:

<Window.DataContext>
    <manageModules:CreateModule.CreationViewModel />
</Window.DataContext>

因此,我在设置DataContext时遇到问题,因为提供命名空间不足以识别viewmodel路径。

错误:

  

命名空间中不存在名称“CreationViewModel”   “CLR-名称空间:ManageModules;装配= ManageModules”

我的ViewModel如下:

namespace ManageModules
module CreateModule =

    open System.Windows.Input
    open UILogic.State
    open UILogic.Interaction
    open ManageModule.Entities
    open System.Collections.ObjectModel

    type CreationViewModel() =
        inherit ViewModelBase()

        let mutable (_modules:Module ObservableCollection) = ObservableCollection()

        member this.Modules
            with get()      = _modules
            and set(value)  = _modules <- value

        member this.Add moduleItem = 
            _modules.Add(moduleItem)

1 个答案:

答案 0 :(得分:0)

只需从ViewModel中删除模块声明。

请删除此行:

module CreateModule =

并保留其余代码:

namespace ManageModules

    open System.Windows.Input
    open UILogic.State
    open UILogic.Interaction
    open ManageModule.Entities
    open System.Collections.ObjectModel

    type CreationViewModel() =
        inherit ViewModelBase()
        let name =      { First=String20("Scott"); Last=String20("Nimrod"); Suffix=None }
        let duration =  { Hours=1; Minutes=30; Seconds=0 }
        let moduleItem = { Author=name; Duration=duration }

        let mutable (_modules:Module ObservableCollection) = ObservableCollection()

        do _modules.Add(moduleItem)

        member this.Modules
            with get()      = _modules
            and set(value)  = _modules <- value

        member this.Add moduleItem = 
            _modules.Add(moduleItem)