像Button
,Entry
,Label
,Picker
等的視圖可以具有x:Name
屬性。如何從對象中獲取Xamarin.Forms元素的名稱?
<Label x:Name="myLabelName" Text="Some text" />
x
被定義爲xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
。
現在我想處理未分類的項目,因此我使用object
作爲類型。
如何從object
獲得x:Name
屬性作爲字符串的代碼?在這個階段我不知道這種類型。
編輯:
爲了讓事情更清楚,我想發佈一些代碼。我有一個正常的XAML頁面這樣的:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XamlSamples.HelloXamlPage">
<Label x:Name="myLabelName" Text="Some text" />
</ContentPage>
在代碼隱藏文件,我可以,如果我做nameof(this.myLabelName)
得到了x:Name
屬性。但是,如果只有object
,這是如何完成的?
public partial class HelloXamlPage
{
public HelloXamlPage()
{
InitializeComponent();
List<string> itemsWhichPassTheCheck = this.Check(new List<object>() { this.myLabelName });
}
}
private List<string> Check(List<object> itemList)
{
// do some check here ...
// here I have only object and I want to get the x:Name attribute thereof
}
當你想得到元素的屬性名稱。我的意思是當事件或何時加載? –
不知道你的意思。用戶按下一個按鈕,然後我想進行一些檢查。在這裏我需要訪問name屬性。 – testing