2014-10-29 102 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