【发布时间】:2015-07-13 06:40:33
【问题描述】:
我正在从 stackLayout 组件生成一个 xaml 文件。我已经调用了这个 TimerButton。
我有两个 TimerButtons,想区分它们。
//In MainPage.xaml
<component:TimerButton x:Name="Smoke"></component:TimerButton>
<component:TimerButton x:Name="Snuff"></component:TimerButton>
我需要发送 ID,以便我可以在 TimerButton 组件内的 C# 代码 (ViewModel.TobaccoType) 中设置一个值。我试过使用 x: arguments/name/type 没有运气。
//In TimerButton.xaml.cs
ViewModel = new TimerButtonViewModel();
if (this.FindByName<TimerButton>("Smoke") != null)
{
ViewModel.TobaccoType = "Smoke";
}
【问题讨论】:
-
这就是我试图用 x:Name 做的事情。但无法在 Timerbutton.xaml.cs 文件中访问它。
-
好吧,我需要它在按钮初始化时运行代码。我想知道它是鼻烟按钮还是烟雾按钮,而不必将 SmokeTimerButton 和 SnuffTimerButton 分别作为组件。