简体   繁体   中英

VB.net WPF and Win 10 Toast Notifications

I'm trying to show toast notifications in Win 10 from vb.net WPF app.
1. I added <TargetPlatformVersion>10.0</TargetPlatformVersion> to .vbproj file in the <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> section.
2. I Added references to Windows.Data and Windows.UI
3. My code:

Class MainWindow
    Private Sub BtnClick()
        Dim ToastN As ToastNotification = New ToastNotification("Test", "This is test notification.")
    End Sub
End Class

Public Class ToastNotification
    Public Sub New(Title As String, Message As String)
        Dim TempStr As String = $"<toast>
                                    <visual>
                                        <binding template='ToastGeneric'>
                                            <text>{Title}</text>
                                            <text>{Message}</text>
                                        </binding>
                                    </visual>
                                  </toast>"
        Dim xmlDoc As Windows.Data.Xml.Dom.XmlDocument = New Windows.Data.Xml.Dom.XmlDocument()
        xmlDoc.LoadXml(TempStr)
        Dim Toast As Windows.UI.Notifications.ToastNotification = New Windows.UI.Notifications.ToastNotification(xmlDoc)
        Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier(Title).Show(Toast)
    End Sub
End Class
  1. Button's click event is set to BtnClick

Application runs but doesn't do anything. Everything works fine just ...createTestNotifier(Title).Show(Toast) doesn't work.

Could you please help. What am I doing wrong?

Source ZIP

You have to create a shortcut of your app with that:

private bool TryCreateShortcut()
    {
        string shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + string.Format("\\Microsoft\\Windows\\Start Menu\\Programs\\{0}.lnk", Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName));
        if (!File.Exists(shortcutPath))
        {
            InstallShortcut(shortcutPath);
            return true;
        }
        return false;
    }

    private void InstallShortcut(string shortcutPath)
    {
        // Find the path to the current executable
        string exePath = Process.GetCurrentProcess().MainModule.FileName;
        IShellLinkW newShortcut = (IShellLinkW)new CShellLink();

        // Create a shortcut to the exe
        ErrorHelper.VerifySucceeded(newShortcut.SetPath(exePath));
        ErrorHelper.VerifySucceeded(newShortcut.SetArguments(""));

        // Open the shortcut property store, set the AppUserModelId property
        IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut;

        using (PropVariant appId = new PropVariant(Utility.APP_ID))
        {
            ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(SystemProperties.System.AppUserModel.ID, appId));
            ErrorHelper.VerifySucceeded(newShortcutProperties.Commit());
        }

        // Commit the shortcut to disk
        IPersistFile newShortcutSave = (IPersistFile)newShortcut;

        ErrorHelper.VerifySucceeded(newShortcutSave.Save(shortcutPath, true));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM