[英]XmlNode AppendChild works in C# but not VB.Net
I keep running into the below error message in vb.net, but in the same project done in C#, this works perfectly. 我一直在vb.net中遇到以下错误消息,但是在用C#完成的同一项目中,这完美地工作了。 After manually converting the project from C# to VB, this is where the error arises.
手动将项目从C#转换为VB之后,就会出现错误。 Any suggestions would be appreciated.
任何建议,将不胜感激。
Vb.Net: Vb.Net:
Const App_ID As String = "WindowsToastTest"
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
' Get a toast XML template
Dim toastXml As XmlDocument = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText03)
' Fill in the text elements
Dim stringElements As XmlNodeList = toastXml.GetElementsByTagName("text")
For i As Integer = 0 To stringElements.Length
stringElements(i).AppendChild(toastXml.CreateTextNode("Line " + i))
Next
' Specify the absolute path to an image
Dim imagePath As String = "file:///" + Path.GetFullPath("toastImageAndText.png")
Dim imageElements As XmlNodeList = toastXml.GetElementsByTagName("image")
imageElements(0).Attributes.GetNamedItem("src").NodeValue = imagePath
' Create the toast And attach event listeners
Dim toast As ToastNotification = New ToastNotification(toastXml)
' Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast)
End Sub
C#: C#:
namespace ToastSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private const String APP_ID = "ToastSample";
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// Get a toast XML template
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText03);
// Fill in the text elements
XmlNodeList stringElements = toastXml.GetElementsByTagName("text");
for (int i = 0; i < stringElements.Length; i++)
{
stringElements[i].AppendChild(toastXml.CreateTextNode("Line " + i));
}
// Specify the absolute path to an image
String imagePath = "file:///" + Path.GetFullPath("toastImageAndText.png");
XmlNodeList imageElements = toastXml.GetElementsByTagName("image");
imageElements[0].Attributes.GetNamedItem("src").NodeValue = imagePath;
// Create the toast and attach event listeners
ToastNotification toast = new ToastNotification(toastXml);
// Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
}
}
}
The problem isn't with the XML, it's with your string concatenation. 问题不在于XML,而在于字符串连接。 Concat strings by & and + in VB.Net When you use
string + number
in vb it tries to cast the string to a number, which is why you're getting the error 'Conversion from string "Line" to type 'Double' is invalid'. 在VB.Net中用&和+构造Concat字符串时,在vb中使用
string + number
,它会尝试将字符串转换为数字,这就是为什么出现错误“从字符串“ Line”转换为类型“ Double”的原因)无效'。 Instead use &: 而是使用&:
stringElements(i).AppendChild(toastXml.CreateTextNode("Line " & i))
Hope that helps. 希望能有所帮助。
The + operator is different between languages. 语言之间的+运算符不同。
To concatenate in VB, use & instead. 要在VB中串联,请使用&代替。
Dim stringElements As XmlNodeList = toastXml.GetElementsByTagName("text")
For i As Integer = 0 To stringElements.Length
stringElements(i).AppendChild(toastXml.CreateTextNode("Line " & i))
Next
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.