在设备中抛出InvalidOperationException但不是模拟器

时间:2012-10-25 14:31:07

标签: silverlight windows-phone-7 invalidoperationexception

在我的WP7应用中,InvalidOperationException被抛入设备中(虽然它在模拟器中正常工作)。从Microsoft提供的堆栈跟踪,似乎源可能是代码。

Image start_rec = (Image)FindName(start_name);
// Return the general transform for the specified visual object.
GeneralTransform generalTransform1 = canvas1.TransformToVisual(start_rec);
pt = generalTransform1.Transform(new Point(0, 0));
double start_y = pt.Y * (-1);
double start_x = pt.X * (-1);
Image destination_rec = (Image)FindName("dot" + destination_name);
// Return the general transform for the specified visual object.
generalTransform1 = canvas1.TransformToVisual(destination_rec);

// Retrieve the point value relative to the child.
pt = generalTransform1.Transform(new Point(0, 0));
double destination_y = pt.Y * (-1);
double destination_x = pt.X * (-1);

我在上面的代码中尝试做什么: canvas1中有图片。我试图通过画布获得图像的位置。

我的问题是为什么我会收到Exception

编辑:Stacktrace

Frame    Image             Function                                            Offset    
0        coredll.dll       xxx_RaiseException                                  19        
1        mscoree3_7.dll                                                        436172    
2        mscoree3_7.dll                                                        383681    
3        mscoree3_7.dll                                                        540620    
4                          TransitionStub                                      0         
5                          Microsoft.Xna.Framework.Audio.SoundEffect.Play      200       
6                          Microsoft.Xna.Framework.Audio.SoundEffect.Play      68        
7                          BoxIt.MainPage.correct_line                         1780      
8                          BoxIt.MainPage.evaluate                             360       
9                          BoxIt.MainPage.canvas1_ManipulationCompleted        196       
10                         MS.Internal.CoreInvokeHandler.InvokeEventHandler    2752      
11                         MS.Internal.JoltHelper.FireEvent                    1324      
12       mscoree3_7.dll                                                        428848    
13       mscoree3_7.dll                                                        430212    
14       mscoree3_7.dll                                                        610999    
15       mscoree3_7.dll                                                        374145    
16                                                                             0         
17       agcore.dll        CCoreServices::CLR_FireEvent                        385       
18       npctrl.dll        CControlBase::ScriptCallback                        435       
19       npctrl.dll        CXcpDispatcher::OnScriptCallback                    547"

编辑2:功能correct_line()

内的代码
void correct_line(int a_h, int a_v)
    {
        Point pt;
        int destination_num;
        string destination_name;

        string[] words = start_name.Split('t');
        //textBox1.Text = words[1];

        int.TryParse(words[1], out destination_num);
        int start_num = destination_num;
        destination_num = destination_num + a_h + a_v * 4;
       // textBox1.Text = start_num + "  " + destination_num;
        Boolean u = true;
        if (list.Count > 0)
        {
            u=!(list.Contains(start_num + destination_num));
        }
        if (u)
        {
            if (start_num > 0 & destination_num > 0)
            {
                int po = Math.Abs(start_num-destination_num);
                if(po==1 | po==4)
                {
                destination_name = (destination_num).ToString();
                Image start_rec = (Image)FindName(start_name);
                // Return the general transform for the specified visual object.
                GeneralTransform generalTransform1 = canvas1.TransformToVisual(start_rec);
                pt = generalTransform1.Transform(new Point(0, 0));
                double start_y = pt.Y * (-1);
                double start_x = pt.X * (-1);
                Image destination_rec = (Image)FindName("dot" + destination_name);
                // Return the general transform for the specified visual object.
                generalTransform1 = canvas1.TransformToVisual(destination_rec);

                // Retrieve the point value relative to the child.
                pt = generalTransform1.Transform(new Point(0, 0));
                double destination_y = pt.Y * (-1);
                double destination_x = pt.X * (-1);

                //textBox1.Text = " " + start_num + " " + a_h + " " + destination_num + " " + a_v ;
                Line line = new Line() { X1 = start_x + dotwidth / 2, Y1 = start_y + dotwidth / 2, X2 = destination_x + dotwidth / 2, Y2 = destination_y + dotwidth / 2 };
                if (msg == "2" & move == 2)
                {
                    line.Stroke = new SolidColorBrush(Colors.Black);
                }
                else
                {
                    line.Stroke = new SolidColorBrush(Colors.White);
                }
                line.StrokeThickness = 15;
                line.SetValue(Canvas.ZIndexProperty, 25);
                line.Tag = "correct_line";
                this.canvas1.Children.Add(line);
                if (gsplay == 1)
                {
                    //stroke.Play();
                    strokemusic.Play();
                }
                //
                update_matrix(start_num, destination_num);
                list.Add(start_num + destination_num);


                //
                h = 0;
                v = 0;
                //move = checkfor_box(start_num, destination_num, a_h, a_v);
                move = checkfor_box3();
                find_move();

            }
            }
        }
    }

SoundeffectOnNavigatedTo函数

中启动
 protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);


        if (settings.Contains("gs"))
        {
            string hx = (string)settings["gs"];

            if (hx == "1")
            {
                gsplay = 1;
                var laserStream1 =  Application.GetResourceStream(new Uri("boxdone.wav", UriKind.Relative));

                boxdonemusic = SoundEffect.FromStream(laserStream1.Stream);



                var laserStream2 =  Application.GetResourceStream(new Uri("stroke.wav", UriKind.Relative));

                strokemusic = SoundEffect.FromStream(laserStream2.Stream);

            }
            else
            {
                gsplay = 0;
            }
        }



    }

1 个答案:

答案 0 :(得分:1)

您的strokemusic.Play();方法的correct_line行会抛出异常,这是非常肯定的。现在的问题是知道原因。

在挖掘一下之后,似乎在播放声音时出现InvalidOperationException的两个主要原因是:

  • 声音不以受支持的格式存储。它不太可能,因为它可能会在模拟器中崩溃
  • 手机已连接到Zune,在这种情况下,不允许应用程序播放声音。它还可以解释为什么错误仅发生在真实设备上(模拟器未连接到Zune)。

如何解决问题? Simpy用try / catch子句包围你的行:

try
{
    strokemusic.Play();
}
catch (Exception) { }