【问题标题】:UWP button not firing eventUWP 按钮未触发事件
【发布时间】:2016-12-06 16:16:48
【问题描述】:

我正在处理一个 UWP 项目。我有两个按钮可以从一页转到另一页。但我不明白为什么永远不会调用按钮,我尝试了不同的教程和技术它不起作用。

<Page
    x:Class="MultiplatformMvvm.UWP.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MultiplatformMvvm.UWP"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Page.Resources>
        <ResourceDictionary>

            <Style x:Key="textBlockStyle" TargetType="TextBlock">
                <Setter Property="Margin" Value="12,20,12,12"/>
                <Setter Property="HorizontalAlignment" Value="Center"/>
                <Setter Property="FontSize" Value="24"/>
            </Style>

            <Style x:Key="buttonStyle" TargetType="Button">
                <Setter Property="Margin" Value="12"/>
                <Setter Property="HorizontalAlignment" Value="Center"/>
                <Setter Property="Background" Value="Purple"/>
                <Setter Property="Foreground"  Value="White"/>
            </Style>

        </ResourceDictionary>
    </Page.Resources>

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel>
            <TextBlock Text="Un café !" Style="{StaticResource textBlockStyle}"/>
            <Button x:Name="coffeeButton" Style="{StaticResource buttonStyle}" >
                Go Coffee
            </Button>
            <Button x:Name="myContactsButton" Style="{StaticResource buttonStyle}">
                My contacts
            </Button>
        </StackPanel>
    </Grid>
</Page>

我的 C# 代码:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        InitializeComponent();
        coffeeButton.Click += coffeeButton_Click;
    }

    void coffeeButton_Click(object sender, RoutedEventArgs e)
    {
        // I never enter here
        Frame.Navigate(typeof(CoffeeListPage));
    }

    private void myContactsButton_Click(object sender, RoutedEventArgs e)
    {
        // I never enter here
        Frame.Navigate(typeof(ContactListPage));
    }
}

【问题讨论】:

  • 您在呈现 UI 之前创建 Click 事件。这就是为什么没有创建点击事件的原因(至少这是我所理解的。)在 UI 本身中试试这个。 &lt;Button x:Name="coffeeButton" Style="{StaticResource buttonStyle}" Content="Go Coffee" Tapped="coffeeButton_Tapped" /&gt;
  • 我试过了,但是不行
  • 您是否收到错误消息?还是没有触发 Click/Tap 事件?
  • 没有错误,什么也没发生
  • 很高兴工作。我建议您将您在此问题上的经验作为答案并将其标记为已接受,以便遇到相同问题的其他人受益。

标签: c# xaml uwp


【解决方案1】:

您是否尝试过: - 将处理程序附加到 xaml 本身中的按钮并查看它是否有效? - 如果你使用绑定,那么添加一个命令绑定?

同样在你的场景中,试试这个: 在 InitializeComponent(); 之后的构造函数中;添加:

已加载 += (s, e) => { coffeeButton.Click += coffeeButton_Click; };

看看有没有用。

【讨论】:

  • 可以给我们 buttonStyle 吗?是否有可能在样式中覆盖某些行为垫?在 UWP 应用程序中,因为想法是您的应用程序将在移动设备上运行,所以最好处理 Tapped 事件,因为当用户点击时触发,当鼠标点击时触发点击
  • 我清理了代码并清理了它工作的构建项目,这是一个缓存问题... :( 感谢您的帮助!
【解决方案2】:

我终于清理了 UWP 项目中的所有缓存并重建,它终于可以工作了。 希望它可以帮助某人。

【讨论】:

  • 感谢您分享您的建议。它也解决了我同样的问题。