How to navigate links by button in WPF Modern UI in C#?
Asked Answered
A

2

6

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?

Ataliah answered 1/4, 2014 at 8:32 Comment(2)
Look at the documentation to make sure that Uri is correct.Gorcock
i tried many ways but not able to get the solution. even i tried stackoverflow link - [#20031663Ataliah
G
9

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);
    }
}
Gorcock answered 2/4, 2014 at 7:19 Comment(3)
Thanks Lukas. You saved my life. And how can I activate the link as well, which is mui:Link.Ataliah
Sorry, but I don't know what do you mean: "..how can I activate link.."Gorcock
I am using Modern UI as i mentioned in my Question. and Modern UI uses link for navigation. So i am asking about those links. like below <mui:LinkGroup DisplayName="Tool to add or update games" > <mui:LinkGroup.Links> <mui:Link DisplayName="Home" Source="Pages/Home.xaml" /> <mui:Link DisplayName="Add Game" Source="Pages/AddGame.xaml" /> <mui:Link DisplayName="Update Game" /> <mui:Link DisplayName="Automation Files" /> </mui:LinkGroup.Links> </mui:LinkGroup> </mui:ModernWindow.MenuLinkGroups>Ataliah
P
0

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);
                }));
Polymorphism answered 25/2, 2015 at 21:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.