2010-07-23 26 views
3

我正在使用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> 

應用程序編譯並運行。該錯誤僅在設計時發生。

你能告訴我我做錯了什麼嗎?

預先感謝您。

回答

3

這是一個已知問題。由於某種原因混合不識別靜態全局資源。

作爲解決方法,您可以在視圖中創建ViewModelLocator的本地資源。

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

您必須包含ViewModel命名空間。

The issue is reported in codeplex here

and in stackoverflow here

看來,這是在Blend解決4

+0

感謝您的幫助,但它仍然無法正常工作。我將資源添加到視圖中,我想添加自定義用戶控件,並且錯誤仍然存​​在。我必須改變別的東西嗎? 編輯:它通過將定位器作爲本地資源添加到窗口和用戶控件並從app.xaml中刪除它,現在可以工作。我認爲這對設計時間是可以的,因爲它在混合4中得到解決,我可以忍受這一點。謝謝 – obivandamme 2010-07-28 09:54:34

+1

我很確定它在Blend 4中沒有解決。還有其他一些聰明的技巧嗎? – 2010-08-26 01:04:18

+0

@Simon_Weaver:似乎不像你說的那樣。但工作正常與現在都設計:( – Amsakanna 2010-09-10 10:40:35

0

這是很難診斷沒有看到的代碼,但讓我刺穿它無論如何。

這聽起來像問題是你的定位器的接線。請確保您的App.xaml中包含以下代碼:

<Application.Resources> 
    <local:NameOfMyViewModelLocatorClass x:Key="Locator" /> 
</Application.Resources> 

這應該將所有內容連接在一起。

+0

完成了。我使用了mvvm light工具包,並且一切都正確連接。如前所述,它在運行時工作。該錯誤僅在設計時存在。當我回到我的辦公室時,我將在星期一發布代碼 – obivandamme 2010-07-24 17:55:29

3

,我想出了一個相當不錯的解決此問題的,因爲它似乎並沒有被固定在Blend 4:

在您的XAML UserControl的構造函數中只需添加它所需的資源,前提是您在Blend中處於設計模式:

public partial class OrdersControl : UserControl 
{ 
    public OrdersControl() 
    { 
     // MUST do this BEFORE InitializeComponent() 
     if (DesignerProperties.GetIsInDesignMode(this)) 
     { 
      if (AppDomain.CurrentDomain.BaseDirectory.Contains("Blend 4")) 
      { 
       // load styles resources 
       ResourceDictionary rd = new ResourceDictionary(); 
       rd.Source = new Uri(System.IO.Path.Combine(Environment.CurrentDirectory, "Resources/Styles.xaml"), UriKind.Absolute); 
       Resources.MergedDictionaries.Add(rd); 

       // load any other resources this control needs such as Converters 
       Resources.Add("booleanNOTConverter", new BooleanNOTConverter()); 
      } 
     } 

     // initialize component 
     this.InitializeComponent(); 

} 

可能有一些邊緣情況,但它在我得到一個大的紅色錯誤符號之前的簡單情況下工作正常。我很樂意看到如何更好地解決這個問題的建議,但這至少可以讓我爲用戶控件添加動畫,否則這些控件會顯示爲錯誤。

你也可以提取出資源的創建App.xaml.cs

internal static void CreateStaticResourcesForDesigner(Control element) 
    { 
     if (AppDomain.CurrentDomain.BaseDirectory.Contains("Blend 4")) 
     { 
      // load styles resources 
      ResourceDictionary rd = new ResourceDictionary(); 
      rd.Source = new Uri(System.IO.Path.Combine(Environment.CurrentDirectory, "Resources/Styles.xaml"), UriKind.Absolute); 
      element.Resources.MergedDictionaries.Add(rd); 

      // load any other resources this control needs 
      element.Resources.Add("booleanNOTConverter", new BooleanNOTConverter()); 
     } 
    } 

,然後在控制在此之前的InitializeComponent():

 // create local resources 
    if (DesignerProperties.GetIsInDesignMode(this)) 
    { 
     App.CreateStaticResourcesForDesigner(this); 
    } 
+0

This!Just saved my day。 – 2014-08-20 01:16:18

-1

那麼在設計甚至刪除問題您需要在ViewModelLocator的構造函數中包含以下條件:

if (ViewModelBase.IsInDesignModeStatic) 
{ 
} 
else 
{ 
    //Include function to create ViewModel here like the following 
    CreateMain(); 
} 

希望這可以解決問題。

相關問題