Reputation: 3240
I have updated my questions with a short and direct questions as mentioned below: I am aware of basic knowledge on MVVM, but I do not know its implementation. Problems:--
I think its because of bad implementation or low standard coding. Can any body please suggest me with a good implementation. Thanks in advance.
UPDATE:- my 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>
my WCFService.cs class(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 Interface
// 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 class
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>
Upvotes: 1
Views: 2730
Reputation: 4568
I think the culprits to your multiple calls may be
serviceClient.GetSectionDetailCompleted += new EventHandler<GetSectionDetailCompletedEventArgs>(serviceClient_GetSectionDetailCompleted);
Seems to me that you are registering to the completion event multiple times, but never unregister from it. The more times you call cbDeptName_SelectionChanged
and cbDeptName_SelectionChanged
, the more event handlers are being created. When your service returns with a response, they're all invoked.
EDIT:
The reason those methods are invoked the first time when your app loads is because when binding the combobox controls, they automatically change their selected item to the first item in your list, thus calling the SelectionChanged
events.
EDIT 2:
To avoid multiple invocations of the same event, you should register to that event only on your Loaded
event, like you do to serviceClient.GetDeptIDNameCompleted
Your Load event should look something like this:
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();
}
Don't forget to remove these lines from where they are now in your code.
Upvotes: 3