2014-10-29 67 views
0

我想在自定义渲染器中创建一个搜索视图,但它总是返回null我尝试了几种不同的方法,但迄今为止相同的结果。现在我已经解决了这个问题,因为它至少不会抛出任何其他错误。SearchView在CustomRenderer中空了

我的布局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <SearchView 
     android:minWidth="25px" 
     android:minHeight="25px" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/mapSearch" 
     android:layout_marginBottom="0.0dp" 
     class="Android.Widget.SearchView"/> 
    <fragment 
     android:id="@+id/mapWithOverlay" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:minWidth="25px" 
     android:minHeight="25px" 
     class="com.google.android.gms.maps.MapFragment" /> 
</LinearLayout> 

在OnElementChangedMethod

_activity = this.Context as Activity; 
_searchView = (SearchView) _activity.FindViewById(Resource.Id.mapSearch); 
SetupSearchView(); 

SetupSearchView方法

public void SetupSearchView() 
{ 
    if (_searchView != null) 
    { 
     _searchView.QueryTextSubmit += async (sender, e) => 
     { 
      Console.WriteLine(e.Query); 
     }; 
    } 
} 
+0

我想你提到上述的作品,那么你的方法不适合你尝试过吗?如果你打包一个小演示,我会看看。我的联系方式在我的个人资料上。 – Pete 2014-10-29 13:46:52

+0

以上,我不工作,我会尝试并获得演示起来,这可能是因为它也是我将如何绑定搜索执行但如果搜索查看为空,这是我在看到这一问题的maprenderer – BillPull 2014-10-29 14:50:52

回答

0

如果这是继从以前的问题,您PageRenderer方法,那么也许OnElementChanged前布局一直膨胀被解雇?,给_searchView一个,因为它不能在这一点上可以找到?

有可能是一个更好的处理程序来把这个初始化逻辑,但我还没有Xamarin.FormsPageRenderers目前做任何事情。

也许尝试寻找搜索查看控制,只是以前执行搜索,你应该已经从以前的搜索的参考?

因此,像: -

public void DoSearch() 
{ 
    if (_searchView==null) 
    { 
     _activity = this.Context as Activity; 
     _searchView = (SearchView) _activity.FindViewById(Resource.Id.mapSearch); 
     SetupSearchView(); 
    } 
    ... 
    ... 
} 

那么至少将确认是否有任何与在得早起引用OnElementChanged事件处理程序?,指出存在更好方法把这个?

+0

内时刻。是否有更多的全球搜索事件? – BillPull 2014-10-29 17:43:43

+0

我不知道你的项目组成。以上是对你来说,试图获得对我怀疑的_searchView的引用是空的,这就是为什么你有问题。如果你能够打包一些东西,我可以更准确地解释你遇到的问题的原因。如果没有,我只是猜测你可能会遇到什么样的错误。有时它更快更容易查看代码。 – Pete 2014-10-29 17:53:45