访问 ControlTemplate 中定义的 XAML 控件

Access a XAML control defined in a ControlTemplate

我正在使用 Xamarin 表单,在 XAML 中,我正在尝试更改 PlayButton (Button) 中定义的 Image 属性 =13=] 在 App.xaml 中。我似乎无法在后面的代码中访问 App.xaml.cs 文件中的 Button 控件。有谁知道如何访问按钮或知道如何更改图像 属性?

<Application.Resources>
 <ResourceDictionary>
   <ControlTemplate x:Key="PageTemplate">
      <StackLayout Orientation="Horizontal" Margin="10">
          <Button x:Name="PlayButton" />
      </Stacklayout>
   </ControlTemplate>
 <ResourceDictionary>
</Application.Resources>

一个模板就是一个模板。在模板实际应用于某个元素之前,无法访问 Button

... So now on the click event located in App.xaml.cs, I cannot access the button to further change the image. Where can access this button to achieve this and how?

您可以在事件处理程序中转换发送者参数:

private void Button_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    //...
}