Pet*_*vov 9 silverlight facebook cross-domain facebook-c#-sdk
我有一个位于Facebook照片服务器上的crossdomain.xml的问题.第一个问题出现在Silverlight请求clientaccesspolicy.xml - Facebook服务器返回403 - Access Denied,这很好,因为他们也在他们的服务器上部署了crossdomain.xml.然后Silverlight请求crossdomain.xml,它得到的响应就是这样:
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain- policy.dtd">
<cross-domain-policy>
<allow-access-from domain="*" secure="false" to-ports="*" />
<site-control permitted-cross-domain-policies="master-only" />
</cross-domain-policy>
Run Code Online (Sandbox Code Playgroud)
然后我用这个玩了一段时间,将crossdomain.xml部署到我自己的服务器上并得到了相同的结果 - 一个安全例外.然后我开始解决问题并得出结论,如果我只删除to-ports ="*"属性,一切都会按预期工作?有没有人知道如何克服这个问题,有没有人遇到过同样的问题,或者是我做错了什么?
我在尝试以编程方式从 Facebook 检索图像时遇到了同样的问题。奇怪的是,如果您将 Silverlight 图像控件指向 Facebook 图像 url,则图像将被检索并显示,不会出现错误。这让我开始思考,我想出了一个可行的解决方法,它似乎始终适合我的情况。我希望你也觉得它很有用。
var uri = new Uri("http://graph.facebook.com/mglace/picture/", UriKind.Absolute);
var bmp = new BitmapImage();
bmp.ImageOpened += (sender, e) => { /* Do something here with the sender (which is the BitmapImage) */ };
bmp.CreateOptions = BitmapCreateOptions.None;
bmp.UriSource = uri;
Run Code Online (Sandbox Code Playgroud)
创建一个BitmapImage
对象,为该ImageOpened
事件设置一个事件处理程序并将CreateOptions
属性设置为BitmapCreateOptions.None
。最后,将 设为UriSource
您要检索的 Facebook 图片。图像会立即下载,因为我们将 设为CreateOptions
(None
默认值为DelayedCreation
)。然后,您可以在事件处理程序中执行您想要的任何操作ImageOpened
。
我想将此逻辑封装在我的服务层中,并加强错误处理等,因此我将其包装在 Reactive Extensions observable 中,以使其更易于使用。这是我的最终代码片段:
public IObservable<BitmapImage> GetProfilePhoto(string profileId)
{
return Observable.Create<BitmapImage>(
observer =>
{
// This handler handles a successful fetch
EventHandler<RoutedEventArgs> openedHandler =
(sender, args) =>
{
try
{
observer.OnNext(sender as BitmapImage);
observer.OnCompleted();
}
catch (Exception ex)
{
observer.OnError(ex);
}
};
// This handler handle a failure
EventHandler<ExceptionRoutedEventArgs> failedHandler =
(sender, args) => observer.OnError(args.ErrorException);
var url = string.Format("http://graph.facebook.com/{0}/picture/", profileId);
var uri = new Uri(url, UriKind.Absolute);
BitmapImage bmp = null;
try
{
Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
bmp = new BitmapImage();
bmp.ImageOpened += openedHandler;
bmp.ImageFailed += failedHandler;
bmp.CreateOptions = BitmapCreateOptions.None;
bmp.UriSource = uri;
});
}
catch (Exception ex)
{
observer.OnError(ex);
}
return () =>
{
// Cleanup the event handlers
if (bmp != null)
{
bmp.ImageOpened -= openedHandler;
bmp.ImageFailed -= failedHandler;
}
};
});
}
Run Code Online (Sandbox Code Playgroud)
及用法:
GetProfilePhoto("mglace")
.Subscribe(image => { /* Do something with the image in here*/ },
error => { /* Handle any errors in here */ },
() => { /* Finalization code goes here */ });
Run Code Online (Sandbox Code Playgroud)
我希望有人觉得这很有用。
归档时间: |
|
查看次数: |
909 次 |
最近记录: |