CRM2011与Silverlight交互的问题

来源:互联网 发布:网易邮箱端口设置 编辑:程序博客网 时间:2024/06/12 02:44
 

1.webresource的路径问题:

Silverlight生成的xap文件在CRM2011里一般有两种浏览方式,一是嵌入到html页面里进行浏览,二是在form表单里嵌入浏览,如果在html中嵌入则需注意路径问题,若xap文件的名称为new_/sl/mysl.xap,而html的文件名为new_/html/myhtml.html,则在html页面中引用xap文件的路径应为:” ../sl/mysl.xap”.

2. 给Silverlight程序传值:

若在html页面传则增加一个” initParams”的参数:

    <param name="initParams" value="id={3A9FA9E8-AFA7-E011-8F5A-000C290D0CA9},type=10013,typename=new_bj,orgname=crm2011,userlcid=2052,orglcid=2052"/>

若在form中直接引用.xap的webresource则勾上”将记录对象类型代码和…..”即可。

 

3.发生” 跨线程访问无效”的异常:

如果在Silverlight中异步调用结束之后,需要更改界面元素的值,则不能直接像a.content=”aaaa”;这样修改,这样会报出” 跨线程访问无效”的错误。

解决方法:

界面首次加载时取得当前的线程,然后在修改界面元素时使用该线程的回调函数来修改即可,代码如下:

public partial class MainPage : UserControl

    {

        SynchronizationContext syn;

        private crm2011Context _context;

        private String _serverUrl;

 

        public MainPage(App app)

        {

            InitializeComponent();

            syn = SynchronizationContext.Current;

            _serverUrl = ServerUtility.GetServerUrl();

            if (!String.IsNullOrEmpty(_serverUrl))

            {

                _context = new crm2011Context(new Uri(String.Format("{0}/xrmservices/2011/organizationdata.svc/", _serverUrl), UriKind.Absolute));

                _context.IgnoreMissingProperties = true;

                string id = app.Host.InitParams["id"];

                try

                {

                    DataServiceQuery<Account> query = (DataServiceQuery<Account>)_context.AccountSet.Where(a => a.AccountId.Equals(new Guid(id)));

                    query.BeginExecute(OnAccountSearchComplete, query);

                }

                catch (Exception ex)

                {

 

                }

            }

        }

        private void getStr(object str)

        {

            label1.Content = str.ToString();

        }

        private void OnAccountSearchComplete(IAsyncResult result)

        {

            try

            {

                //Get the original query back from the result.

                DataServiceQuery<Account> query = result.AsyncState as DataServiceQuery<Account>;

                Account retrievedAccount = new DataServiceCollection<Account>(query

                    .EndExecute(result)).First<Account>();

 

                syn.Post(new SendOrPostCallback(getStr), retrievedAccount.Name);

            }

            catch (Exception ex)

            {

               

            }

        }

    }

syn.Post(new SendOrPostCallback(getStr), retrievedAccount.Name);这句是关键。

 

4.出现”System.Security.SecurityException: 安全性错误”

此错误是由于silverlight的跨域访问的问题,解决方法:新建一个文件,名称为:” clientaccesspolicy.xml”,用记事本打开,将下面代码copy进去:

<?xml version="1.0" encoding="utf-8" ?>

<access-policy>

  <cross-domain-access>

    <policy>

      <allow-from http-request-headers="*">

        <domain uri="*"/>

      </allow-from>

      <grant-to>

        <resource path="/" include-subpaths="true"/>

      </grant-to>

    </policy>

  </cross-domain-access>

</access-policy>

保存后将clientaccesspolicy.xml放在crm2011 web目录的根目录下面,这样silverlight即可跨域访问,就不会报错了。

 AMT 邱正卫(Peter)