none
关于搜索问题【C#】 RRS feed

  • 问题

  • 软件打开后搜索很正常,软件没运行,点击搜索能调出软件的搜索页面,但是输入内容后搜索,程序就崩溃了,怎么回事啊?

    而且我新建模版的搜索页面,没有这个问题,难道是我写的搜索页面有问题?

    下面是我的app.xaml.cs 的代码

     /// <summary>
            /// Invoked when the application is activated to display search results.
            /// </summary>
            /// <param name="args">Details about the activation request.</param>
            protected  override  void OnSearchActivated(Windows.ApplicationModel.Activation.SearchActivatedEventArgs args)
            {
                if (args.PreviousExecutionState == ApplicationExecutionState.NotRunning ||
                    args.PreviousExecutionState == ApplicationExecutionState.ClosedByUser ||
                    args.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    SearchPane.GetForCurrentView().SuggestionsRequested += OnSuggestionsRequested;
                }
                var previousContent = Window.Current.Content;
                var frame = previousContent as Frame;
                if (frame == null)
                {               
                    frame = new Frame();               
                }

                Window.Current.Content = frame;               
                Window.Current.Activate();
                frame.Navigate(typeof(Views.SearchView), args.QueryText);
            }

    下面是我搜索页面的LoadState代码

     /// <summary>
            /// 使用在导航过程中传递的内容填充页。在从以前的会话
            /// 重新创建页时,也会提供任何已保存状态。
            /// </summary>
            /// <param name="navigationParameter">最初请求此页时传递给
            /// <see cref="Frame.Navigate(Type, Object)"/> 的参数值。
            /// </param>
            /// <param name="pageState">此页在以前会话期间保留的状态
            /// 字典。首次访问页面时为 null。</param>
            protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
            {
                if (pageState == null)
                {
                    if (SearchVM == null)
                    {
                        SearchVM = new SearchViewModel();
                    }            
                    if (navigationParameter != null)
                    {
                        string strCommmet = navigationParameter as string;
                        if (!string.IsNullOrEmpty(strCommmet))
                        {
                            this.updateTitle(strCommmet);
                            SearchVM.strSearchComment = strCommmet;                       
                            SearchVM.Search();
                        }
                    }
                }
                else
                {
                    SearchVM = pageState["VMDATA"] as SearchViewModel;
                   
                }

                this.DataContext = SearchVM;
            }

    麻烦大家给看看,哪里出问题了?


    Welcome to my sina Weibo:http://t.sina.com.cn/1791733541

    2012年12月10日 5:26

答案

  • 果然是个小问题:通过设置不启动调试代码,如上图设置,找到了解决方法:

    在未启动程序时候调用系统搜索菜单是不产生 protected override void OnLaunched(LaunchActivatedEventArgs args)事件的,

    而直接产生 protected override void OnSearchActivated(Windows.ApplicationModel.Activation.SearchActivatedEventArgs args)事件

    而我的程序在Onlaunched定义了一个全局变量,在搜索页面中调用这个变量就崩溃了 所以定义全局变量的时候要注意啦


    Welcome to my sina Weibo:http://t.sina.com.cn/1791733541

    2012年12月12日 3:28