[W8.1] [C#]使用反射添加事件处理程序 [英] [W8.1] [C#] Adding an event handler using reflection

查看:52
本文介绍了[W8.1] [C#]使用反射添加事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows 8.1提供了仍受Windows 10支持的SettingsPane类,但Microsoft文档将其视为遗留。

Windows 8.1 provides SettingsPane class which is still supported by Windows 10, but Microsoft documentation considers it as a legacy.

我的Windows应用程序旨在使用SettingsPane类,如果它存在,但即使在此课程无法使用之后仍继续工作。为实现这一目标,自定义类SettingsMenu有一个静态方法GetForCurrentView,它返回null或
一个类的实例来处理:

My Windows app is designed to use SettingsPane class, if it is present, but to continue working even after this class becomes unavailable. To achieve that, it a custom class SettingsMenu has a static method GetForCurrentView, which returns either null, or an instance of class to work with:

internal static SettingsMenu GetForCurrentView() {    if (Type.GetType("Windows.UI.ApplicationSettings.SettingsPane, Windows.UI, ContentType=WindowsRuntime") != null) {      var pane = SettingsPane.GetForCurrentView();      if (pane != null) return new SettingsMenu(pane);  }  return null;   }  private SettingsMenu(SettingsPane settingsPane) {

   。 。 。  

   . . . . 

   settingsPane.CommandsRequested + = onPaneCommandsRequested;

   settingsPane.CommandsRequested += onPaneCommandsRequested;

}

private void onPaneCommandsRequested(SettingsPane sender,SettingsPaneCommandsRequestedEventArgs args){

private void onPaneCommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args) {

                             .....

                            .....

}




 它可以正常工作,但通过使用reflecion来避免显式引用SettingsPane和相关内容可能会更安全:

  It works all right, but probably will be safer to avoid explicit reference to SettingsPane and relevant stuff, by using reflecion:

   internal static SettingsMenu GetForCurrentView() {    Type paneType = Type.GetType("Windows.UI.ApplicationSettings.SettingsPane, Windows.UI, ContentType=WindowsRuntime");

    if(paneType!= null){

    if (paneType != null) {

        var pane = paneType.GetRuntimeMethod(" GetForCurrentView",new Type [] {})。Invoke(null,new Object [] {});

        var pane = paneType.GetRuntimeMethod("GetForCurrentView", new Type[] { }).Invoke( null, new Object[] { });

       if(pane!= null)返回新的SettingsMenu(窗格); 

       if (pane != null) return new SettingsMenu(pane); 

    }

    }

   返回null; 

    return null; 

}

  private SettingsMenu(object settingsPane){

  private SettingsMenu(object settingsPane) {

。 。

     settingsPane.GetType()。GetRuntimeEvent(" CommandsRequested")。AddMethod。

     settingsPane.GetType().GetRuntimeEvent("CommandsRequested").AddMethod.

Invoke(settingsPane,new object [] {onPaneCommandsRequested});   

Invoke(settingsPane, new object[]{onPaneCommandsRequested});   

  ; }

我对AddMethod(替换event + =)有一个编译问题,它不接受onPaneCommandsRequested作为对象。 MSDN提供了一些将方法转换为对象的方法(例如new Action<>()),但我尝试的任何代码都不会编译
代码。

I have a compilation problem with AddMethod (to replace event += ), which does not accept onPaneCommandsRequested as an object. The MSDN methions some method to convert an method to an object (e.g. new Action<> ()), but nothing I tried makes the code to compile.

任何建议?
$

Any suggestions ?

推荐答案

请按
发布指南:主题行标签

为什么要在这里反思?

Why bother with reflection here?

是否使用SettingsPane是基于运行它的操作系统上的应用程序类型:如果您构建为Windows 8.1应用程序,请使用SettingsPane。升级到UWP应用程序时请勿使用它。 SettingsPane不会对Windows 8.1应用程序不可用。它是
已经对Windows 10应用程序没用。

Whether to use the SettingsPane or not is based on the type of app not on the OS on which it runs: Use the SettingsPane if you build as a Windows 8.1 app. Don't use it if upgrade to a UWP app. SettingsPane won't become unavailable to Windows 8.1 apps. It's already not useful for Windows 10 apps.

如果你想从Windows 8.1应用程序调用Windows 10 API,我只会使用反射。

I'd only use reflection if you want to call a Windows 10 API from your Windows 8.1 app.

UWP应用程序不需要反映功能检测,因为他们可以使用ApiInformation类。

UWP apps don't need reflection for feature detection since they can use the ApiInformation class.


这篇关于[W8.1] [C#]使用反射添加事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆