我正在尝试从MessageDialog上的UICommand内部进行Page导航,但是我不能让它工作。
在这里,我有一个UICommand,如果用户拒绝接受我的应用程序的协议条款,它将调用Page导航。
var decline = new UICommand("No, I Decline", async command =>
{
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
(Window.Current.Content as Frame).Navigate(typeof(LoginPage));
});
});
commands.Add(decline);但无论是否在UI线程上执行,Page都不会导航。
下面是一个简单的ShowDialogue方法:
public static async void ShowDialogue(string message, string title, IEnumerable<UICommand> commands)
{
var Dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
var dialog = new MessageDialog(message, title);
foreach (var command in commands)
{
dialog.Commands.Add(command);
}
dialog.DefaultCommandIndex = 0;
dialog.CancelCommandIndex = 1;
await dialog.ShowAsync();
});
}编辑:
我的问题是:
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
if (allowToLeavePage == false)
{
e.Cancel = true;
return;
}
displayRequest.RequestRelease();
}在我的代码中,我没有意识到我阻塞了页面导航。
发布于 2018-01-03 11:41:03
不能在我这边重现你的问题。我在一个按钮单击事件句柄中调用了ShowDialogue方法。可以很好地调用上述ShowDialogue方法的完整代码片段如下:
private void optest_Click(object sender, RoutedEventArgs e)
{
string message = "message test";
string title = "title test";
List<UICommand> commands = new List<UICommand>();
var decline = new UICommand("No, I Decline", async command =>
{
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
(Window.Current.Content as Frame).Navigate(typeof(LoginPage));
});
});
commands.Add(decline);
ShowDialogue(message, title, commands);
}我的UWP应用目标版本是build 16299。如果您仍然有问题,请提供一个完整的最小复制项目。更多详情请参考MessageDialog。
还有一件事,你应该只在升级使用MessageDialog的Universal Windows8应用程序时才使用MessageDialog,并且需要最大限度地减少更改。对于Windows10中的新应用程序,我们建议使用ContentDialog控件。请参阅MessageDialog类的“重要”部分。
https://stackoverflow.com/questions/48063581
复制相似问题