如何从弹出页面移动到Windows Phone应用程序中的其他页面

时间:2012-12-13 05:47:19

标签: windows-phone-7

我正在开发一个小游戏类型的应用程序,当用户赢得游戏时,他将获得弹出窗口作为胜利,我编写了以下代码。

  public void stoptimer()
 {

        if ((Convert.ToString(b1.Content) == "1") && (Convert.ToString(b2.Content) == "2") && (Convert.ToString(b3.Content) == "3") && (Convert.ToString(b4.Content) == "4") && (Convert.ToString(b5.Content) == "5") && (Convert.ToString(b6.Content) == "6") && (Convert.ToString(b7.Content) == "7") && (Convert.ToString(b8.Content) == "8") && (Convert.ToString(b9.Content) == "9") && (Convert.ToString(b10.Content) == "10") && (Convert.ToString(b11.Content) == "11") && (Convert.ToString(b12.Content) == "12") && (Convert.ToString(b13.Content) == "13") && (Convert.ToString(b14.Content) == "14") )

        {
            newTimer.Stop();
            time = txtClock.Text;
            //textBox2.Text = txtClock.Text;
            Popup buyNowScreen;
            buyNowScreen = new Popup();
            buyNowScreen.Child =
                new popupscreen
                    ();
            buyNowScreen.IsOpen = true;
            buyNowScreen.VerticalOffset = 100;
            buyNowScreen.HorizontalOffset = 25;

        }
      }

我编写了以下代码,用于从弹出页面导航到其他页面,但是它无法获取NullReferenceException。

   private void button3_Click(object sender, RoutedEventArgs e)
    {

        ClosePopup();
        NavigationService.Navigate(new Uri("/Menu.xaml", UriKind.Relative));
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        ClosePopup();

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

    }


    private void ClosePopup()
    {
        Popup buyPop = this.Parent as Popup;
        if (buyPop.IsOpen)
        {
            buyPop.IsOpen = false;


        }

    }

2 个答案:

答案 0 :(得分:1)

导航服务在页面之间起作用,你不能使用如此简单的弹出窗口。该服务无法找到,使用哪个页面,这就是它抛出异常的原因。

要解决您的问题,您应该这样做:

弹出类

1)在你的弹出构造函数中,你应该得到PhoneApplicationPage对象。

      private PhoneApplicationPage _page;

      public SomePopup(PhoneApplicationPage page)
            {
             _page = page;

2)您应该为点击事件创建一个新类型。

        public delegate void NavigateHandler(object sender, EventArgs e, PhoneApplicationPage page);

        public event NavigateHandler NavigateFromPopup;

3)在你的button_click事件中你应该调用它:

public void ButtonX_Click(object sender, RoutedEventArgs e)
        {
               if (NavigateFromPopup!= null)
                NavigateFromPopup(this, EventArgs.Empty, _page);
        }

您的网页课程

4)在您的页面上,当您创建弹出窗口时,您应该添加此eventHandler:

            SomePopup p = new SomePopup(this);
            p.NavigateFromPopup +=new SomePopup.NavigateHandler(p_NavigateFromPopup);

5)最后,在你的页面上你应该记下这个事件:

private void p_NavigateFromPopup(object sender, EventArgs e, PhoneApplicationPage page)
        {
            page.NavigationService.Navigate(new Uri("...", UriKind.RelativeOrAbsolute));
        }

这应该有效。

答案 1 :(得分:1)

以下是为弹出页面控件提供事件的过程。

    Popup buyNowScreen=new Popup();

    popupscreen popup1=new popscreen();
    buyNowScreen.Child =popup1;
    buyNowScreen.isOpen=true;
    popup1.button1.click+= new RoutedEventHandler(btn_playagain_click);
    private void btn_playagain_click(object sender, EventArgs e)
     {
        p.IsOpen = false;
        NavigationService.Navigate(new Uri("/NumericEasy.xaml?Refresh=true", UriKind.Relative));
    }