I m working on a UWP project. I have two buttons to go from one page to another one. But I don't understand why the button is never called, I try different tutorials and technique it doesn't work.
<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>
My C# code :
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));
}
}
Have you tried: - Attaching a handler to the button in xaml itself and see it that works? - In case you are using Bindings, then adding a command binding?
Also in your scenario,try this: In your constructor after the InitializeComponent(); add:
Loaded += (s, e) => { coffeeButton.Click += coffeeButton_Click; };
see if that works.
I finally clean all the cache in the UWP project and rebuild and it finally works. Hope it helps someone.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.