2014-02-18 71 views
1

我正在使用Xamarin並且有一個關於從Layout獲取View的問題。如何從佈局獲取視圖?

如果我有一個叫的Layout,我怎麼能從這裏得到View

我曾嘗試這樣的代碼:

View v = Inflate(Resource.Layout.custom_info_window, null);

這是我收到的錯誤:

錯誤CS0103: '充氣' 的名稱在當前環境中不存在

我可以請這個幫忙嗎?

在此先感謝

編輯

這裏是我的充氣代碼:

LayoutInflater inflater =(LayoutInflater)Context.GetSystemService(Context.LayoutInflaterService); 

這是錯誤我得到:

Error CS0120: An object reference is required for the non-static field, method, or property 'Android.Content.Context.GetSystemService(string)' 
+0

您需要使用佈局充氣器..實際上充氣是佈局充氣器類的子功能。爲更多參考http://developer.android.com/reference/android/view/LayoutInflater.html – WISHY

回答

1

你應該使用這一個..

LayoutInflater inflater = (LayoutInflater)this.GetSystemService(Context.LayoutInflaterService); 
var layout = inflater.Inflate(Resource.Layout.custom_info_window, layoutImages) as LinearLayout; 
+0

你能看看我的編輯嗎? – Garry

+0

你有沒有做任何自定義視圖? – Piyush

+0

是的,我做了一個自定義的視圖。 – Garry

1
LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View v = mInflater.inflate((Resource.Layout.custom_info_window, null); 
+0

你能看看我的編輯嗎? – Garry

1

應該還可以得到從上下文LayoutInflater,是這樣的:

LayoutInflater.from(getApplicationContext()).inflate(Resource.Layout.custom_info_window, null); 
+1

這似乎是最簡單的方法。 –

0

您將需要使用一個Context對象來獲得服務。如果沒有別的工作,使用靜態屬性Application.Context:

var inflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater; 
var view = inflater.Inflate(...