我正在为Windows和Windows Phone创建通用应用程序 . 因此,我为这两种类型的项目声明了不同的Styles.xaml文件 .
但是样式中还包含一些点击事件,它们没有被触发我怎样才能发布这些点击事件 .
AppliationStyle.xaml 在这里
<DataTemplate x:Key="albumDataTemplate">
<Border CornerRadius="30" Background="LightGreen" Padding="10" Grid.Column="1" BorderBrush="White" Height="460" BorderThickness="2" Width="250">
<StackPanel HorizontalAlignment="Left" Orientation="Vertical" >
<Image Source="{Binding Url}" Margin="5" Height="220" Width="200"/>
<TextBlock Text="{Binding Title}" FontSize="25" Foreground="Purple" />
<TextBlock VerticalAlignment="Top" FontSize="20"
Foreground="White" >
<Run Text="Artist :" />
<Run Text="{Binding ArtistName}" Foreground="Red" />
</TextBlock>
<TextBlock VerticalAlignment="Top" FontSize="20"
Foreground="Purple" >
<Run Text="Price : $" />
<Run Text="{Binding Price}" Foreground="Red" />
</TextBlock>
<Image Height="30" HorizontalAlignment="Left" Source="{Binding Rating}" VerticalAlignment="Top"/>
<Button BorderBrush="Transparent" ToolTipService.ToolTip="Add Album To Cart" Tag="{Binding Id}" Click="OnAddToCart" VerticalAlignment="Bottom" Margin="0 20 0 0" HorizontalAlignment="Right" Height="60" Width="60" Padding="0">
<Image Source="Images/cart/shoppingcart_add.png" />
</Button>
</StackPanel>
</Border>
</DataTemplate>
我在用户控件中使用以上样式,这对Win 8.1和Win phone 8.1项目都是通用的 . AlbumView.xaml
<Grid>
<GridView ItemsPanel="{StaticResource albumItemPanelTemplate}"
ItemTemplate="{StaticResource albumDataTemplate}"
SelectionMode="Single" x:Name="albumListView"
ItemsSource="{Binding}" SelectionChanged="OnSelectedAlbum" >
</GridView>
</Grid>
我在 AlbumView.xaml.cs 中定义了那些CLick处理程序,就像这样
private async void OnAddToCart(object sender, RoutedEventArgs e)
{
}
那么在代码隐藏文件中存在将代码与代码绑定的替代方法是什么 . 我有一些编写事件设置器的方法,但不能使它适合这种情况 .
1 回答
您可能必须在ApplicationStyle.xaml.cs中定义click方法(OnAddToChart)以便您的项目进行编译...这是当您单击该按钮而不是AlbumView页面上的那个按钮时调用的方法 .
我遇到了与App.xaml中定义的项目模板相同的问题,并试图在另一个页面上获取按钮Click事件 .
我也想知道如何处理这种情况......如何从全局定义的模板中获取Click事件?
我的解决方案是为我需要事件的每个列表本地定义模板...大量重复的项目模板代码......
在你的情况下,它会是这样的