【问题标题】:Can't go back to the same page twice. c# Windows Phone 8.1不能两次返回同一页面。 c# Windows Phone 8.1
【发布时间】:2015-02-11 09:01:33
【问题描述】:

我的 WP8.1 应用程序出现问题。我有一个 listView,当我点击项目时,它会让我进入一个包含详细信息的新页面,但是当我回到我的 listView 时,我可以重新点击相同的项目,但它不会让我到任何地方。

这是我在 MainPage.xaml.cs 中的 listView SelectionChanged 处理程序。

private void lvNom_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Person personneSelect = (Person)lvNom.SelectedItem;
        Frame.Navigate(typeof(DetailPersonne), personneSelect);      
    }

这是我需要在 DetailPersonne.xaml.cs 中显示的页面背后的代码

    public sealed partial class DetailPersonne : Page
{
    public Person person;
    public DetailPersonne()
    {
        this.InitializeComponent();

    }
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        person = e.Parameter as Person;

        if(string.IsNullOrEmpty(person.Nom) == true){
            tbNom.Text = "Non renseigné.";
        }
        else{
             tbNom.Text = person.Nom;
        }
        if (string.IsNullOrEmpty(person.Prenom) == true)
        {
            tbPrenom.Text = "Non renseigné.";
        }
        else{
        tbPrenom.Text = person.Prenom;
        }

我所做的唯一导航更改是在我的 App.xaml.cs 中处理硬件后退按钮

  public App()
    {
        this.InitializeComponent();
        this.Suspending += this.OnSuspending;
        HardwareButtons.BackPressed += HardwareButtons_BackPressed;
    }
    void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        if(rootFrame != null && rootFrame.CanGoBack)
        {
            e.Handled = true;
            rootFrame.GoBack();
        }
    }

因此,如果有人对此进行了修复,那就太好了!提前致谢 ! :)

【问题讨论】:

    标签: c# xaml listview windows-phone-8.1


    【解决方案1】:

    尝试将所选项目重置为 SelectionChanged 未触发,因为当您导航返回时选择未更改。

        private void lvNom_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(lvNom.SelectedItem != null)
            {
                Person personneSelect = (Person)lvNom.SelectedItem;
                lvNom.SelectedItem = null;
                Frame.Navigate(typeof(DetailPersonne), personneSelect);
            }      
        }
    

    【讨论】:

    • 可能还需要检查事件内部的 null,因此您将 null 放入 SelectedItem,它会触发 SelectionChanged 并且可能抛出空引用异常。
    • @Romasz 谢谢。更新了我的答案
    • 感谢您的回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多