前段时间项目中一个有多个View的Form需要使用代码来完成一些特殊的功能,网上的资料不太好找,现整理出来,全当抛砖引玉。注意:InfoPath编码思想比较特别,控件都是绑定到数据源上,修改的都是数据源,而不是通过控件的属性来修改。
1、获取非主数据源
XPathNavigator myNavigator = this.DataSources["SecondaryDataSource"].CreateNavigator();
MessageBox.Show(myNavigator.OuterXml.ToString(),"Secondary Data Source XML");
2、获取当前View的信息
if (this.CurrentView.ViewInfo.Name == "ViewName") //判断当前View名称
{ }
在public void FormEvents_ViewSwitched(object sender, ViewSwitchedEventArgs e)事件中可以使用以上判断来对特定的View进行操作。
3、获取/设置某个数据源的值,
XPathNavigator root = this.MainDataSource.CreateNavigator();
XPathNavigator nameField = root .SelectSingleNode("/my:myFields/my:MainName/my:name", NamespaceManager);
nameField.SetValue("Hello");
string >
4、遍历Repeat控件,获取用户输入的数据
string message="";
XPathNavigator root = MainDataSource.CreateNavigator();
XPathNodeIterator nameNodes = root.Select("/my:myFields/my:MainName/my:name", NamespaceManager);
while (nameNodes.MoveNext())
{
message += nameNodes.Current.Value + System.Environment.NewLine;
}
MessageBox.Show(message);
5、动态添加控件
CurrentView.ExecuteAction(ActionType.XCollectionInsert, "Group_1");
6、使数据源所对应的文本值处于选中状态
XPathNavigator textNode = CreateNavigator().SelectSingleNode("/my:myFields/my:field1", NamespaceManager);
CurrentView.SelectText(textNode);
CurrentView.ExecuteAction(ActionType.Cut);
7、调用WebService
// Open connection.
WebServiceConnection wsc = (WebServiceConnection)this.DataConnections["Main query"];
// Create XmlDocuments.
XmlDocument inputDocument = new XmlDocument();
XmlDocument outputDocument = new XmlDocument();
XmlDocument errorsDocument = new XmlDocument();
// Load input document.
inputDocument.LoadXml("<inputValue1>Test</inputValue1><inputValue2>5</inputValue2>");
// Create XPathNavigator objects for documents.
XPathNavigator inputNav = inputDocument.CreateNavigator();
XPathNavigator outputNav = outputDocument.CreateNavigator();
XPathNavigator errorsNav = errorsDocument.CreateNavigator();
// Call Execute method.
wsc.Execute(inputNav, outputNav, errorsNav);
获取Repeat控件内部的数据时,我采用DataSet存储,因为我发现遍历每个Repeat时,无法遍历其下子控件的值。比如。Repeat DataTable有3个,子控件TextBox控件的值也有3个。通过XPathNodeIterator实例遍历DataTable的时候无法获取对应的TextBox的值,默认都是获取第一次出现的值,所以只好借助于DataRow分别遍历给不同的Column赋值。不知道园子里谁有更好的方法,请告知。谢谢
1、获取非主数据源
XPathNavigator myNavigator = this.DataSources["SecondaryDataSource"].CreateNavigator();
MessageBox.Show(myNavigator.OuterXml.ToString(),"Secondary Data Source XML");
2、获取当前View的信息
if (this.CurrentView.ViewInfo.Name == "ViewName") //判断当前View名称
{ }
在public void FormEvents_ViewSwitched(object sender, ViewSwitchedEventArgs e)事件中可以使用以上判断来对特定的View进行操作。
3、获取/设置某个数据源的值,
XPathNavigator root = this.MainDataSource.CreateNavigator();
XPathNavigator nameField = root .SelectSingleNode("/my:myFields/my:MainName/my:name", NamespaceManager);
nameField.SetValue("Hello");
string >
4、遍历Repeat控件,获取用户输入的数据
string message="";
XPathNavigator root = MainDataSource.CreateNavigator();
XPathNodeIterator nameNodes = root.Select("/my:myFields/my:MainName/my:name", NamespaceManager);
while (nameNodes.MoveNext())
{
message += nameNodes.Current.Value + System.Environment.NewLine;
}
MessageBox.Show(message);
5、动态添加控件
CurrentView.ExecuteAction(ActionType.XCollectionInsert, "Group_1");
6、使数据源所对应的文本值处于选中状态
XPathNavigator textNode = CreateNavigator().SelectSingleNode("/my:myFields/my:field1", NamespaceManager);
CurrentView.SelectText(textNode);
CurrentView.ExecuteAction(ActionType.Cut);
7、调用WebService
// Open connection.
WebServiceConnection wsc = (WebServiceConnection)this.DataConnections["Main query"];
// Create XmlDocuments.
XmlDocument inputDocument = new XmlDocument();
XmlDocument outputDocument = new XmlDocument();
XmlDocument errorsDocument = new XmlDocument();
// Load input document.
inputDocument.LoadXml("<inputValue1>Test</inputValue1><inputValue2>5</inputValue2>");
// Create XPathNavigator objects for documents.
XPathNavigator inputNav = inputDocument.CreateNavigator();
XPathNavigator outputNav = outputDocument.CreateNavigator();
XPathNavigator errorsNav = errorsDocument.CreateNavigator();
// Call Execute method.
wsc.Execute(inputNav, outputNav, errorsNav);
获取Repeat控件内部的数据时,我采用DataSet存储,因为我发现遍历每个Repeat时,无法遍历其下子控件的值。比如。Repeat DataTable有3个,子控件TextBox控件的值也有3个。通过XPathNodeIterator实例遍历DataTable的时候无法获取对应的TextBox的值,默认都是获取第一次出现的值,所以只好借助于DataRow分别遍历给不同的Column赋值。不知道园子里谁有更好的方法,请告知。谢谢