我試圖把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
你使用什麼容器? MEF還是Unity?你可以發佈你的視圖代碼隱藏和ViewModel代碼隱藏的代碼嗎?此外,還有關於如何將V-VM注入區域的代碼。 MVVMLight,設計數據等是您在PRISM/Silverlight中處理的單獨問題。查看未顯示最有可能的組成錯誤 – katit 2012-01-14 16:24:56
我正在使用Unity。我將通過編輯問題發佈整個代碼。 – 2012-01-14 17:55:19
除了默認代碼外,View上沒有隱藏代碼。 您認爲正在運行的應用程序的解釋截圖會有幫助嗎? – 2012-01-14 18:13:34