2012-01-14 86 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