question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot launch notification from ViewModel

See original GitHub issue

Synopsis

This may be related to #1 , but I am having issues with getting a notification to launch using an MVVM pattern. Relevant code is below; if you’d like to take a look at the application yourself, please feel free to clone from here - if you run into any issues getting the application to start, let me know, you may need to work some magic to get the application to run in Visual Studio.

View (/View/UI/DevWindow.xaml)

...
<!--Namespace/DataContext declaration-->
xmlns:Notifications="clr-namespace:Enterwell.Clients.Wpf.Notifications.Controls;assembly=Enterwell.Clients.Wpf.Notifications"
DataContext="{Binding Main, Source={StaticResource Locator}}"
...
<!--NotificationMessageContainer is placed within a Grid-->
<Notifications:NotificationMessageContainer 
    Background="{DynamicResource WindowBackgroundBrush}"
    Foreground="{DynamicResource LabelTextBrush}"
    Manager="{Binding NotificationMessageManager}" />
...

ViewModel (/ViewModel/MainViewModel.cs)

// Instantiate NotificationMessageManager, which the View is bound to above
public INotificationMessageManager NotificationMessageManager { get; } = new NotificationMessageManager();
...
// Create the message in the constructor
public MainViewModel()
{
    try
    {
        NotificationMessageManager.CreateMessage()
            .Accent("#1751C3")
            .Animates(true)
            .AnimationInDuration(0.75)
            .AnimationOutDuration(2)
            .HasBadge("Info")
            .HasMessage("Please ingest the latest STIG Compilation Library on the settings page.")
            .Dismiss().WithButton("Dismiss", button => { })
            .Queue();
    }
    catch (Exception exception)
    {
        log.Error(string.Format("Unable to instantiate MainViewModel."));
        log.Debug("Exception details:", exception);
    }
}
...

Debugging

Running through a quick debug, NotificationMessageManager is properly instantiated and the message is created and associated to it. Similarly, looking at the visual tree, the NotificationMessageContainer exists, but is empty. Finally, no error messages are generated and logged, and my output window isn’t throwing anything concerning this control, so I have nothing to suggest that anything is failing.

Thanks in advance! I love the way this looks, so I am really hoping to be able to utilize the control. However, I am following a strict “no code behind” policy, which I would rather not violate.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
amkuchtacommented, May 24, 2018

Eureka! That was it! My updated code:

View

...
<!--Added an event trigger on the window (which requires an additional namespace for interactivity)-->
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
...
<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <i:InvokeCommandAction Command="{Binding LaunchStigNotificationCommand}" />
    </i:EventTrigger>
</i:Interaction.Triggers>
...
<!--Everything else stays the same with regards to the NotificationMessageContainer-->
...

ViewModel

...
// Moved the logic to create the NotificationMessage into its own function, which is launched via the command bound above
public RelayCommand LaunchStigNotificationCommand
{ get { return new RelayCommand(LaunchStigNotification); } }

private void LaunchStigNotification()
{ 
    try
    {
        NotificationMessageManager.CreateMessage()
            .Accent("#1751C3")
            .Animates(true)
            .AnimationInDuration(0.75)
            .AnimationOutDuration(0.75)
            .HasBadge("Info")
            .HasMessage("Please ingest the latest STIG Compilation Library on the settings page.")
            .Dismiss().WithButton("Dismiss", button => { })
            .Queue();
    }
    catch (Exception exception)
    {
        log.Error(string.Format("Unable to launch STIG library ingestion notification."));
        log.Debug("Exception details:", exception);
    }
}
...

Hopefully this approach helps you when you decide to refactor your application 😉 Thank you for the help!!!

0reactions
amkuchtacommented, May 30, 2018

@AleksandarDev GitHub treats the wiki just like any other source - you can clone it and submit updates / PRs via git, which would then have to be reviewed / approved before they are accepted.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Notification from ViewModel to View
I need to send notification from ViewModel to View in MVVM WPF application. In most cases it will be simple MessageBox on View...
Read more >
Store data in ViewModel
In this codelab, you learn how to use a ViewModel, one of the Architecture components. Implement a ViewModel to retain the app data...
Read more >
Updating the Notification - MVVM Running Tracker App - Part 16
I want to watch the course when you finish the course, but it takes too long. I can't wait. 13:19 · Go to...
Read more >
MVVM Running Tracker App - Part 12 - YouTube
In this video we will make our TrackingService a foreground service that comes with a notification and can't be killed by the Android...
Read more >
Patterns - WPF Apps With The Model-View-ViewModel ...
In this article, I will show you how a ViewModel can expose commands to a View, ... PropertyChanged event to notify the WPF...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found