-1

I keep running into the below error message in vb.net, but in the same project done in C#, this works perfectly. After manually converting the project from C# to VB, this is where the error arises. Any suggestions would be appreciated.

enter image description here

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);
    }

}
}
0

2 Answers 2

3

The problem isn't with the XML, it's with your string concatenation. 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'. Instead use &:

stringElements(i).AppendChild(toastXml.CreateTextNode("Line " & i))

Hope that helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh that's right! Good catch, it's always the small things isn't it?
Heh, yep. You get stuck assuming it must be something in the complex parts!
1

The + operator is different between languages.

To concatenate in VB, use & instead.

Dim stringElements As XmlNodeList = toastXml.GetElementsByTagName("text")
For i As Integer = 0 To stringElements.Length
    stringElements(i).AppendChild(toastXml.CreateTextNode("Line " & i))
Next

1 Comment

Thanks! Worked fine after doing proper concatenation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.