Question

I am using ModernUI. I have one issue with Button and link.

I am trying to navigate by Button Click event and my code in "Home.xaml" is as follow

private void addGameButton_Click(object sender, RoutedEventArgs e)
{
    BBCodeBlock bs = new BBCodeBlock();
    try
    {
        bs.LinkNavigator.Navigate(new Uri("pack://application:/Pages/AddGame.xaml"), null);
    }
    catch (Exception error)
    {
        ModernDialog.ShowMessage(error.Message, FirstFloor.ModernUI.Resources.NavigationFailed, MessageBoxButton.OK);
    }
}

mui:Link works fine in MainWindows.xaml for navigation. but I want to navigate to AddGame.xaml from Home.xaml Page by a Button, which is in Home.xaml page.

My file structure is as below, for reference.

Folder Structure

So please let me know, where am i doing wrong?

Was it helpful?

Solution

The second parameter of bs.LinkNavigator.Navigate method is source that cannot be null. Try this:

private void addGameButton_Click(object sender, RoutedEventArgs e)
{
    BBCodeBlock bs = new BBCodeBlock();
    try
    {
        bs.LinkNavigator.Navigate(new Uri("/Pages/AddGame.xaml", UriKind.Relative), this);
    }
    catch (Exception error)
    {
        ModernDialog.ShowMessage(error.Message, FirstFloor.ModernUI.Resources.NavigationFailed, MessageBoxButton.OK);
    }
}

OTHER TIPS

Interestingly, in my environment, the following code works:

if (App.HasDashboardRole)
            {
                App.Current.Dispatcher.Invoke(new Action(() =>
                {
                    var bs = new BBCodeBlock();
                    bs.LinkNavigator.Navigate(new Uri("/Pages/Dashboard.xaml", UriKind.Relative), this);
                }));
            }
            else if (App.HasBarcodeBuilderRole)
            {
                App.Current.Dispatcher.Invoke(new Action(() =>
                {
                    var bs = new BBCodeBlock();
                    bs.LinkNavigator.Navigate(new Uri("/Pages/BarcodeBuilderPage.xaml", UriKind.Relative), this);
                }));
            }

When this code does not:

App.Current.Dispatcher.Invoke(new Action(() =>
                {
                    var bs = new BBCodeBlock();
                    if (App.HasDashboardRole)
                        bs.LinkNavigator.Navigate(new Uri("/Pages/Dashboard.xaml", UriKind.Relative), this);
                    else if (App.HasBarcodeBuilderRole)
                        bs.LinkNavigator.Navigate(new Uri("/Pages/BarcodeBuilderPage.xaml", UriKind.Relative), this);
                }));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top