Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当EventHandler完成时导航到页面?_C#_Windows Phone 7_Windows Phone 7.1_Windows Phone_Windows Phone 7.1.1 - Fatal编程技术网

C# 当EventHandler完成时导航到页面?

C# 当EventHandler完成时导航到页面?,c#,windows-phone-7,windows-phone-7.1,windows-phone,windows-phone-7.1.1,C#,Windows Phone 7,Windows Phone 7.1,Windows Phone,Windows Phone 7.1.1,所以我尝试在WP7中做一件非常简单的事情,比如: 主页上的一个按钮将启动相机,当相机成功拍摄照片时,我想将照片传递到第二页并启动它 这是我的密码: 在主页构造函数中,我初始化相机任务并设置委托: camTask.Completed+=新事件处理程序(camTask_Completed) 然后我实现了camTask\u Completed void camTask_Completed(object sender, PhotoResult e) { //throw new

所以我尝试在WP7中做一件非常简单的事情,比如:

主页上的一个按钮将启动相机,当相机成功拍摄照片时,我想将照片传递到第二页并启动它

这是我的密码:

在主页构造函数中,我初始化相机任务并设置委托:

camTask.Completed+=新事件处理程序(camTask_Completed)

然后我实现了
camTask\u Completed

 void camTask_Completed(object sender, PhotoResult e)
    {
        //throw new NotImplementedException();
        
        img = PictureDecoder.DecodeJpeg(e.ChosenPhoto);
        NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative));
    }
该应用程序将运行无误,直到我按下“接受”后,我拍了照片

例外情况是:

Exception{“当任务不在前台时,不允许导航。”}System.Exception{System.invalidooperationexception}

我的理解是,我不应该在
camTask\u Completed
方法中启动第二个页面

那么我的问题是:如何启动关于EventHandler结果的另一个页面

谢谢

更新:(有关此子问题的答案,请参阅本页中的此部分)

单击按钮(启动相机)后,我发现另一个错误:

它抛出了一个例外,说:

<代码> >“类型”系统。Windows .Media.Trave'不能序列化。请考虑用DATACONTractAttor属性标记它,并标记要用DATAMEMBAREATION属性序列化的所有成员。“< /代码>

在哪里可以序列化
转换
内容

我在谷歌上搜索了一下,发现:

找到答案后,错误实际上也表明了这一点:)

[数据合同]

[KnownType(typeof(System.Windows.Media.MatrixTransform))]

似乎它可以解决这个问题,但我应该把这些线放在哪里

这是我在主页上将图像传递到第二页的代码,
img
是一个
WriteableBitmap

 protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);
        var brush = new ImageBrush();
        brush.ImageSource = img;
        PhoneApplicationService.Current.State["bkg"] = brush;
    }

再次感谢。

也许您应该尝试使用dispatcher:

activePage.Dispatcher.BeginInvoke(
     () => NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative)));

这在MetroApps中有效。

也许您应该尝试使用dispatcher:

activePage.Dispatcher.BeginInvoke(
     () => NavigationService.Navigate(new Uri("/SecondPage.xaml", UriKind.Relative)));

这适用于MetroApps。

这是CameraCaptureTask的一个已知问题。
以下是我使用的解决方法:

    void OnCameraCaptureTaskCompleted(object sender, PhotoResult args)
    {
        //Delay navigation until the first navigated event
        NavigationService.Navigated += new NavigatedEventHandler(navigateCompleted);
    }

    void navigateCompleted(object sender, EventArgs e)
    {
        //Do the delayed navigation from the main page
        this.NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
        NavigationService.Navigated -= new NavigatedEventHandler(navigateCompleted);
    }

这是CameraCaptureTask的一个已知问题。
以下是我使用的解决方法:

    void OnCameraCaptureTaskCompleted(object sender, PhotoResult args)
    {
        //Delay navigation until the first navigated event
        NavigationService.Navigated += new NavigatedEventHandler(navigateCompleted);
    }

    void navigateCompleted(object sender, EventArgs e)
    {
        //Do the delayed navigation from the main page
        this.NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));
        NavigationService.Navigated -= new NavigatedEventHandler(navigateCompleted);
    }

thx对于您的快速响应,错误是,我认为,因为
camTask_Completed
是从摄像头启动的方法,因此主页不在前台,因此无法执行调度程序。问题是只有前台线程可以访问UI。试试这个:void camTask_Completed(object sender,PhotoResult e){//throw new NotImplementedException();img=picturedcoder.DecodeJpeg(e.ChosenPhoto);mainpage.Dispatcher.BeginInvoke(()=>NavigationService.Navigate(newURI(“/SecondPage.xaml”,UriKind.Relative));}thx mate,我尝试了你的方法,也发现了另一个错误,请参考我更新的问题thanx.)thx对于您的快速响应,错误是,我认为,因为
camTask_Completed
是从摄像头启动的方法,因此主页不在前台,因此无法执行调度程序。问题是只有前台线程可以访问UI。试试这个:void camTask_Completed(object sender,PhotoResult e){//throw new NotImplementedException();img=picturedcoder.DecodeJpeg(e.ChosenPhoto);mainpage.Dispatcher.BeginInvoke(()=>NavigationService.Navigate(newURI(“/SecondPage.xaml”,UriKind.Relative));}thx mate,我尝试了你的方法,也发现了另一个错误,请参考我更新的问题thanx.)我认为这是另一个问题。似乎您正在尝试序列化对象。您正在保存对象状态吗
SolidColorBrush
具有类型为
Transform
的名为
Transform
的属性。如果是那样的话,你应该序列化颜色。是的,我正在尝试将相机照片传递到第二页的画布上,并将其设置为
canvas.background
。在这种情况下,你应该将生成的图像保存到IsolatedStorage中,然后在第二页阅读。看这里:我认为这是另一个问题。似乎您正在尝试序列化对象。您正在保存对象状态吗
SolidColorBrush
具有类型为
Transform
的名为
Transform
的属性。如果是那样的话,你应该序列化颜色。是的,我正在尝试将相机照片传递到第二页的画布上,并将其设置为
canvas.background
。在这种情况下,你应该将生成的图像保存到IsolatedStorage中,然后在第二页阅读。请看这里: