Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7713021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:46:02+00:00 2026-06-01T01:46:02+00:00

I have the following Behavior: public class NavigateAndBroadcastAction : NavigateToPageAction { protected override void

  • 0

I have the following Behavior:

    public class NavigateAndBroadcastAction : NavigateToPageAction
        {
            protected override void Invoke(object parameter)
            {            
                base.Invoke(parameter);
                Messenger.Default.Send<NavigatingMessage<ViewModelBase>>(new NavigatingMessage<ViewModelBase>(this, PassedObject), NavigationToken);
            }

            public ViewModelBase PassedObject
            {
                get { return (ViewModelBase)GetValue(PassedObjectProperty); }
                set { SetValue(PassedObjectProperty, value); }
            }

            // Using a DependencyProperty as the backing store for PassedObject.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty PassedObjectProperty = DependencyProperty.Register("PassedObject", typeof(ViewModelBase), typeof(NavigateAndBroadcastAction), new PropertyMetadata(null)); 
...
    }

It basically uses the NavigateToPageAction (available in Blend also) but allows me to also broadcast a ViewModel object (I use it to navigate from List page to Detail page and to pass the selected object)

Xaml would look like this: (the PassedObject Binding is to an instance of DetailViewModel which inherits from ViewModelBase)

<i:Interaction.Triggers>
     <i:EventTrigger EventName="MouseLeftButtonDown">
         <b:NavigateAndBroadcastAction TargetPage="/View/SubjectDetailPage.xaml" NavigationToken="SubjectDetailNavigationToken" PassedObject="{Binding}" />
     </i:EventTrigger>
</i:Interaction.Triggers>

Now, I want to register for the Message:

Messenger.Default.Register<NavigatingMessage<DetailViewModel>>(this, NavigationToken, true, Action);

But that doesnt work. What does work, is to register for NavigatingMessage<ViewModelBase> and then cast the received message to NavigatingMessage<DetailViewModel>. Is there a way around that?

Can it be done so that the messenger detects the actual type of object being sent and correctly delivers to objects that registed for that type?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:46:03+00:00Added an answer on June 1, 2026 at 1:46 am

    One possible way would be to use reflection to send the message, by creating the message with the correct generic type at runtime.
    Another one would be to use dynamic and type inference:

    protected override void Invoke(object parameter)
    {            
        base.Invoke(parameter);
        dynamic viewModel = PassedObject;
        Messenger.Default.Send(GetMessage(this, viewModel), NavigationToken);
    }
    
    private NavigatingMessage<T> GetMessage<T>(NavigateToPageAction action, T item)
    {
        return new NavigatingMessage<T>(action, item);
    }
    

    The version using reflection is a bit more messy:

    protected override void Invoke(object parameter)
    {            
        base.Invoke(parameter);
        Send(PassedObject, NavigationToken);
    }
    
    void Send(ViewModelBase objectToSend, string navigationToken)
    {
        var genericMessageType = typeof(NavigatingMessage<>)
        var viewModelType = objectToSend.GetType();
        var messageType = genericMessageType.MakeGenericType(viewModelType);
        var message = Activator.CreateInstance(messageType, this, objectToSend);
    
        var method = typeof(Messenger).GetMethods()
                                      .Single(x => x.Name == "Send" &&
                                                   x.GetParameters().Count() == 2 &&
                                                   x.GetParameters()
                                                    .First()
                                                    .ParameterType
                                                    .GetGenericTypeDefinition()
                                                     == genericMessageType);
        method.MakeGenericMethod(viewModelType)
              .Invoke(Messenger.Default, new [] { message, navigationToken });
    }
    

    This code assumes that NavigationToken is a string. If not, just change the type of the second parameter of the Send method. If Messenger only contains one overload of the Send method you could simplify the condition in Single. On the other hand, if there are a lot of overloads of that method, you might need to refine it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following configuration: @Aspect public class MyAspect { @Around(@annotation(SomeAnnotation)) public Object myMethod(ProceedingJoinPoint
I have the following utility: class Worker { public void DoWorkAsync(Action callback) { Action
Suppose i have the following class in C#: public class B : A {
I have the following: using System; using System.DirectoryServices.AccountManagement; public class ChangePassword { public static
I have the following definitions. interface IWeapon {} class Warrior { [Inject] public IEnumerable<IWeapon>
consider the following test class: [TestClass] public class ExampleTests { [TestMethod] public void FileDoesNotExists()
I have the following classes: class State { protected: Vec3D accel; Vec3D gyro; Vec3D
I have the following code public class Test { Lock lock = new ReentrantLock();
I have the following situation where a client class executes different behavior based on
Let's have the following class hierarchy: public class MyType { } public class MySuperclass<T

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.