如何在C#中的WPF Modern UI中按按钮导航链接?

时间:2014-04-01 08:32:16

标签: c# wpf xaml modern-ui

我正在使用ModernUI。我有一个与Button和link有关的问题。

我正在尝试通过按钮点击事件和我的代码导航到" Home.xaml"如下

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:链接在MainWindows.xaml中正常工作以进行导航。但是我想通过一个Button(Home.xaml页面)从Home.xaml Page导航到AddGame.xaml。

我的文件结构如下,供参考。

Folder Structure

所以请告诉我,我在哪里做错了?

2 个答案:

答案 0 :(得分:9)

bs.LinkNavigator.Navigate方法的第二个参数是 source ,它不能为null。试试这个:

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);
    }
}

答案 1 :(得分:0)

有趣的是,在我的环境中,以下代码有效:

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);
                }));
            }

当此代码没有:

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);
                }));