C# 多次调用我的WCF服务方法

C# 多次调用我的WCF服务方法,c#,.net,silverlight,mvvm,C#,.net,Silverlight,Mvvm,我更新了我的问题,提出了以下简短而直接的问题: 我知道MVVM的基本知识,但不知道它的实现。 问题:-- 这是第一次使用所有方法,例如private void UserControl_Loaded()、void serviceClient_getDeptNameCompleted()、private void cbDeptName_SelectionChanged()、void serviceClient_GetSectionsListCompleted()、private void cbDe

我更新了我的问题,提出了以下简短而直接的问题: 我知道MVVM的基本知识,但不知道它的实现。 问题:--

  • 这是第一次使用所有方法,例如private void UserControl_Loaded()、void serviceClient_getDeptNameCompleted()、private void cbDeptName_SelectionChanged()、void serviceClient_GetSectionsListCompleted()、private void cbDeptSections_SelectionChanged()、void serviceClient_GetSectionDetailCompleted()等一次又一次的连续呼叫
  • 如果我正在更改组合框中的任何选择,那么每个方法都会调用多次(变量)
我认为这是因为糟糕的实现或低标准的编码。能否请任何机构向我推荐一个好的实施方案。提前谢谢

更新:- 我的a.xml


1.
艺术类
A.
政治学
5.
B
经济
3.
C
社会科学课程
7.
D
历史
5.
E
地理
2.
2.
科学
A.
物理
10
B
数学
2.
C
化学
6.
D
植物
5.
E
玉米学
4.
3.
商业
A.
会计
3.
B
营销
2.
C
人力资源
5.
D
财务
5.
4.
图书馆
A.
掌管
3.
B
店员
3.

我的WCFService.cs类(Service1.svc.cs)

//注意:您可以使用“重构”菜单上的“重命名”命令同时更改代码、svc和配置文件中的类名“Service1”。
公共类服务1:IService1
{
xml文档文档;
字符串xmlPath=@“D:\WcfService1\WcfService1\Resources\a.xml”;
公共字典GetDeptIDName()
{
尝试
{
如果(!File.Exists(xmlPath))
{
返回null;
}
其他的
{
Dictionary depIDName=新字典();
doc=新的XmlDocument();
文档加载(xmlPath);
XmlNodeList nl=doc.GetElementsByTagName(“部门”);
foreach(nl中的XmlNode节点)
{
字符串id=(node.FirstChild.InnerXml).Replace(“\r”,”).Replace(“\n”,”).Replace(“\t”,”);
字符串名称=(node.FirstChild.NextSibling.InnerXml).Replace(“\r”,”).Replace(“\n”,”).Replace(“\t”,”);
depid name.Add(id,name);
}
返回depIDName;
}
}
捕获(例外)
{
返回null;
}
}
公共列表GetSectionsList(字符串deptName)
{
尝试
{
如果(!File.Exists(xmlPath))
{
返回null;
}
其他的
{
List lstSectionList=新列表();
doc=新的XmlDocument();
文档加载(xmlPath);
XmlNodeList nl=doc.GetElementsByTagName(“部门”);
foreach(nl中的XmlNode节点)
{
字符串名称=(node.FirstChild.NextSibling.InnerXml).Replace(“\r”,”).Replace(“\n”,”).Replace(“\t”,”);
if(name==deptName)
{
UpdateSectionsList(参考lstSectionList,node.LastChild);
}
}
返回列表;
}
}
捕获(例外)
{
返回null;
}
}
私有void UpdateSectionsList(参考列表lstSectionList,
XmlNode(XmlNode)
{
XmlNodeList nl=xmlNode.ChildNodes;
foreach(nl中的XmlNode xNode)
{
foreach(XmlNodeList中的XmlNode sectionNode)xNode.ChildNodes
{
如果(“sectionID”==sectionNode.Name)
{
lstSectionList.Add(sectionNode.InnerXml);
}
}
}
}
/// 
///获取所有部分的详细信息。
/// 
/// 
/// 
/// 
public CompositeType GetSectionDetail(字符串deptName,
字符串段ID)
{
尝试
{
如果(!File.Exists(xmlPath))
{
<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>
// 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;
            }
        }
    }
// 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;
            }
        }
    }
 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);
        }
    }
<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"
            />
<?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>
serviceClient.GetSectionDetailCompleted += new EventHandler<GetSectionDetailCompletedEventArgs>(serviceClient_GetSectionDetailCompleted);
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();
}