/ / Xaml.am फ़ाइल में xaml.cs फ़ाइल के अंदर बाहरी स्टैकलेयूट घटक आईडी का संदर्भ - c #, xaml, xamarin

एक्सएएमएल के अंदर एक्सटर्नल एक्सटर्नल घटक आईडी का संदर्भ

मैं एक स्टैमलैटआउट घटक से एक xaml फ़ाइल इनहेरिट बना रहा हूं। मैंने इसे टिमरबटन कहा है।

मेरे पास दो टाइमरबटन हैं और उनके बीच अंतर करना चाहते हैं।

//In MainPage.xaml
<component:TimerButton x:Name="Smoke"></component:TimerButton>
<component:TimerButton x:Name="Snuff"></component:TimerButton>

मुझे आईडी भेजने की आवश्यकता है ताकि मैं टिमरबॉटन घटक के अंदर C # कोड (ViewModel.TobaccoType) में मान सेट कर सकूं। मैंने x का उपयोग करने की कोशिश की है: कोई भाग्य के साथ तर्क / नाम / प्रकार।

//In TimerButton.xaml.cs
ViewModel = new TimerButtonViewModel();

if (this.FindByName<TimerButton>("Smoke") != null)
{
ViewModel.TobaccoType = "Smoke";
}

उत्तर:

उत्तर № 1 के लिए 1

आप निम्नलिखित कर सकते हैं। टिमर्बटन को तथाकथित रूप दें DependencyProperty इस तरह:

public static readonly DependencyProperty TobaccoTypeProperty =
DependencyProperty.Register(
"TobaccoType", typeof(String),
typeof(TimerButton), null);

public String TobaccoType
{
get { return (String)GetValue(TobaccoTypeProperty); }
set { SetValue(TobaccoTypeProperty, value); }
}

और फिर आप इसे अपने एक्सएएमएल में इस तरह देखें:

//In MainPage.xaml
<component:TimerButton x:Name="Smoke" TobaccoType="Smoke"></component:TimerButton>
<component:TimerButton x:Name="Snuff" TobaccoType="Snuff"></component:TimerButton>

यह गुण आप आसानी से अपने TimerButton.cs में संदर्भित कर सकते हैं