C# SearchPane.QuerySubmitted-与单个xaml类相比,路由到App.xaml.cs的正确方式是什么
所以,我发现用户对使用魔咒有点困惑,他们更希望有一个应用程序内的按钮,他们可以按下/点击进行搜索。我更喜欢使用SearchPane方法,因为它具有处理搜索历史提示的所有内置过程 因此,在App.xaml.cs中,我有以下内容:C# SearchPane.QuerySubmitted-与单个xaml类相比,路由到App.xaml.cs的正确方式是什么,c#,xaml,windows-runtime,C#,Xaml,Windows Runtime,所以,我发现用户对使用魔咒有点困惑,他们更希望有一个应用程序内的按钮,他们可以按下/点击进行搜索。我更喜欢使用SearchPane方法,因为它具有处理搜索历史提示的所有内置过程 因此,在App.xaml.cs中,我有以下内容: protected override void OnWindowCreated(WindowCreatedEventArgs args) { // Register QuerySubmitted handler for the window
protected override void OnWindowCreated(WindowCreatedEventArgs args)
{
// Register QuerySubmitted handler for the window at window creation time and only registered once
// so that the app can receive user queries at any time.
SearchPane searchPane = SearchPane.GetForCurrentView();
searchPane.QuerySubmitted += new TypedEventHandler<SearchPane, SearchPaneQuerySubmittedEventArgs>(OnQuerySubmitted);
searchPane.SearchHistoryEnabled = true;
}
在WindowCreated上受保护的覆盖无效(WindowCreatedEventArgs args)
{
//在窗口创建时为窗口注册QuerySubmitted处理程序,并且只注册一次
//这样应用程序可以随时接收用户查询。
SearchPane SearchPane=SearchPane.GetForCurrentView();
searchPane.QuerySubmitted+=新类型DevenHandler(OnQuerySubmitted);
searchPane.SearchHistoryEnabled=true;
}
因此,这将设置一个事件,以便在执行常规搜索时,调用App.xaml.cs中OnQuerySubmitted的事件处理程序来处理搜索符是否用于该特定上下文。大致上,我查找一个要打开的文件,如果没有打开任何文件,则不允许搜索,并给出一个消息对话框来指示这一点
但是,在其他xaml页面中,我希望选择搜索符或xaml定义的按钮,以手动打开搜索窗格(我知道有些代码是多余的):
private void GoSearch(对象发送方,路由目标)
{
//启用搜索窗格和
SearchPane SearchPane=SearchPane.GetForCurrentView();
searchPane.SearchHistoryEnabled=true;
//注册QuerySubmitted事件处理程序
searchPane.QuerySubmitted+=新类型DevenHandler(OnQuerySubmitted1);
//显示搜索窗格
searchPane.Show();
}
但我发现,毫不奇怪,当遍历每个页面时,我会创建越来越多的事件处理程序。因此,一次搜索最终会得到大量被调用的处理程序。我不喜欢这样,但是当页面失去上下文时,如果不去掉处理程序,我不知道还能做什么
如果我能让App.xaml.cs在当前页面的xaml代码中调用一个函数,我会感觉更好。但我不确定这是否可行,除非我声明了一组静态成员函数。这似乎也不太理想
这里有关于正确方法的建议吗?您可以使用订阅消息类型将消息发送到UI的任何部分,并在发布该类型的消息时通知它。这将要求您使用Microsoft提供的Prism库,但该库还可以为您的应用程序提供许多其他好处。我选择了一个更简单的路径
private void OnQuerySubmitted(object sender, SearchPaneQuerySubmittedEventArgs args)
{
// Get frame information about the currently visible window.
Frame frame = Window.Current.Content as Frame;
// If the current frame is not valid, do not continue
if (frame == null)
{
return;
}
// Based on who the base frame is, call the process query handler for the proper frame....
if ((typeof(A.MainPage) == frame.CurrentSourcePageType)
{
// do something
}
else if ((typeof(A.MainPage2) == frame.CurrentSourcePageType)
{
// do something
}
// and so on
这不是最好的方法,每次我添加一个框架都会导致我实现,但世界上还有更糟糕的事情
private void OnQuerySubmitted(object sender, SearchPaneQuerySubmittedEventArgs args)
{
// Get frame information about the currently visible window.
Frame frame = Window.Current.Content as Frame;
// If the current frame is not valid, do not continue
if (frame == null)
{
return;
}
// Based on who the base frame is, call the process query handler for the proper frame....
if ((typeof(A.MainPage) == frame.CurrentSourcePageType)
{
// do something
}
else if ((typeof(A.MainPage2) == frame.CurrentSourcePageType)
{
// do something
}
// and so on