【问题标题】:How to Raise Events in Application level XAML styles如何在应用程序级 XAML 样式中引发事件
【发布时间】:2014-05-23 05:33:16
【问题描述】:

我正在为 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)
{

}

那么,将这些事件与代码绑定的替代方法存在于代码隐藏文件中。我有一些编写 eventsetter 的方法,但无法使其适合这种情况。

【问题讨论】:

  • 您可以将ICommand 绑定到Click 事件。 MVVM Light 有一个帮手。然后,您可以将命令存储在 ViewModel 中。

标签: .net windows xaml windows-phone windows-phone-8.1


【解决方案1】:

您可能必须在 ApplicationStyle.xaml.cs 中定义 click 方法 (OnAddToChart) 才能编译您的项目...这是您单击该按钮时调用的方法,而不是 AlbumView 页面上的方法.

我遇到了同样的问题,因为我在 App.xaml 中定义了一个项目模板,并试图在另一个页面上获取按钮 Click 事件。

我也想知道如何处理这种情况...如何从全局定义的模板中获取 Click 事件?

我的解决方案是在本地为我需要该事件的每个列表定义模板...大量重复的项目模板代码...

在你的情况下,它会是这样的

<GridView ItemsPanel="{StaticResource albumItemPanelTemplate}" 
                  SelectionMode="Single" x:Name="albumListView"
                  ItemsSource="{Binding}" SelectionChanged="OnSelectedAlbum" >
<GridView.ItemTemplate>
<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>
</GridView.ItemTemplate>

        </GridView>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    相关资源
    最近更新 更多