Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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# 如何在WinRT课堂上布置活动?_C#_Events_Windows Runtime - Fatal编程技术网

C# 如何在WinRT课堂上布置活动?

C# 如何在WinRT课堂上布置活动?,c#,events,windows-runtime,C#,Events,Windows Runtime,在经典类库中,我可以执行以下操作: public event EventHandler Changed { add { decorated.Changed += value; } remove { decorated.Changed -= value; } } 现在,如果我使用winRT类,编译器会抱怨这样的代码。我设法修补了“添加”,但仍坚持删除: public event EventHandler Changed { add { return decorated.Changed

在经典类库中,我可以执行以下操作:

public event EventHandler Changed
{
  add { decorated.Changed += value; }
  remove { decorated.Changed -= value; }
}
现在,如果我使用winRT类,编译器会抱怨这样的代码。我设法修补了“添加”,但仍坚持删除:

public event EventHandler Changed
{
  add { return decorated.Changed += value; }
  remove { decorated.Changed -= value; }
}
我应该如何实现删除部分

存储委托和事件标记之间的映射,以支持托管代码中Windows运行时事件的实现

当需要手动管理事件的添加和删除时,请使用此类型

此表的实例存储代表已添加到事件的事件处理程序的委托。要引发事件,请调用
InvocationList
属性返回的委托(如果它不是
null
)。每个事件都需要此表的一个实例

比如说,

private EventRegistrationTokenTable<EventHandler<Object>> _changed
     = new EventRegistrationTokenTable<EventHandler<Object>>();

public event EventHandler<Object> Changed
{
    add    { return _changed.AddEventHandler(value); }
    remove { _changed.RemoveEventHandler(value);     }
}
private EventRegistrationTokenTable\u已更改
=新的EventRegistrationTokenTable();
公共事件事件处理程序已更改
{
添加{return\u changed.AddEventHandler(value);}
删除{u已更改。RemoveEventHandler(值);}
}

存储委托和事件标记之间的映射,以支持托管代码中Windows运行时事件的实现

当需要手动管理事件的添加和删除时,请使用此类型

此表的实例存储代表已添加到事件的事件处理程序的委托。要引发事件,请调用
InvocationList
属性返回的委托(如果它不是
null
)。每个事件都需要此表的一个实例

比如说,

private EventRegistrationTokenTable<EventHandler<Object>> _changed
     = new EventRegistrationTokenTable<EventHandler<Object>>();

public event EventHandler<Object> Changed
{
    add    { return _changed.AddEventHandler(value); }
    remove { _changed.RemoveEventHandler(value);     }
}
private EventRegistrationTokenTable\u已更改
=新的EventRegistrationTokenTable();
公共事件事件处理程序已更改
{
添加{return\u changed.AddEventHandler(value);}
删除{u已更改。RemoveEventHandler(值);}
}

一种解决方案是创建一个伪备份事件和查找字典,用于存储转发事件所需的信息。例如:

public event EventHandler<Object> Changed
{
  add
  {
    // get fake token so that we can return something and/or unsubscribe
    EventRegistrationToken token = _changed.AddEventHandler(value);
    // subscribe and store the token-handler pair
    _otherClass.Event += value;
    _dictionary[token] = value;
    return token;
  }
  remove
  {
    // recall value and remove from dictionary
    _otherClass.Event -= _dictionary[value];
    _dictionary.Remove(value);
    // remove it from the "fake" event
    _changed.RemoveEventHandler(value);
  }
}

private EventRegistrationTokenTable<EventHandler<Object>> _changed;
private Dictionary<EventRegistrationToken, EventHandler<Object>> _dictionary;
公共事件事件处理程序已更改 { 添加 { //获取假代币,以便我们可以返回某些内容和/或取消订阅 EventRegistrationToken=_changed.AddEventHandler(值); //订阅并存储令牌处理程序对 _otherClass.Event+=值; _字典[标记]=值; 返回令牌; } 去除 { //调用值并从字典中删除 _otherClass.Event-=\u字典[值]; _删除(值); //将其从“假”事件中删除 _已更改。RemoveEventHandler(值); } } private EventRegistrationTokenTable已更改; 私人词典; 或者,从WinRT类订阅CLR事件可能更容易,只需将CLR事件转发给WinRT订阅者即可;或多或少地从您的要求中反转流程:

public WinRtObject()
{
  _otherClass.Event += (sender, o) => OnChanged(o);
}

public event EventHandler<Object> Changed;

private void OnChanged(object e)
{
  EventHandler<object> handler = Changed;
  if (handler != null)
    handler(this, e);
}
public WinRtObject()
{
_otherClass.Event+=(发送方,o)=>OnChanged(o);
}
公共事件事件处理程序已更改;
更改后的专用void(对象e)
{
EventHandler=已更改;
if(处理程序!=null)
处理者(本,e);
}

一种解决方案是创建一个伪备份事件和查找字典,用于存储转发事件所需的信息。例如:

public event EventHandler<Object> Changed
{
  add
  {
    // get fake token so that we can return something and/or unsubscribe
    EventRegistrationToken token = _changed.AddEventHandler(value);
    // subscribe and store the token-handler pair
    _otherClass.Event += value;
    _dictionary[token] = value;
    return token;
  }
  remove
  {
    // recall value and remove from dictionary
    _otherClass.Event -= _dictionary[value];
    _dictionary.Remove(value);
    // remove it from the "fake" event
    _changed.RemoveEventHandler(value);
  }
}

private EventRegistrationTokenTable<EventHandler<Object>> _changed;
private Dictionary<EventRegistrationToken, EventHandler<Object>> _dictionary;
公共事件事件处理程序已更改 { 添加 { //获取假代币,以便我们可以返回某些内容和/或取消订阅 EventRegistrationToken=_changed.AddEventHandler(值); //订阅并存储令牌处理程序对 _otherClass.Event+=值; _字典[标记]=值; 返回令牌; } 去除 { //调用值并从字典中删除 _otherClass.Event-=\u字典[值]; _删除(值); //将其从“假”事件中删除 _已更改。RemoveEventHandler(值); } } private EventRegistrationTokenTable已更改; 私人词典; 或者,从WinRT类订阅CLR事件可能更容易,只需将CLR事件转发给WinRT订阅者即可;或多或少地从您的要求中反转流程:

public WinRtObject()
{
  _otherClass.Event += (sender, o) => OnChanged(o);
}

public event EventHandler<Object> Changed;

private void OnChanged(object e)
{
  EventHandler<object> handler = Changed;
  if (handler != null)
    handler(this, e);
}
public WinRtObject()
{
_otherClass.Event+=(发送方,o)=>OnChanged(o);
}
公共事件事件处理程序已更改;
更改后的专用void(对象e)
{
EventHandler=已更改;
if(处理程序!=null)
处理者(本,e);
}

编译器的抱怨是什么?它说“无法将类型”System.Runtime.InteropServices.WindowsRuntime.EventRegistrationToken“隐式转换为”System.EventHandler“。是的,这在WinRT中已更改,EventRegistrationToken是基本类型,它存储事件的cookie。我只能找到一个C++/CX示例:编译器的抱怨是什么?它说“无法将类型”System.Runtime.InteropServices.WindowsRuntime.EventRegistrationToken“隐式转换为”System.EventHandler“。是的,这在WinRT中已更改,EventRegistrationToken是基本类型,它存储事件的cookie。我只能找到一个C++/CX示例:是-这是我实现自定义事件的方式,但它不处理来自其他类的装饰事件。如果我应该使用关联的EventRegistrationTokenTable实例,我需要访问它。它在教室里。如果装饰类不公开它呢?是的-这是我实现自定义事件的方式,但它不处理来自其他类的装饰事件。如果我应该使用关联的EventRegistrationTokenTable实例,我需要访问它。它在教室里。如果装饰类不公开它呢?