2012-01-14 83 views
1

我試圖把Prism(僅用於CompositeUI)和MVVM輕工具包(用於MVVM架構= D)一起工作,但我在Light ViewModelLocator中遇到了一些麻煩。 當我們使用定位成一個「簡單」的WPF應用程序,我們可以在App.xaml中使用的應用程序資源,這樣的:當我們使用棱鏡棱鏡和MVVM光工具包

<Application.Resources> 
    <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" /> 
</Application.Resources> 

但是,我們的業務代碼是模塊項目中,並且它不具有的App.xaml,然後我試圖把該資源的資源觀裏面:

<Window.Resources> 
    <ResourceDictionary> 
     <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" /> 
    </ResourceDictionary> 
</Window.Resources> 

它只是關閉了我的設計模式(也運行時)錯誤,但沒有按視圖」 t出現在分配給它的區域中。

有人已經試圖做這樣的事情? 是否有可能將Prism和MVVM Light一起使用?

這是我的 「全」 代碼:

(ViewLight.xaml)

<Window x:Class="TryERP2.Financeiro.View.ViewLight" 
    ... a lot of NS ... 
    mc:Ignorable="d" 
    xmlns:vm="clr-namespace:TryERP2.Financeiro.ViewModel" 
    d:DesignHeight="150" d:DesignWidth="245" SizeToContent="WidthAndHeight"> 
    <Window.Resources> 
     <ResourceDictionary> 
      <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" /> 
     </ResourceDictionary> 
    </Window.Resources> 

    <Grid DataContext="{Binding Light, Source={StaticResource Locator}}"> 
     <Button Content="{Binding MyButtonText}" HorizontalAlignment="Stretch" Name="button1" VerticalAlignment="Stretch" /> 
    </Grid> 
</Window> 

ViewLightModel.cs:

public class ViewLightModel : ViewModelBase 
{ 
    public ViewLightModel() 
    { } 

    public const string MyButtonTextPropertyName = "MyButtonText"; 
    private string _myButtonText = "Button Text"; 

    public string MyButtonText 
    { 
     get 
     { return _myButtonText; } 

     set 
     { 
      if (_myButtonText == value) 
      { return; } 

      _myButtonText = value; 
      RaisePropertyChanged(MyButtonTextPropertyName); 
     } 
    } 
} 

Financeiro.cs(模塊初始化類...部分顯示...只是我註冊和「援引」的意見):

 var container = ServiceLocator.Current.GetInstance<IUnityContainer>(); 
     container.RegisterType<Object, ViewLight>("ViewLight"); 

     var regionManager = ServiceLocator.Current.GetInstance<IRegionManager>(); 
     var main = new Uri("ViewLight", UriKind.Relative); 
     regionManager.RequestNavigate("Desktop", main); 

「正常的」MVVM Light應用程序有一個App.xaml文件,(我認爲)不在Prism modules Views中使用。該文件具有以下結構:

<Application x:Class="TryERP2.Financeiro.App" 
     ... a lot of NS ... 
     xmlns:vm="clr-namespace:TryERP2.Financeiro.ViewModel" 
     StartupUri="MainWindow.xaml" 
     mc:Ignorable="d"> 

    <Application.Resources> 
     <vm:ViewModelLocator x:Key="Locator" 
          d:IsDataSource="True" /> 
    </Application.Resources> 

</Application> 

這就是執行我的應用程序時發生的情況。這是該模塊上的視圖應該被裝入這個空白,顯示出它的按鈕,但是什麼都沒有發生:

http://imageshack.us/photo/my-images/818/capturarwy.png

當我試圖改變這個觀點,並把它的位置「簡單視圖」(不使用MVVMLight)它完美的作品,像顯示在下面的圖片:

http://imageshack.us/photo/my-images/513/capturar1a.png

+0

你使用什麼容器? MEF還是Unity?你可以發佈你的視圖代碼隱藏和ViewModel代碼隱藏的代碼嗎?此外,還有關於如何將V-VM注入區域的代碼。 MVVMLight,設計數據等是您在PRISM/Silverlight中處理的單獨問題。查看未顯示最有可能的組成錯誤 – katit 2012-01-14 16:24:56

+0

我正在使用Unity。我將通過編輯問題發佈整個代碼。 – 2012-01-14 17:55:19

+0

除了默認代碼外,View上沒有隱藏代碼。 您認爲正在運行的應用程序的解釋截圖會有幫助嗎? – 2012-01-14 18:13:34

回答

0

對不起。我犯了一個大錯誤。 正如您可能已經注意到的那樣,我正在構建一個Ribbon應用程序,並試圖在空白區域下顯示控件。 可以在那裏顯示的項目有控件。它不工作,因爲我是想顯示一個窗口:

<Window x:Class="TryERP2.Financeiro.View.ViewLight" 
... a lot of NS ... 
mc:Ignorable="d" 
xmlns:vm="clr-namespace:TryERP2.Financeiro.ViewModel" 
d:DesignHeight="150" d:DesignWidth="245" SizeToContent="WidthAndHeight"> 
... etc 

我,改成一個用戶控制,而不是一個窗口固定它,它完美地工作。

我的新ViewLight.xaml:

<UserControl x:Class="TryERP2.Financeiro.View.ViewLight" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     mc:Ignorable="d" 
     xmlns:vm="clr-namespace:TryERP2.Financeiro.ViewModel" 

     d:DesignHeight="150" d:DesignWidth="245"> 
    <UserControl.Resources> 
     <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" /> 
    </UserControl.Resources> 

    <Grid DataContext="{Binding Light, Source={StaticResource Locator}}"> 
     <Button Content="{Binding MyButtonText}" HorizontalAlignment="Stretch" Name="button1" VerticalAlignment="Stretch" /> 
    </Grid> 
</UserControl> 

我的新ViewLight.xaml.cs(代碼隱藏):

public partial class ViewLight : UserControl 
{ 
    public ViewLight() 
    { 
     InitializeComponent(); 
    } 
} 

而且視圖模型仍然是相同的。

0

在ViewLight.xaml你應該改變

<Window.Resources> 
    <ResourceDictionary> 
     <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" /> 
    </ResourceDictionary> 
</Window.Resources> 

<Window.Resources> 
     <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" /> 
</Window.Resources> 

因爲Window.Resources已經是一個資源字典,你在它創建另一個。您不能在此訪問定位器資源:

<Grid DataContext="{Binding Light, Source={StaticResource Locator}}"> 

另一個選項是使用MergedDictionary。

+0

嗨...我已經嘗試刪除標記,但這沒有效果。關於綁定,我只是將它更改爲標籤,因爲我收到錯誤。 DataContext屬性的原始位置是標記。 – 2012-01-15 23:42:58

+0

當它位於Window標記中時,我收到了以下錯誤消息:「找不到名爲'Locator'的資源,資源名稱區分大小寫。」它曾在一個簡單的MvvmLight應用程序上工作,資源位於標記中。 – 2012-01-15 23:49:17

+0

你有什麼錯誤?檢查vs中的輸出(在調試模式下)以捕獲綁定錯誤。 – 2012-01-16 07:09:28