3

我需要为每个页面注册不同的分享魅力监听器。我有 2 页。我在每一个中添加了以下代码:

        DataTransferManager.GetForCurrentView().DataRequested += App_DataRequested;

我在一个页面的构造函数和另一个页面的 UserControl_Loaded 事件中添加了它(第一页没有 UserControl_Loaded 所以我直接将它添加到构造函数)。在第二页尝试加载的那一刻,我得到了异常:

WinRT information: An event handler has already been registered
Additional information: A method was called at an unexpected time.

我应该把它放在哪里,什么是“正确”的时间?同样令人困惑的是,我们为每个视图都有不同的 DataTransferManager,但当前只有一个处于活动状态。更重要的是,我注意到,如果你只为第一页添加一个监听器,其他页面无论如何都会共享这个监听器。如果我对所有页面只有一个共享侦听器,在 app.xaml.cs 中注册它是否正确?

4

4 回答 4

2

我解决此问题的方法是取消注册 onNavigatedfrom 事件中的事件,如下所示:

受保护的覆盖无效 OnNavigatedFrom(NavigationEventArgs e) { DataTransferManager.GetForCurrentView().DataRequested -= App_DataRequested; base.OnNavigatedFrom(e); }

于 2012-12-07T05:37:21.960 回答
1

在构造函数的 BasePage.cs 中,我添加了

public BasePage()
{        
    if (!_isListenToDataRequested)
    {
        _isListenToDataRequested = true;

        DataTransferManager manager = DataTransferManager.GetForCurrentView();  
        manager.DataRequested += AppDataRequested;
    }
}

private async void AppDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
    IShareable shareable = Frame.Content as IShareable;
    if (shareable != null)
    {
        DataRequestDeferral deferral = args.Request.GetDeferral();

        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => shareable.AppDataRequested(sender, args));

        deferral.Complete();
    }
}  

我所有的页面看起来像

public sealed partial class ContentPage : IShareable
{
    public void AppDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
    {...}
}
于 2013-04-12T18:03:36.903 回答
0

我建议在导航事件中执行此操作, OnNavigatingFrom 事件将在您要访问的页面的 OnNavigatingTo 之前触发,因此您不会遇到此问题。

protected override Task OnNavigatingTo(WinRTXamlToolkit.Controls.AlternativeNavigationEventArgs e)
{
    DataTransferManager.GetForCurrentView().DataRequested += dataTransfer_DataRequested;
    return base.OnNavigatingTo(e);
}

protected override Task OnNavigatingFrom(WinRTXamlToolkit.Controls.AlternativeNavigatingCancelEventArgs e)
{
    DataTransferManager.GetForCurrentView().DataRequested -= dataTransfer_DataRequested;
    return base.OnNavigatingFrom(e);
}

//Note: This is the WinRT Xaml Toolkit version of the events, but the standard events will work the same way.
于 2013-04-12T12:47:59.853 回答
0

另一个解决方案如下运行

private DataTransferManager dataTransferManager;

把它放在页面加载事件中

 this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new DispatchedHandler(() =>
            {
                this.dataTransferManager = DataTransferManager.GetForCurrentView();
                this.dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.OnDataRequested);
            }));

protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        // Unregister the current page as a share source.
        this.dataTransferManager.DataRequested -=
            new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>
                (this.OnDataRequested);
    }
于 2013-03-11T08:58:21.157 回答