1

以下に示すように、短くて直接的な質問で質問を更新しました。MVVM に関する基本的な知識は知っていますが、その実装については知りません。問題:--

  • 初めて、private void UserControl_Loaded()、void serviceClient_GetDeptIDNameCompleted()、private void cbDeptName_SelectionChanged()、void serviceClient_GetSectionsListCompleted()、private void cbDeptSections_SelectionChanged()、void serviceClient_GetSectionDetailCompleted() などのすべてのメソッドが 1 回ずつ順番に呼び出されます。
  • コンボボックスのいずれかから選択を変更すると、すべてのメソッドが数回 (変数) 呼び出されます。

実装が悪いか、標準コーディングが低いためだと思います。良い実装を私に提案してください。前もって感謝します。

更新:-私のa.xml

<Departments>

    <Department>

        <deptID>
            1
        </deptID>

        <deptName>
            Arts
        </deptName>

        <Sections>

            <Section>
                <sectionID>A</sectionID>
                <sectionName>Political Science</sectionName>
                <NoOfLecturers>5</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>B</sectionID>
                <sectionName>Economics</sectionName>
                <NoOfLecturers>3</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>C</sectionID>
                <sectionName>Social Studies</sectionName>
                <NoOfLecturers>7</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>D</sectionID>
                <sectionName>History</sectionName>
                <NoOfLecturers>5</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>E</sectionID>
                <sectionName>Geography</sectionName>
                <NoOfLecturers>2</NoOfLecturers>
            </Section>

        </Sections>

    </Department>

    <Department>

        <deptID>
            2
        </deptID>

        <deptName>
            Sciences
        </deptName>

        <Sections>

            <Section>
                <sectionID>A</sectionID>
                <sectionName>Physics</sectionName>
                <NoOfLecturers>10</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>B</sectionID>
                <sectionName>Mathematics</sectionName>
                <NoOfLecturers>2</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>C</sectionID>
                <sectionName>Chemistry</sectionName>
                <NoOfLecturers>6</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>D</sectionID>
                <sectionName>Botany</sectionName>
                <NoOfLecturers>5</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>E</sectionID>
                <sectionName>Zeology</sectionName>
                <NoOfLecturers>4</NoOfLecturers>
            </Section>

        </Sections>

    </Department>

    <Department>

        <deptID>
            3
        </deptID>

        <deptName>
            Commerce
        </deptName>

        <Sections>

            <Section>
                <sectionID>A</sectionID>
                <sectionName>Accounting</sectionName>
                <NoOfLecturers>3</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>B</sectionID>
                <sectionName>Marketing</sectionName>
                <NoOfLecturers>2</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>C</sectionID>
                <sectionName>Human Resources</sectionName>
                <NoOfLecturers>5</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>D</sectionID>
                <sectionName>Finance</sectionName>
                <NoOfLecturers>5</NoOfLecturers>
            </Section>

        </Sections>

    </Department>

    <Department>

        <deptID>
            4
        </deptID>

        <deptName>
            Library
        </deptName>

        <Sections>

            <Section>
                <sectionID>A</sectionID>
                <sectionName>Incharge</sectionName>
                <NoOfLecturers>3</NoOfLecturers>
            </Section>

            <Section>
                <sectionID>B</sectionID>
                <sectionName>Clerk</sectionName>
                <NoOfLecturers>3</NoOfLecturers>
            </Section>

        </Sections>

    </Department>

</Departments>

私の WCFService.cs クラス (Service1.svc.cs)

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        XmlDocument doc;

        string xmlPath = @"D:\WcfService1\WcfService1\Resources\a.xml";

        public Dictionary<string, string> GetDeptIDName()
        {
            try
            {
                if (!File.Exists(xmlPath))
                {
                    return null;
                }
                else
                {
                    Dictionary<string, string> depIDName = new Dictionary<string, string>();
                    doc = new XmlDocument();
                    doc.Load(xmlPath);
                    XmlNodeList nl = doc.GetElementsByTagName("Department");

                    foreach (XmlNode node in nl)
                    {
                        string id = (node.FirstChild.InnerXml).Replace("\r", "").Replace("\n", "").Replace("\t", "");
                        string name = (node.FirstChild.NextSibling.InnerXml).Replace("\r", "").Replace("\n", "").Replace("\t", "");
                        depIDName.Add(id, name);
                    }

                    return depIDName;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<string> GetSectionsList(string deptName)
        {
            try
            {
                if (!File.Exists(xmlPath))
                {
                    return null;
                }
                else
                {
                    List<string> lstSectionList = new List<string>();
                    doc = new XmlDocument();
                    doc.Load(xmlPath);
                    XmlNodeList nl = doc.GetElementsByTagName("Department");

                    foreach (XmlNode node in nl)
                    {
                        string name = (node.FirstChild.NextSibling.InnerXml).Replace("\r", "").Replace("\n", "").Replace("\t", "");
                        if (name == deptName)
                        {
                            UpdateSectionsList(ref lstSectionList, node.LastChild);
                        }
                    }

                    return lstSectionList;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        private void UpdateSectionsList(ref List<string> lstSectionList,
                                        XmlNode xmlNode)
        {
            XmlNodeList nl = xmlNode.ChildNodes;

            foreach (XmlNode xNode in nl)
            {
                foreach (XmlNode sectionNode in (XmlNodeList)xNode.ChildNodes)
                {
                    if ("sectionID" == sectionNode.Name)
                    {
                        lstSectionList.Add(sectionNode.InnerXml);
                    }
                }
            }
        }

        /// <summary>
        /// Get all the sections detail.
        /// </summary>
        /// <param name="deptName"></param>
        /// <param name="sectionID"></param>
        /// <returns></returns>
        public CompositeType GetSectionDetail(string deptName,
                                              string sectionID)
        {
            try
            {
                if (!File.Exists(xmlPath))
                {
                    return null;
                }
                else
                {
                    CompositeType compositeObj = new CompositeType();

                    doc = new XmlDocument();

                    doc.Load(xmlPath);

                    XmlNodeList nl = doc.GetElementsByTagName("Department");

                    foreach (XmlNode node in nl)
                    {
                        string name = (node.FirstChild.NextSibling.InnerXml).
                                       Replace("\r", "").
                                       Replace("\n", "").
                                       Replace("\t", "");

                        if (name == deptName)
                        {
                            UpdateSectionDetail(out compositeObj,
                                                GetSectionNodes(node.LastChild,
                                                                sectionID));
                        }
                    }

                    return compositeObj;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        private void UpdateSectionDetail(out CompositeType compositeObj,
                                         XmlNode xmlNode)
        {
            compositeObj =
                new CompositeType();

            compositeObj.SectionID = 
                xmlNode.FirstChild.InnerXml;

            compositeObj.SectionName = 
                xmlNode.LastChild.PreviousSibling.InnerXml;

            compositeObj.SectionLecturers =
                xmlNode.LastChild.InnerXml;
        }

        private XmlNode GetSectionNodes(XmlNode xmlNode,
                                        string sectionID)
        {
            try
            {
                XmlNodeList nl = xmlNode.ChildNodes;

                XmlNode returnNode = xmlNode;

                foreach (XmlNode xNode in nl)
                    foreach (XmlNode sectionNode in (XmlNodeList)xNode.ChildNodes)
                        if (("sectionID" == sectionNode.Name)
                                        &&
                            (sectionNode.InnerXml == sectionID))
                        {
                            returnNode = xNode;
                        }

                return returnNode;
            }
            catch(Exception)
            {
                return null;
            }
        }
    }

IService1.cs インターフェイス

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        Dictionary<string, string> GetDeptIDName();

        [OperationContract]
        List<string> GetSectionsList(string deptName);

        [OperationContract]
        CompositeType GetSectionDetail(string deptName, string sectionID);

        // TODO: Add your service operations here
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        string sectionID = string.Empty, sectionName = string.Empty, NoOfLectures = string.Empty;

        [DataMember]
        public string SectionID
        {
            get
            {
                return sectionID; 
            }
            set 
            {
                sectionID = value; 
            }
        }

        [DataMember]
        public string SectionName
        {
            get
            {
                return sectionName;
            }
            set
            {
                sectionName = value;
            }
        }
        [DataMember]
        public string SectionLecturers
        {
            get
            {
                return NoOfLectures;
            }
            set
            {
                NoOfLectures = value;
            }
        }
    }

MainPage.xaml.cs クラス

 public partial class MainPage : UserControl
    {
        Service1Client serviceClient = new Service1Client();

        public MainPage()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            serviceClient.GetDeptIDNameCompleted += new EventHandler<GetDeptIDNameCompletedEventArgs>(serviceClient_GetDeptIDNameCompleted);
            serviceClient.GetDeptIDNameAsync();
        }

        void serviceClient_GetDeptIDNameCompleted(object sender, GetDeptIDNameCompletedEventArgs e)
        {
            cbDeptName.ItemsSource = e.Result;
            cbDeptName.SelectedItem = cbDeptName.Items[0];
        }

        private void cbDeptName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //Remove square brackets from the List box item.
            string lstBoxItem = Regex.Replace(cbDeptName.SelectedValue.ToString(),
                                              @"[\[\]\s]",
                                              string.Empty);

            //Split the list box item into two parts by considering the (,)
            //to separate the DICOM ID and Patient name.
            string[] strItems = (lstBoxItem).Split(Convert.ToChar(","));

            serviceClient.GetSectionsListCompleted += new EventHandler<GetSectionsListCompletedEventArgs>(serviceClient_GetSectionsListCompleted);
            serviceClient.GetSectionsListAsync(strItems[1]);
        }

        void serviceClient_GetSectionsListCompleted(object sender, GetSectionsListCompletedEventArgs e)
        {
            cbDeptSections.ItemsSource = e.Result;
            cbDeptSections.SelectedItem = cbDeptSections.Items[0];
            //serviceClient.GetSectionDetailCompleted += new EventHandler<GetSectionDetailCompletedEventArgs>(serviceClient_GetSectionDetailCompleted);
            //serviceClient.GetSectionDetailAsync(strItems[1], cbDeptSections.SelectedItem.ToString());
        }

        private void cbDeptSections_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (null == cbDeptSections.SelectedItem)
                return;

            lbSectionDetail.Items.Clear();

            //Remove square brackets from the List box item.
            string lstBoxItem = Regex.Replace(cbDeptName.SelectedValue.ToString(),
                                              @"[\[\]\s]",
                                              string.Empty);

            //Split the list box item into two parts by considering the (,)
            //to separate the DICOM ID and Patient name.
            string[] strItems = (lstBoxItem).Split(Convert.ToChar(","));

            serviceClient.GetSectionDetailCompleted += new EventHandler<GetSectionDetailCompletedEventArgs>(serviceClient_GetSectionDetailCompleted);
            serviceClient.GetSectionDetailAsync(strItems[1], cbDeptSections.SelectedItem.ToString());
        }

        void serviceClient_GetSectionDetailCompleted(object sender, GetSectionDetailCompletedEventArgs e)
        {
            CompositeType compositeObj = e.Result;
            //lbSectionDetail.Items.Clear();
            lbSectionDetail.Items.Add(compositeObj.SectionID);
            lbSectionDetail.Items.Add(compositeObj.SectionName);
            lbSectionDetail.Items.Add(compositeObj.SectionLecturers);
        }
    }

MainPage.xaml

<StackPanel>

        <StackPanel        
            x:Name      ="LayoutRoot"
            Background  ="White"
            Orientation ="Horizontal"
            >

            <ComboBox
                x:Name                      ="cbDeptID"
                MinWidth                    ="50"
                HorizontalContentAlignment  ="Center"
                DisplayMemberPath           ="Key"
                SelectedValue               ="{Binding ElementName=cbDeptName, Path=SelectedValue, Mode=TwoWay}"
                />

            <ComboBox
                x:Name              ="cbDeptName"
                MinWidth            ="50"
                DisplayMemberPath   ="Value"
                SelectionChanged    ="cbDeptName_SelectionChanged"
                />

            <ComboBox
                x:Name              ="cbDeptSections"
                MinWidth            ="100"
                SelectionChanged    ="cbDeptSections_SelectionChanged"
                />

        </StackPanel>

        <ListBox
            x:Name                                      ="lbSectionDetail"
            ScrollViewer.HorizontalScrollBarVisibility  ="Auto"
            ScrollViewer.VerticalScrollBarVisibility    ="Auto"
            />

clientaccesspolicy.xml

<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
    <cross-domain-access>
        <policy>
            <allow-from http-request-headers="SOAPAction">
                <domain uri="*"/>
            </allow-from>
            <grant-to>
                <resource path="/" include-subpaths="true"/>
            </grant-to>
        </policy>
    </cross-domain-access>
</access-policy>
4

1 に答える 1

3

私はあなたの複数の呼び出しの犯人はかもしれないと思います

serviceClient.GetSectionDetailCompleted += new EventHandler<GetSectionDetailCompletedEventArgs>(serviceClient_GetSectionDetailCompleted);

完了イベントに複数回登録しているようですが、登録を解除することはできません。cbDeptName_SelectionChangedとを呼び出す回数が多いほどcbDeptName_SelectionChanged、より多くのイベントハンドラーが作成されます。サービスが応答とともに返されると、それらはすべて呼び出されます。

編集:これらのメソッドがアプリの読み込み時に最初に呼び出される理由は、コンボボックスコントロールをバインドするときに、選択したアイテムがリストの最初のアイテムに自動的に変更され、SelectionChangedイベントが呼び出されるためです。

編集2:同じイベントの複数の呼び出しを回避するには、次のように、イベントでのみそのイベントに登録する必要がありますLoadedserviceClient.GetDeptIDNameCompleted

Loadイベントは次のようになります。

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    // Put these here instead of where they are now
    serviceClient.GetSectionsListCompleted += new EventHandler<GetSectionsListCompletedEventArgs>(serviceClient_GetSectionsListCompleted);
    serviceClient.GetSectionDetailCompleted += new EventHandler<GetSectionDetailCompletedEventArgs>(serviceClient_GetSectionDetailCompleted);

    serviceClient.GetDeptIDNameCompleted += new EventHandler<GetDeptIDNameCompletedEventArgs>(serviceClient_GetDeptIDNameCompleted);
    serviceClient.GetDeptIDNameAsync();
}

これらの行をコード内の現在の場所から削除することを忘れないでください。

于 2012-09-25T06:10:06.707 に答える