0

我在这里很新,因此,我的问题可能不像你习惯的那样清楚。不过,我希望你能给我一个提示,以解决我的问题。目前,我正在使用带有 Authoring Extensions 的 Visual Studio 2013 为带有 UR 7 的 System Center Service Manager 2012 R2 开发自定义更改请求表。此表单基于带有 .Net-FrameWork 3.5 的 WPF 用户控件 (Visual C#)。我的目标是开发一个列表视图,我想在其中添加一个添加按钮而不是一个文件附件到该视图。代码如下所示:

panagement 包中的类型投影:

<TypeProjection ID="TypeProjection.RFCMinor" Accessibility="Public" Type="RFC.Minor.RFCMinor">
      <Component Path="$Target/Path[Relationship='WorkItem!System.WorkItemHasFileAttachment']$" Alias="FileAttachments">
        <Component Path="$Target/Path[Relationship='SupportingItem!System.FileAttachmentAddedByUser']$" Alias="FileAttachmentAddedBy" />
      </Component>
    </TypeProjection>

xaml-Code 中的列表视图项:

<ListView x:Name="lvAttachedRiskRelFiles" MinHeight="70" ItemsSource="{Binding Path=FileAttachments, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                                    MouseDoubleClick="lvAttachedRiskRelFiles_MouseDoubleClick">
                            <ListView.View>
                                <GridView>
                                    <scwpf:SortableGridViewColumn Header="File Name" DisplayMemberBinding="{Binding FileName}" Width="Auto"/>
                                    <scwpf:SortableGridViewColumn Header="Attached By" DisplayMemberBinding="{Binding AttachedBy}" Width="Auto"/>
                                    <scwpf:SortableGridViewColumn Header="File Size (KB)" DisplayMemberBinding="{Binding FileSize}" Width="Auto"/>
                                    <scwpf:SortableGridViewColumn Header="Attached Date" DisplayMemberBinding="{Binding AttachedDate}" Width="Auto"/>
                                </GridView>
                            </ListView.View>
                            <Button Content="Add" Name="btnAddRiskFile" Click="btnAddRiskFile_Click"/>

C#代码目前看起来像:

private void btnAddRiskFile_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        // Create OpenFileDialog 
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
        Nullable<bool> result = dlg.ShowDialog();
        // Get the selected file name 
        if (result == true)
        {
            // Open document 
            string filename = dlg.FileName;
            AddFileToListView(filename);
        }
    }
internal void AddFileToListView(String sFile)
    {
        //emg: current EnterpriseManagementGroup

        //System.SupportingItem.Library; ManagementPack ID: 23e3ae8e-1981-8560-2e55-8730cbc04965
        ManagementPack mpSupporting =
           emg.ManagementPacks.GetManagementPack(new Guid("23e3ae8e-1981-8560-2e55-8730cbc04965"));

        //Get the System.FileAttachment class
        ManagementPackClass mpcAttachment = emg.EntityTypes.GetClass("System.FileAttachment", mpSupporting);

        //Get attachment details
        string sExt = Path.GetExtension(sFile);
        string sAttachmentName = Path.GetFileNameWithoutExtension(sFile);

        //Create new stream and read file into memory
        MemoryStream ms = new MemoryStream();

        using (FileStream fs = File.OpenRead(sFile))
        {
            ms.SetLength(fs.Length);
            fs.Read(ms.GetBuffer(), 0, (int)fs.Length);
        }

        //Did we get any data?
        if (ms != null && ms.Length != 0)
        {
            //Reset stream position
            ms.Position = 0;

            //Create a new attachment
            CreatableEnterpriseManagementObject cemoAttachment = new CreatableEnterpriseManagementObject(emg, mpcAttachment);

            //Create a new attachment relationship from SupportingItem -> File Attachment (via "HasFileAttachment: ID =aa8c26dc-3a12-5f88-d9c7-753e5a8a55b4)
            //DisplayName : Has File Attachment
            //Source      : System.WorkItem
            //Target      : System.FileAttachment

            ManagementPackRelationship relAttachment =
            emg.EntityTypes.GetRelationshipClass(new Guid("aa8c26dc-3a12-5f88-d9c7-753e5a8a55b4"));
            CreatableEnterpriseManagementRelationshipObject cemroAttachment =
                new CreatableEnterpriseManagementRelationshipObject(emg, relAttachment);

            //Create a new added by user relationship
            // ID: ffd71f9e-7346-d12b-85d6-7c39f507b7bb
            // DisplayName : Added By User
            //Source      : System.FileAttachment
            //Target      : System.User

            ManagementPackRelationship relAddedByUser =
            emg.EntityTypes.GetRelationshipClass(new Guid("ffd71f9e-7346-d12b-85d6-7c39f507b7bb"));
            CreatableEnterpriseManagementRelationshipObject cemroAddedByUser =
                new CreatableEnterpriseManagementRelationshipObject(emg, relAddedByUser);

            //Set properties of attachment
            string sFileName = sAttachmentName;
            if (sExt != "") sFileName += "." + sExt;
            cemoAttachment[mpcAttachment, "AddedDate"].Value = DateTime.UtcNow;
            cemoAttachment[mpcAttachment, "DisplayName"].Value = sFileName;
            cemoAttachment[mpcAttachment, "Extension"].Value = sExt;
            cemoAttachment[mpcAttachment, "Content"].Value = ms;
            cemoAttachment[mpcAttachment, "Size"].Value = ms.Length;
            cemoAttachment[mpcAttachment, "Description"].Value = sFileName;
            cemoAttachment[mpcAttachment, "Id"].Value = Guid.NewGuid().ToString();

            //Set the source and target for the attachment and save (this must be done first)
            cemroAttachment.SetSource(emoWorkItem); //Here I do not know how to get the current WorkItem?
            cemroAttachment.SetTarget(cemoAttachment);
            cemroAttachment.Commit();

            //Set the added by user relationship (this must done after the previous relationship)
            cemroAddedByUser.SetSource(cemoAttachment);
            cemroAddedByUser.SetTarget(emoUser); //Here I do not know how to get the current User?
            cemroAddedByUser.Commit();
        }
    }

附件过程的 C#-Code 最初来自此网页。我的问题是我不知道如何宣布关系的当前用户和当前工作项。当我按下添加按钮时,文件对话框关闭并且什么都没有出现。我认为这个问题是由缺少关系引用发出的。

你们中有人知道从哪里开始解决这个问题吗?此外,你知道我能做些什么来解决它吗?

最好的问候,并在此先感谢您。

4

1 回答 1

0

对于将面临同样问题的其他人:解决方案很容易找到:

 IDataItem idiParentWorkItem = this.DataContext as IDataItem;
 EnterpriseManagementObject emoWorkItem = emg.EntityObjects.GetObject<EnterpriseManagementObject>((Guid)idiParentWorkItem["$Id$"], ObjectQueryOptions.Default);
 EnterpriseManagementObject emoUser = emg.EntityObjects.GetObject<EnterpriseManagementObject>((Guid)ConsoleContextHelper.Instance.CurrentUser["$Id$"], ObjectQueryOptions.Default);

此外,我正在寻找我想要显示的错误成员绑定。因此,我需要更改成员绑定。

总而言之,对我有用的代码如下所示:

c#:

internal void AddFileToListView(String sFile)
    {
        IDataItem idiParentWorkItem = this.DataContext as IDataItem;
        EnterpriseManagementObject emoWorkItem = emg.EntityObjects.GetObject<EnterpriseManagementObject>((Guid)idiParentWorkItem["$Id$"], ObjectQueryOptions.Default);
        EnterpriseManagementObject emoUser = emg.EntityObjects.GetObject<EnterpriseManagementObject>((Guid)ConsoleContextHelper.Instance.CurrentUser["$Id$"], ObjectQueryOptions.Default);

        //System.SupportingItem.Library; ManagementPack ID: 23e3ae8e-1981-8560-2e55-8730cbc04965
        ManagementPack mpSupporting =
           emg.ManagementPacks.GetManagementPack(new Guid("23e3ae8e-1981-8560-2e55-8730cbc04965"));

        //Get the System.FileAttachment class
        ManagementPackClass mpcAttachment = emg.EntityTypes.GetClass("System.FileAttachment", mpSupporting);

        //Get attachment details
        string sExt = Path.GetExtension(sFile);
        string sAttachmentName = Path.GetFileNameWithoutExtension(sFile);

        //Create new stream and read file into memory
        MemoryStream ms = new MemoryStream();

        using (FileStream fs = File.OpenRead(sFile))
        {
            ms.SetLength(fs.Length);
            fs.Read(ms.GetBuffer(), 0, (int)fs.Length);
        }

        //Did we get any data?
        if (ms != null && ms.Length != 0)
        {
            //Reset stream position
            ms.Position = 0;

            //Create a new attachment
            CreatableEnterpriseManagementObject cemoAttachment = new CreatableEnterpriseManagementObject(emg, mpcAttachment);

            //Create a new attachment relationship from SupportingItem -> File Attachment (via "HasFileAttachment: ID =aa8c26dc-3a12-5f88-d9c7-753e5a8a55b4)
            //DisplayName : Has File Attachment
            //Source      : System.WorkItem
            //Target      : System.FileAttachment

            ManagementPackRelationship relAttachment =
            emg.EntityTypes.GetRelationshipClass(new Guid("aa8c26dc-3a12-5f88-d9c7-753e5a8a55b4"));
            CreatableEnterpriseManagementRelationshipObject cemroAttachment =
                new CreatableEnterpriseManagementRelationshipObject(emg, relAttachment);

            //Create a new added by user relationship
            // ID: ffd71f9e-7346-d12b-85d6-7c39f507b7bb
            // DisplayName : Added By User
            //Source      : System.FileAttachment
            //Target      : System.User

            ManagementPackRelationship relAddedByUser =
            emg.EntityTypes.GetRelationshipClass(new Guid("ffd71f9e-7346-d12b-85d6-7c39f507b7bb"));
            CreatableEnterpriseManagementRelationshipObject cemroAddedByUser =
                new CreatableEnterpriseManagementRelationshipObject(emg, relAddedByUser);

            //Set properties of attachment
            string sFileName = sAttachmentName;
            if (sExt != "") sFileName += sExt;
            cemoAttachment[mpcAttachment, "AddedDate"].Value = DateTime.UtcNow;
            cemoAttachment[mpcAttachment, "DisplayName"].Value = sFileName;
            cemoAttachment[mpcAttachment, "Extension"].Value = sExt;
            cemoAttachment[mpcAttachment, "Content"].Value = ms;
            cemoAttachment[mpcAttachment, "Size"].Value = ms.Length;
            cemoAttachment[mpcAttachment, "Description"].Value = sFileName;
            cemoAttachment[mpcAttachment, "Id"].Value = Guid.NewGuid().ToString();

            //Set the source and target for the attachment and save (this must be done first)
            cemroAttachment.SetSource(emoWorkItem);
            cemroAttachment.SetTarget(cemoAttachment);

            ////Set the added by user relationship (this must done after the previous relationship)
            cemroAddedByUser.SetSource(cemoAttachment);
            cemroAddedByUser.SetTarget(emoUser);

            cemroAttachment.Commit();
            cemroAddedByUser.Commit();

            EnterpriseManagementObjectDataType emoDataType = new EnterpriseManagementObjectDataType(mpcAttachment);
            IDataItem item = emoDataType.CreateProxyInstance(cemoAttachment);

            Collection<IDataItem> items = lvAttachedRiskRelFiles.ItemsSource as Collection<IDataItem>;
            if (!items.Contains(item))
                items.Add(item);
        }
    }


xml:

<ListView x:Name="lvAttachedRiskRelFiles" ItemsSource="{Binding Path=FileAttachments, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                                    MouseDoubleClick="lvAttachedRiskRelFiles_MouseDoubleClick">
                            <ListView.View>
                                <GridView>
                                    <scwpf:SortableGridViewColumn Header="Display Name" DisplayMemberBinding="{Binding DisplayName}" Width="Auto"/>
                                    <scwpf:SortableGridViewColumn DisplayMemberBinding="{Binding FileAttachmentAddedBy.DisplayName}" Width="Auto" Header="Added By"/>
                                    <scwpf:SortableGridViewColumn DisplayMemberBinding="{Binding Size}" Width="Auto" Header="Size" />
                                    <scwpf:SortableGridViewColumn DisplayMemberBinding="{Binding AddedDate}" Width="Auto" Header="Added Date"/>
                                </GridView>
                            </ListView.View>
                        </ListView>
于 2015-10-30T08:53:51.043 回答