1

我正在尝试处理来自本机模块的异常,用于 Windows 的本机反应,我已经按照 Windows 文档的本机反应编写了本机模块, 但是当我在本机模块中触发错误时,我希望函数 App_UnhandledException 或 TaskScheduler_UnobservedTaskException 捕获例外,但事实并非如此。

他们有什么方法可以从本机模块中捕获异常吗?

应用程序.xaml.cs

using Microsoft.ReactNative;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace testprojecthandlederrors
{
sealed partial class App : ReactApplication
{
    public App()
    {
   #if BUNDLE
        JavaScriptBundleFile = "index.windows";
        InstanceSettings.UseWebDebugger = false;
        InstanceSettings.UseFastRefresh = false;
#else
        JavaScriptBundleFile = "index";
        InstanceSettings.UseWebDebugger = true;
        InstanceSettings.UseFastRefresh = true;
#endif

#if DEBUG
        InstanceSettings.UseDeveloperSupport = true;
#else
        InstanceSettings.UseDeveloperSupport = false;
#endif

        Microsoft.ReactNative.Managed.AutolinkedNativeModules.RegisterAutolinkedNativeModulePackages(PackageProviders); // Includes any autolinked modules

        PackageProviders.Add(new Microsoft.ReactNative.Managed.ReactPackageProvider());
        PackageProviders.Add(new ReactPackageProvider());
        this.UnhandledException += App_UnhandledException;
        TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
        InitializeComponent();
    }

    private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
    {
        var e_message = e.Exception.Message;
        Debug.WriteLine("Error Detected from Another thread", e_message);
    }

    private void App_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        var e_message = e.Exception.Message;
        Debug.WriteLine("Error Detected from Main thread", e_message);
    }

    /// <summary>
    /// Invoked when the application is launched normally by the end user.  Other entry points
    /// will be used such as when the application is launched to open a specific file.
    /// </summary>
    /// <param name="e">Details about the launch request and process.</param>
    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        base.OnLaunched(e);
        var frame = (Frame)Window.Current.Content;
        frame.Navigate(typeof(MainPage), e.Arguments);
    }



    /// <summary>
    /// Invoked when the application is activated by some means other than normal launching.
    /// </summary>
    protected override void OnActivated(IActivatedEventArgs e)
    {
        var preActivationContent = Window.Current.Content;
        base.OnActivated(e);
        if (preActivationContent == null && Window.Current != null)
        {
            // Display the initial content
            var frame = (Frame)Window.Current.Content;
            frame.Navigate(typeof(MainPage), null);
        }
    }
}
}

自定义模块.cs

using Microsoft.ReactNative.Managed;

namespace testprojecthandlederrors
{
[ReactModule]
class CustomModule
{
    [ReactMethod("saveImage")]
    public void SaveImage(string base64)
    {
       //Throw error
       var base64String = base64.Replace("data:image/png;base64,", string.Empty);
    }
}
}

应用程序.js

...
const testException = () => {
  NativeModules.CustomModule.saveImage();
}
... 
4

0 回答 0