0

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

  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;


        }

    }
4

2 回答 2

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)在您的页面上,当您创建一个弹出窗口时,您应该添加此事件处理程序:

            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));
        }

那应该行得通。

于 2012-12-13T06:40:58.323 回答
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));
    }
于 2012-12-17T06:01:38.230 回答