2016-01-24 19 views
1

如何訪問嵌套在名稱空間\模塊路徑中的視圖模型?如何訪問嵌套在命名空間模塊路徑中的視圖模型?

例子:

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> 

Issue:

請注意,我的viewmodel不是直接位於名稱空間下,而是名稱空間\ module路徑。

例子:

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

這不起作用:

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

結果,我無法設置DataContext的,因爲提供的命名空間是不夠的,以確定視圖模型路徑。

錯誤:

The name "CreationViewModel" does not exist in the namespace "clr-namespace:ManageModules;assembly=ManageModules".

我的視圖模型如下:

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

@ Mark Seeman - 好主意! –

+0

我在我的博客上找到了一個解決方案。在我的文章中,我只有名字空間。因此,我沒有申報模塊。 https://bizmonger.wordpress.com/2015/11/25/mvvm-with-f-tutorial/ –

回答

0

剛剛從視圖模型中刪除模塊聲明。

所以刪除此行:

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)