简体   繁体   English

Windows Phone 8:消息框已停止工作?

[英]Windows Phone 8: Message box has stopped working?

So im working on an application using the motion api and updating via event handles. 因此,我使用运动api在应用程序上进行工作并通过事件句柄进行更新。 The issue is i am having trouble getting the message box to show and i cant understand why. 问题是我在显示消息框时遇到麻烦,我无法理解原因。 basic code below: 基本代码如下:

    public MainPage()
    {
        InitializeComponent();
        MessageBox.Show("welcome"); //box not showing
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {

            motion = new Motion();
            motion.TimeBetweenUpdates = TimeSpan.FromMilliseconds(20);
            motion.CurrentValueChanged +=
                new EventHandler<SensorReadingEventArgs<MotionReading>>         (motion_CurrentValueChanged);

            motion.Start();

    }


    void motion_CurrentValueChanged(object sender, SensorReadingEventArgs<MotionReading> e)
    {
        Dispatcher.BeginInvoke(() => CurrentValueChanged(e.SensorReading));
    }



    private void CurrentValueChanged(MotionReading e)
    {
            Thickness mar = characterMain.Margin;

            txtblck1.Text = "Yaw " + e.Attitude.Yaw.ToString() + " Pitch " + e.Attitude.Pitch + " Roll " + e.Attitude.Roll;

            mar.Left = hor + (e.Attitude.Roll * 200);
            mar.Top = vert + (e.Attitude.Pitch * 200);
            characterMain.Margin = mar;

            bool col = engine1.CDetection_V1(characterMain.Margin.Left, characterMain.Margin.Top, characterMain.Width, characterMain.Height, BadGuy.Margin.Left, BadGuy.Margin.Top, BadGuy.Width, BadGuy.Height);
            if (col == true)
            {
                MessageBox.Show("hit");//this doesnt
                num.Text = "hit"; //this works
            }


    }

Okay so problem solved! 好了,问题解决了! Turns out that the problem was not my code or how vs was set up it was infact my phone! 事实证明,问题出在我的手机上,而不是我的代码,或者vs的设置方式不对! i had been testing on my 1020 and as a last resort before reinstalling vs2013 i decided to try another app that i knew had a message box on my phone! 我一直在1020上进行测试,并且在重新安装vs2013之前作为不得已的办法,我决定尝试另一个我知道手机上有消息框的应用程序! and noticed it wasnt appearing a simple restart fixed this and my code started working! 并注意到它没有出现简单的重新启动即可解决此问题,并且我的代码开始工作! so it looks like a bug in WP that must happen every now and then! 所以看起来WP中的错误必须时不时发生! Thanks to everyone for the help especially Romasz 感谢大家的帮助,特别是Romasz

尝试在页面的Loaded事件中使用Messagebox.Show() ,而不在构造函数中使用它。

Try using the following code. 尝试使用以下代码。

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        //Your logic
        MessageBox.Show("Welcome");
    }

Difference between using in OnNavigatedTo() and MainPage() is: 在OnNavigatedTo()和MainPage()中使用之间的区别是:

  1. The code will runs only once in MainPage(){}. 该代码仅在MainPage(){}中运行一次。 Eventhough you go back to the MainPage.xaml, the code will not run. 即使您返回MainPage.xaml,代码也不会运行。
  2. Whereas, the code in OnNavigatedTo() will runs everytime you navigate to MainPage.xaml. 而每次您导航到MainPage.xaml时,OnNavigatedTo()中的代码将运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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