我创建了一个事件接收器,但问题是我无法获得对SPContext:SPContext.Currentreturn 的引用null.我需要它来添加一些列表到网站.有谁知道我怎么能得到它?
此外,我尝试在事件接收器中放置断点,但FeatureActivates由于某种原因从未触发.在部署之后立即激活列表时使用的正确事件是什么?
编辑2016年5月8日
我发现我的麻烦在我的应用程序中接收媒体按钮事件的原因.请参阅以下答案.我编辑了这个问题的标题,以便更容易地找到问题.最初的标题是"什么可以阻止Android Lollipop上的媒体按钮".
原始问题,2015年4月:
抓我的头,盯着所有的代码2天无济于事...我的Android应用程序应该对媒体按钮做出反应(例如从耳机,用蓝牙耳机测试),如播放/暂停,接下来,倒带.适用于KitKat及以下版本.我发誓它甚至在棒棒糖上工作,直到几天前.现在什么都没有,没有它听到媒体按钮按下的痕迹.有人会快速建议在哪里寻找麻烦吗?测试了两个Lollipop手机,相同的蓝牙耳机和相同的耳机适用于较低版本的Android.同样的耳机工作正常,在其他应用程序中听到媒体按钮按下.我怎么可能打破???
我现在测试了听新闻媒体按钮的新旧方式.在AndroidManifest.xml中:
<receiver android:name=".MediaButtonIntentReceiver" android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
它表示enabled ="false"的事实是正常的 - 我根据需要启用和禁用接收器,MediaButtonIntentReceiver.java在KitKat上获得正常事件,并在Lollipop上完全沉默.
我接下来切换到最新的appcompat(v22.1)并尝试使用MediaSessionCompat对象和相关代码,如下所示.这在一个小型测试应用程序中运行得很好,只是我写的一个活动 - 得到了我的Logcat消息,确认它听到了Lollipop上按下的媒体键.但是当插入我的应用程序时,再次无法在Lollipop上运行.有没有搞错???
private MediaSessionCompat _mediaSession;
final String MEDIA_SESSION_TAG = "some_tag";
void initMediaSessions(Context context) {
// Start a new MediaSession
if (context == null)
return;
Lt.d("initMediaSession()...");
ComponentName eventReceiver = new ComponentName(context.getPackageName(), MediaButtonIntentReceiver.class.getName());
PendingIntent buttonReceiverIntent = PendingIntent.getBroadcast(
context,
0,
new Intent(Intent.ACTION_MEDIA_BUTTON),
PendingIntent.FLAG_UPDATE_CURRENT
);
// Parameters for new MediaSessionCompat():
// context The context.
// tag A short name for debugging purposes. …Run Code Online (Sandbox Code Playgroud) 我们可以编写一个在更新任何列表时被触发的事件接收器.我们必须为eventreceiver指定的listtemplateid使我们的代码特定于一种列表.如果我们想要在所有站点列表上执行事件代码,该怎么办?
我有一些问题,取消注册一些事件接收者形成一个内容类型.内容类型和接收器由我自己部署和注册,因此我不会尝试删除任何MOSS内置或内部事件接收器.
我尝试使用以下代码片段将其归档:
using (SPSite site = new SPSite("http://wssdev06/"))
{
using (SPWeb web = site.RootWeb)
{
// web.AllowUnsafeUpdates = true;
SPContentType type = web.AvailableContentTypes[<ContentTypeName>];
while (type.EventReceivers.Count > 0)
{
type.EventReceivers[0].Delete();
}
type.Update();
// web.AllowUnsafeUpdates = false;
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,命令"type.Update()"抛出一个异常,告诉我无法修改集合.正如您在代码中看到的,我已经尝试了不同的方法来解决这个问题,因为允许不安全的更新或使用提升的权限运行此代码.但我总是得到同样的例外.
那么我做错了什么?
有人在Sharepoint的事件接收器中成功使用"ItemAdding"方法进行验证吗?
我认为它无法正常用于验证目的,因为它似乎:
这是我到目前为止在网上找到的ItemAdding方法:
http://www.sharepoint-tips.com/2006/09/synchronous-add-list-event-itemadding.html
http://www.synergyonline.com/blog/blog-moss/Lists/Posts/Post.aspx?ID=25
无论如何,如果有人成功使用了这种方法,请告诉我如何,因为我认为它只是破碎/遗漏了一些东西!
谢谢!
我使用以下代码在我的文档库中创建一个文件夹.事件被触发并执行到我的代码的最后一行,没有任何问题.但是,文件夹未在我的文档库中创建或列出.
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
string strDashListRoot = "http://win-hmpjltdbh5q:37642";
using (SPSite site = new SPSite(strDashListRoot))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList spl = web.Lists["client_documents"];
spl.Items.Add("", SPFileSystemObjectType.Folder, "Helllworld");
spl.Update();
web.AllowUnsafeUpdates = false;
}
}
}
Run Code Online (Sandbox Code Playgroud)