[英]XmlNode AppendChild works in C# but not VB.Net
我一直在vb.net中遇到以下錯誤消息,但是在用C#完成的同一項目中,這完美地工作了。 手動將項目從C#轉換為VB之后,就會出現錯誤。 任何建議,將不勝感激。
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#:
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);
}
}
}
問題不在於XML,而在於字符串連接。 在VB.Net中用&和+構造Concat字符串時,在vb中使用string + number
,它會嘗試將字符串轉換為數字,這就是為什么出現錯誤“從字符串“ Line”轉換為類型“ Double”的原因)無效'。 而是使用&:
stringElements(i).AppendChild(toastXml.CreateTextNode("Line " & i))
希望能有所幫助。
語言之間的+運算符不同。
要在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.