简体   繁体   中英

Unable to navigate to another page on button click in windows phone 8.1

I have a button & upon clicking it, I need to go to another xaml page.

This button resides in Page1.xaml

<Button Content="Button Name" HorizontalAlignment="Center" VerticalAlignment="Center" Click="Button_Click" Foreground="#FF119FF0"/>


    private void Button_Click(object sender, RoutedEventArgs e)
    {
       frameBody.Navigate(typeof(HomePage)); 
    }

But I am not able to navigate to HomePage.xaml.

What can be the reason??

These are the libraries I included:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

The function Button_Click() is defined in Page1.xaml.cs.

I think You are missing a directive or an assembly reference?

for window phone 8.1 navigation

 Frame.Navigate(typeof(Urpage));

for window phone 8.0

        NavigationService.Navigate(new Uri("/URpage.xaml", UriKind.Relative));

Check if you are using a windows phone sliverlight project and your class is extending/inheriting "PhoneApplicationPage" like this :

public partial class Example : PhoneApplicationPage
{
        private void Button_Click(object sender, RoutedEventArgs e)
        {    
            NavigationService.Navigate(new Uri("/Homepage.xaml", UriKind.RelativeOrAbsolute));   
        }  
}

Hope this helps

This is the code that I use in my project:

    private void ItemView_ItemClick(object sender, ItemClickEventArgs e)
    {
        var albumId = ((Album)e.ClickedItem).Id;
        if (!Frame.Navigate(typeof(AlbumPage), albumId))
        {
            var resourceLoader = ResourceLoader.GetForCurrentView("Resources");
            throw new Exception(resourceLoader.GetString("NavigationFailedExceptionMessage"));
        }
    }

The part to focus on is Frame.Navigate(typeof(AlbumPage)) .

I want to pass some information to that page, that's why I pass my albumId .

If you want to retrieve the passed argument in your other page, you might want to use this code:

    private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
    {
        var albumId = (int)e.NavigationParameter;
    }

Another most important point to notice is you might want to check 'App.xaml.cs' file and within OnLaunched method change the

   rootFrame.Navigate(typeof(UrPage),e.Arguments);

by default MainPage.xaml loads.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM