2012-02-29 57 views
1

我收到了在構建一個XAML資源文件中的錯誤鍵值這裏是XAML文件:WPF錯誤「缺少的對象

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:s="clr-namespace:DiagramDesigner" 
        xmlns:c="clr-namespace:DiagramDesigner.Controls" 
        xmlns:r="clr-namespace:Automation.Data;assembly=Automation" 
        xmlns:a="clr-namespace:Automation;assembly=Automation" > 
    <DataTemplate x:Key="WorkflowDropdownSetting"> 
     <DataTemplate.Resources> 
      <s:WorkflowDropdown x:Key="settingFlowList" /> 
     </DataTemplate.Resources> 
     <StackPanel Orientation="Horizontal"> 
      <ComboBox ItemsSource="{StaticResource settingFlowList}" Width="200" SelectedValue="{Binding Path=DefaultValue, Mode=TwoWay}" DisplayMemberPath="Name" SelectedValuePath="Id"/> 
      <Button x:Name="btnEditWorkflow" Command="{x:Static s:WorkflowUICommands.EditWorkflow}" CommandParameter="{Binding Path=DefaultValue}">Edit...</Button> 
     </StackPanel> 
    </DataTemplate> 
</ResourceDictionary> 

這裏是WorkflowList.cs來源:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Automation; 
using System.Collections.ObjectModel; 
using Automation.Data; 

namespace DiagramDesigner 
{ 
    public class WorkflowList : ObservableCollection<Workflow> 
    { 
     public WorkflowList() 
     { 
      AutomationDataContext cntxt = Engine.Data; 
      IEnumerable<Workflow> lst = Engine.Data.Workflows.AsEnumerable<Workflow>(); 

      foreach (Workflow flow in lst) 
      { 
       Add(flow); 
      } 
     } 
    } 
} 

我使用這個對象在另一個XAML(而不是資源文件)的資源,它工作正常:

<Window x:Class="DiagramDesigner.WorkflowMain" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:s="clr-namespace:DiagramDesigner" 
    Title="WorkflowMain" SizeToContent="Width" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"> 
    <ScrollViewer Grid.IsSharedSizeScope="True"> 
     <ScrollViewer.Resources> 
      <ResourceDictionary> 
       <s:WorkflowList x:Key="flows"/> 
       <Brush x:Key="BrdrBrush">#D6FF9436</Brush> 
       <Style TargetType="TextBlock"> 
        <Setter Property="HorizontalAlignment" Value="Stretch"/> 
        <Setter Property="VerticalAlignment" Value="Stretch"/> 
       </Style> 
       <Style TargetType="WrapPanel"> 
        <Setter Property="Margin" Value="2"/> 
        <Setter Property="Background" Value="PowderBlue"/> 
        <Setter Property="VerticalAlignment" Value="Stretch"/> 
       </Style> 
       <Style x:Key="Heading" TargetType="TextBlock"> 
        <Setter Property="FontSize" Value="20"/> 
        <Setter Property="FontWeight" Value="bold"/> 
       </Style> 
       <Style x:Key="ColumnHeader" TargetType="Label"> 
        <Setter Property="FontWeight" Value="bold"/> 
       </Style> 
      </ResourceDictionary> 
     </ScrollViewer.Resources> 
     <Border BorderThickness="1" 
      Padding="4" 
      BorderBrush="{StaticResource BrdrBrush}" 
      Background="AliceBlue" 
      SnapsToDevicePixels="True" HorizontalAlignment="Stretch"> 
      <StackPanel> 
       <Grid> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition SharedSizeGroup="Job"></ColumnDefinition> 
         <ColumnDefinition SharedSizeGroup="Name"></ColumnDefinition> 
         <ColumnDefinition SharedSizeGroup="Description"></ColumnDefinition> 
         <ColumnDefinition SharedSizeGroup="FirstAction"></ColumnDefinition> 
         <ColumnDefinition SharedSizeGroup="Button"></ColumnDefinition> 
        </Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition/> 
         <RowDefinition/> 
         <RowDefinition/> 
        </Grid.RowDefinitions> 
        <WrapPanel Grid.Row="0" Grid.ColumnSpan="5"> 
         <TextBlock Text="Workflows:" Style="{StaticResource Heading}"/> 
        </WrapPanel> 
        <WrapPanel Grid.Column="0" Grid.Row="1"> 
         <Label Style="{StaticResource ColumnHeader}">Name:</Label> 
        </WrapPanel> 
        <WrapPanel Grid.Column="1" Grid.Row="1"> 
         <Label Style="{StaticResource ColumnHeader}">Name:</Label> 
        </WrapPanel> 
        <WrapPanel Grid.Column="2" Grid.Row="1"> 
         <Label Style="{StaticResource ColumnHeader}">Description:</Label> 
        </WrapPanel> 
        <WrapPanel Grid.Column="3" Grid.Row="1"> 
         <Label Style="{StaticResource ColumnHeader}">First Task:</Label> 
        </WrapPanel> 
        <WrapPanel Grid.Column="4" Grid.Row="1"> 
        </WrapPanel> 
       </Grid> 
       <ItemsControl ItemsSource="{Binding Source={StaticResource flows}}" Grid.Row="2" Grid.ColumnSpan="5" HorizontalAlignment="Stretch"> 
        <ItemsControl.Template> 
         <ControlTemplate> 
          <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" /> 
         </ControlTemplate> 
        </ItemsControl.Template> 
        <ItemsControl.ItemsPanel> 
         <ItemsPanelTemplate> 
          <StackPanel Margin="0,5,0,5"/> 
         </ItemsPanelTemplate> 
        </ItemsControl.ItemsPanel> 
        <ItemsControl.ItemTemplate> 
         <DataTemplate> 
          <DataTemplate.Resources> 
           <Style TargetType="{x:Type WrapPanel}"> 
            <Setter Property="Margin" Value="2"/> 
            <Setter Property="Background" Value="PowderBlue"/> 
            <Setter Property="MinHeight" Value="30" /> 
            <Setter Property="VerticalAlignment" Value="Stretch"/> 
           </Style> 
          </DataTemplate.Resources> 
          <Grid Background="AliceBlue" Margin="0 0 0 5" HorizontalAlignment="Stretch"> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition SharedSizeGroup="Job"></ColumnDefinition> 
            <ColumnDefinition SharedSizeGroup="Name"></ColumnDefinition> 
            <ColumnDefinition SharedSizeGroup="Description"></ColumnDefinition> 
            <ColumnDefinition SharedSizeGroup="FirstAction"></ColumnDefinition> 
            <ColumnDefinition SharedSizeGroup="Button"></ColumnDefinition> 
           </Grid.ColumnDefinitions> 
           <WrapPanel> 
            <TextBlock Text="{Binding Path=Name}" Grid.Column="1"/> 
           </WrapPanel> 
           <WrapPanel> 
            <TextBlock Text="{Binding Path=Job.Name}" Grid.Column="0"/> 
           </WrapPanel> 
           <WrapPanel Grid.Column="2"> 
            <TextBlock Text="{Binding Path=Description}"/> 
           </WrapPanel> 
           <WrapPanel Grid.Column="3"> 
            <TextBlock Text="{Binding Path=FirstAction.Name}"/> 
           </WrapPanel> 
           <WrapPanel Grid.Column="4"> 
            <Button Name="editButton" Command="{x:Static s:WorkflowUICommands.EditWorkflow}" CommandParameter="{Binding}" IsDefault="True" VerticalAlignment="Stretch">Edit...</Button> 
           </WrapPanel> 
          </Grid> 
         </DataTemplate> 
        </ItemsControl.ItemTemplate> 
       </ItemsControl> 
       <Button Name="btnNewWorkflow" Command="{x:Static s:WorkflowUICommands.NewWorkflow}" >New Workflow...</Button> 
      </StackPanel> 
     </Border> 
    </ScrollViewer> 
</Window> 

當我建我看到這個錯誤:

Error 5 Missing key value on 'WorkflowDropdown' object. C:\Source\Projects\devtest\UI\WorkflowUI\DiagramDesigner\Resources\SettingTemplates.xaml 7 2 DiagramDesigner 

The only question我能找到的關於因爲我使用X這個錯誤並不適用:關鍵了。

該解決方案運行得很好,但錯誤阻止了設計者的互動。

XamlObjectWriterException was thrown on "DataTemplate":Missing key value on 'WorkflowList' object. Click here to hide detail. 

An Unhandled Exception has occurred 
Missing key value on 'WorkflowList' object. 
at System.Xaml.XamlObjectWriter.GetKeyFromInstance(Object instance, XamlType instanceType, IAddLineInfo lineInfo) 
at System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentCollection(ObjectWriterContext ctx) 
at System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentProperty(ObjectWriterContext ctx) 
at System.Xaml.XamlObjectWriter.WriteEndObject() 
at System.Xaml.XamlWriter.WriteNode(XamlReader reader) 
at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector) 
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) 

任何幫助將不勝感激,因爲我似乎只是紡紗我的車輪:直到您嘗試與其進行交互,然後將其與在頂部列出了以下異常禁用它顯示的罰款。

+0

從「Error 5」消息看來,錯誤出現在第44行,並且上面的代碼片段中沒有44行代碼 - 此處的代碼縮寫,或者您可能沒有檢查實際加載的文件? – XAMeLi 2012-03-01 14:51:45

+0

代碼縮寫。我編輯併發布了實際的錯誤。謝謝。 – JonathanWardRogers 2012-03-01 15:59:40

回答

0

我沒有答案,但可以告訴設計師對於自定義類,屬性等非常敏感和不穩定,特別是當它們位於項目的子文件夾中時。