OnNavigatedTo send to other page

Jun 6, 2013 at 11:53 AM
Hi. Got the following requirement. When entering a page need to know if a certain context exist. If it doesn't redirect the client to a certain page. Unfortunately can do the redirect part, so:

In order to do this I've got my shell shell with a link like <mui:Link Source="/View/SomeIndex" (...)
and that page implements the IContent. In the OnNavigatedTo got the following code:
use FirstFloor.ModernUI.Windows.Navigation;
(...)
public void OnNavigatedTo(NavigationEventArgs e)
{
  if(EvaluenteIfRequiresContext())
    NavigationCommands.GoToPage.Execute("/View/ContextPage.xaml", null);
}
The NavigationCommand is executed but doesn't have any effect.

Does anybody have any suggestion?

Thank you
Jun 6, 2013 at 1:16 PM
Edited Jun 6, 2013 at 1:16 PM
Ok. Once again I'll answer myself! :)

I have done a workarround. Instead of invoking the navigation command I'm now finding the frame/window and set the contentsource to the url that I want. So:
    ModernWindow window = this.TryFindParent<ModernWindow>();
    window.ContentSource = new Uri("/View/ContextPage.xaml", UriKind.Relative);
Still, if anyone can figure it why the navigation command doesn't work, that will be great.

Regards,
Sérgio Amorim
Coordinator
Jun 7, 2013 at 5:23 PM
My guess this doesn't work is that you try to change the content frame source while the frame source is being changed. You could move the NavigationCommand.GoToPage.Execute invocation to a Dispatcher.Invoke(() => {..}) call, so the invocation is deferred after the OnNavigated has completed.
Jun 11, 2013 at 10:57 AM
Excellent. It worked like a charm, thank you once again.

Just for future reference, in my example the invoke should be:
ModernWindow window = this.TryFindParent<ModernWindow>();
Dispatcher.Invoke((Action)delegate() 
{
    NavigationCommands.GoToPage.Execute("/View/DebitosDirectos/EntidadesCredoras/AlterarContexto.xaml", window);
});
Aug 20, 2013 at 10:41 AM
Hi

I am also having a problem with this .

This is my code
public partial class Home : UserControl
{
    public string DeleteFilesExit;
     //setting
    private MatchCollection ObjNameVerMc;
    public string DbObjVer;
    // globats end 
    private System.IO.FileSystemWatcher m_Watcher;

    public Home()
    {
        InitializeComponent();

        ExtractPath = ConfigurationManager.AppSettings.Get("ExtractPath");
        AppUpdated = ConfigurationManager.AppSettings.Get("AppUpdated");


        GetCurrentEnv();
        WatchHostFile(@"C:\Windows\System32\drivers\etc\hosts");
        FillComboBox();

        if (!IsProcessOpen("BlueTray"))
        { ModernDialog.ShowMessage("BlueTray is not running please start it.", "BlueTray not found", MessageBoxButton.OK); }



        if (AppUpdated.Equals("true", StringComparison.Ordinal))
        {
            MessageBoxResult AppUpdatedMSG = ModernDialog.ShowMessage("CoreExtract has been updated please update you settings", "CoreExtract Updated", MessageBoxButton.OK);

            if (AppUpdatedMSG == MessageBoxResult.OK)
            {
                ModernWindow window = this.TryFindParent<ModernWindow>();
                Dispatcher.Invoke((Action)delegate()
                {
                    NavigationCommands.GoToPage.Execute("/Pages/ObjectVersions.xaml", window);
                });

            }
        }

    }}

I have the using FirstFloor.ModernUI.Windows.Navigation; statment at the top o my code but I still get this error Error 2 'CoreExtract2.Pages.Home' does not contain a definition for 'TryFindParent' and no extension method 'TryFindParent' accepting a first argument of type 'CoreExtract2.Pages.Home' could be found (are you missing a using directive or an assembly reference?) C:\Users\justinfo\Documents\Visual Studio 2012\Projects\CoreExtract2\CoreExtract2\Pages\Home.xaml.cs 63 48 CoreExtract2 any ideas what is wrong ?
Sep 3, 2013 at 11:50 AM
The TryFindParent extension is here http://www.hardcodet.net/2008/02/find-wpf-parent
Credits to Phillip Sumi
Sep 8, 2013 at 6:35 PM
hi,

im trying to do this from an mvvm end, however at that point the ViewModel is not an UI element. Any idea how this could work with Viewmodels?

Best Regards.
Sep 8, 2013 at 7:25 PM
Answeres my own question.

Easy solution by using Application.Current.Mainwi dow is a direct reference to the ModernWindow.
Sep 9, 2013 at 8:54 AM
If you're using MVVM pattern then what you're doing is not the right way. The ViewModel shoudn't know what the view is made of. If you want to initiate some action from the viewmodel that "interacts" with the view then expose some actions from the viewmodel in which the view can assign something to it:
public class MyViewModel 
{
    //The exposed action
    public Action<string> DefineWindowTitle;
    public void CommandImplementation() 
    {
        If(DefineWindowTitle != null)
            DefineWindowTitle("Hello World");
        else
            throw new InvalidOperationException("Do Something is not initialized");
    }
    (...)
}

public class MyView : Control
{
    private MyViewModel viewModel;
    (...)
    private void Initialized()
    {
        this.viewModel = new MyViewModel() {
            DefineWindowTitle = SetWindowTitle
        };
    }
    
    
    private void SetWindowTitle(string windowTitle)
    {
        ModernWindow window = this.TryFindParent<ModernWindow>();
        window.Title = windowTitle;
    }
}
Apr 1, 2014 at 3:45 AM
justinfourie

you have to change the target framework to .Net Framework 4.5
Aug 15, 2014 at 6:29 AM
Hey ,t like this
 var uri = new Uri("/Client.xxxxxxxxxx;component/Views/xxxxxxxxxxx.xaml", UriKind.Relative);
            NavigationCommands.GoToPage.Execute(uri, null);