문제

So im working on an application using the motion api and updating via event handles. 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! 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! 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! Thanks to everyone for the help especially Romasz

다른 팁

Try using the following code.

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

Difference between using in OnNavigatedTo() and MainPage() is:

  1. The code will runs only once in MainPage(){}. Eventhough you go back to the MainPage.xaml, the code will not run.
  2. Whereas, the code in OnNavigatedTo() will runs everytime you navigate to MainPage.xaml.

Try using your Messagebox.Show() in the Loaded event of the page instead of using it in the constructor..

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top