0

We are planning to use Silverlight MVVM application in Dynamics 2011 for few custom features. We also want to have consistent looks for whole application for both Dynamics and Silverlight modules. That’s why we are creating web resource to host this Silverlight application inside CRM.

Now problem is we need to create “Save”, “Edit” etc buttons in Ribbon, which in-turn behaves like buttons inside Silverlight module. Following are important questions

  1. Can we create such buttons in Ribbon to access methods inside View Model of Silverlight application hosted using “Web resource”. These methods will also have to access data changes done by user in Silverlight Views.

  2. Is there any other better way to handle such situation

Thanks,

Nilesh

4

1 回答 1

0

最后,我通过功能区按钮单击成功调用了 Silverlight 应用程序的 C# 代码中的函数。

这是最终输出的屏幕截图。

输出

这是 PoC 正在做的事情

  1. 在自定义区域子区域的 CRM 中托管了 Silverlight 应用程序。这又需要两个网络资源
  2. 功能区中添加了自定义按钮
  3. 第三个网络资源是托管 JavaScript 函数
  4. 单击 JavaScript Web 资源中 CRM 功能区上的自定义按钮时,会调用该函数,然后调用 Silverlight 应用程序的 C# 代码中的方法。将字符串输入传递给此方法
  5. C# 方法是将输入字符串转换为大写并返回。
  6. 最后警报以大写字符串显示。

以下是创建 PoC 的详细信息

  1. 在 CRM 中创建新的解决方案
  2. 通过编辑站点地图 XML 为这个 PoC 创建了新的区域和子区域。这是在customizations.xml 中添加的XML。

  3. 在应用程序功能区中添加了自定义按钮。这是功能区的更新 XML

    序列="101">

  4. 创建 Silverlight 应用程序。这是重要的 C# 代码。

注意 System.Windows.Browser.HtmlPage.RegisterScriptableObject("SilverlightCode", this); 和 [ScriptableMember]

public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            System.Windows.Browser.HtmlPage.RegisterScriptableObject("SilverlightCode", this);                                
        }

        // After the Frame navigates, ensure the HyperlinkButton representing the current page is selected
        private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
        {
            foreach (UIElement child in LinksStackPanel.Children)
            {
                HyperlinkButton hb = child as HyperlinkButton;
                if (hb != null && hb.NavigateUri != null)
                {
                    if (hb.NavigateUri.ToString().Equals(e.Uri.ToString()))
                    {
                        VisualStateManager.GoToState(hb, "ActiveLink", true);
                    }
                    else
                    {
                        VisualStateManager.GoToState(hb, "InactiveLink", true);
                    }
                }
            }
        }

        // If an error occurs during navigation, show an error window
        private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            e.Handled = true;
            ChildWindow errorWin = new ErrorWindow(e.Uri);
            errorWin.Show();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(CustomMethod("Silverlight Button Clicked !"));
        }

        //This method will be called from JavaScript on click of Ribbon Button
        //This method needs to be Public
        [ScriptableMember]
        public string CustomMethod(string message = "")
        {
            //MessageBox.Show(message, "Message", MessageBoxButton.OK);
            return message.ToUpper();
        }
    }

SL代码

这是重要的 HTML 代码。

注意<object id="SLFromJS"

<body>
    <form id="form1" runat="server" style="height:100%">
    <div id="silverlightControlHost">
        <object id="SLFromJS" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
  <param name="source" value="ClientBin/RibbonPoC.xap"/>
  <param name="onError" value="onSilverlightError" />
  <param name="background" value="white" />
  <param name="minRuntimeVersion" value="4.0.50401.0" />
  <param name="autoUpgrade" value="true" />
  <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50401.0" style="text-decoration:none">
   <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
  </a>
    </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
    </form>
</body>
  1. 在 CRM 中托管 Silverlight 应用程序。为此,我们需要创建两个 Web 资源——一个用于托管 HTML,第二个用于 XAP。

  2. 又创建了一个 Web 资源来托管 JavaScript 函数。IE 8 中的开发人员工具 (F12) 帮助我找到了我的 Silverlight 对象 (SLFromJS) 在 HTML DOM 中的确切位置。这是 JavaScript –

注意 window.frames['contentIFrame'].document.forms['form1'].SLFromJS;

function CallSilverlightMethod(sender) {
    alert('Inside JS1!');
    var slc = window.frames['contentIFrame'].document.forms['form1'].SLFromJS;
    alert('Inside JS2!');
    if (slc != null) {
        alert('Inside if!');
        alert(slc.Content.SilverlightCode.CustomMethod('Msg From JavaScript'));
        alert('Going out of if!');
    }
    alert('Out of if!');
}

我的 CRM 解决方案现在如下所示

CRM解决方案

  1. 完毕!现在通过打开 HTML Web 资源的链接来测试工作。

感谢我提到的以下博客文章。

http://www.a2zmenu.com/Blogs/Silverlight/Calling-Silverlight-Method-from-JavaScript.aspx

访问 iframe 中的表单

于 2011-08-25T06:45:23.020 回答