如何在 C# 中的 WPF 现代 UI 中按按钮导航链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22780600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to navigate links by button in WPF Modern UI in C#?
提问by VarunJi
I am using ModernUI. I have one issue with Button and link.
我正在使用ModernUI。我有一个按钮和链接问题。
I am trying to navigate by Button Click event and my code in "Home.xaml" is as follow
我试图通过按钮点击事件导航,我在“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: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.
mui:Link 在 MainWindows.xaml 中运行良好以进行导航。但我想通过 Home.xaml 页面中的按钮从 Home.xaml 页面导航到 AddGame.xaml。
My file structure is as below, for reference.
我的文件结构如下,供参考。


So please let me know, where am i doing wrong?
所以请让我知道,我哪里做错了?
回答by Lukas Kubis
The second parameter of bs.LinkNavigator.Navigate method is sourcethat cannot be null. Try this:
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);
}
}
回答by J-DawG
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);
}));

