【发布时间】: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 本身中试试这个。
<Button x:Name="coffeeButton" Style="{StaticResource buttonStyle}" Content="Go Coffee" Tapped="coffeeButton_Tapped" /> -
我试过了,但是不行
-
您是否收到错误消息?还是没有触发 Click/Tap 事件?
-
没有错误,什么也没发生
-
很高兴工作。我建议您将您在此问题上的经验作为答案并将其标记为已接受,以便遇到相同问题的其他人受益。