我正在使用mvvm light工具包創建WPF應用程序。我創建了一個用戶控件和一個相應的ViewModel。我在ViewModelLocator中創建了一個ViewModel屬性。我將用戶控件datacontext綁定到Locator類中的屬性。當我在Blend或VS Designer中編輯用戶控件時,一切似乎都奏效,因爲我可以看到我的設計時間數據。「無法找到名稱爲{Locator}的資源」使用mvvm-light用戶控件時出錯
當我現在嘗試在主窗口上使用我的用戶控件,該窗口由工具包的wpf模板創建時,我收到錯誤「無法找到名稱爲{Locator}的資源」,並且與我的用戶mainwindow.xaml中的控件在Blend中用紅線標記。在Visual Studio中,同一行用錯誤標記:「無法創建MyView類型的實例」。
編輯: 這是App.xaml中的源代碼
<Application.Resources>
<!--Global View Model Locator-->
<vm:ViewModelLocator x:Key="Locator"
d:IsDataSource="True">
</Application.Resources>
這是對EditCustomerView.xaml
<UserControl.DataContext>
<Binding Path="EditCustomer" Source="{StaticResource Locator}" />
</UserControl.DataContext>
此代碼是在我的主窗口中的代碼
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Window.DataContext>
<Binding Path="Main" Source="{StaticResource Locator}"/>
</Window.DataContext>
<Grid x:Name="LayoutRoot" Background="{DynamicResource BasicBackground}">
<Grid.RowDefinitions>
<RowDefinition Height="0.927*"/>
<RowDefinition Height="0.073*"/>
</Grid.RowDefinitions>
<ListBox Margin="4" SelectedItem="{Binding Main.SelectedCustomer, Mode=Default, Source={StaticResource Locator}}" ItemTemplate="{DynamicResource CustomerTemplate}" ItemsSource="{Binding Customers, Mode=Default}"/>
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Content="Edit" Grid.Row="1" Command="{Binding EditCustomerCommand, Mode=Default}"/>
<Border x:Name="border" Opacity="0.75" Grid.RowSpan="2" Background="#FF706F6F" BorderBrush="Black" BorderThickness="1" Visibility="{Binding EditViewVisibility, Mode=Default}">
<views:EditCustomerView HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</Grid>
應用程序編譯並運行。該錯誤僅在設計時發生。
你能告訴我我做錯了什麼嗎?
預先感謝您。
感謝您的幫助,但它仍然無法正常工作。我將資源添加到視圖中,我想添加自定義用戶控件,並且錯誤仍然存在。我必須改變別的東西嗎? 編輯:它通過將定位器作爲本地資源添加到窗口和用戶控件並從app.xaml中刪除它,現在可以工作。我認爲這對設計時間是可以的,因爲它在混合4中得到解決,我可以忍受這一點。謝謝 – obivandamme 2010-07-28 09:54:34
我很確定它在Blend 4中沒有解決。還有其他一些聰明的技巧嗎? – 2010-08-26 01:04:18
@Simon_Weaver:似乎不像你說的那樣。但工作正常與現在都設計:( – Amsakanna 2010-09-10 10:40:35