单击 CRM 功能区按钮时在 Silverlight 应用程序的 VM 内调用方法

发布于 2024-11-29 09:05:24 字数 424 浏览 1 评论 0 原文

我们计划在 Dynamics 2011 中使用 Silverlight MVVM 应用程序来实现一些自定义功能。我们还希望 Dynamics 和 Silverlight 模块的整个应用程序具有一致的外观。这就是我们创建 Web 资源来在 CRM 内托管此 Silverlight 应用程序的原因。

现在的问题是我们需要在 Ribbon 中创建“保存”、“编辑”等按钮,而这些按钮的行为又类似于 Silverlight 模块中的按钮。以下是重要问题

  1. 我们能否在功能区中创建此类按钮来访问使用“Web 资源”托管的 Silverlight 应用程序的视图模型内的方法。这些方法还必须访问用户在 Silverlight 视图中所做的数据更改。

  2. 有没有其他更好的方法来处理这种情况

谢谢,

Nilesh

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

信仰 2024-12-06 09:05:24

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

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

OutPut

这是 PoC 正在做的

  1. 在自定义区域-子区域的 CRM 中托管有 Silverlight 应用程序。这又需要两个 Web 资源
  2. 在功能区中添加了自定义按钮
  3. 第三个 Web 资源托管 JavaScript 函数
  4. 单击 CRM 功能区上的自定义按钮 JavaScript Web 资源中的函数被调用,该函数又调用 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",
这);和 [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();
        }
    }

SLCode

这是重要的 HTML 代码。

注意

<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. 托管 Silverlight 应用程序。为此,我们需要创建两个 Web 资源 - 一个用于托管 HTML,第二个用于 XAP。

  2. 又创建了一项 Web 资源来托管 JavaScript 函数。 IE 8 中的开发人员工具 (F12) 帮助我在 HTML DOM 中找到 Silverlight 对象 (SLFromJS) 的确切位置。这是 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 解决方案现在如下所示

CRMSolu

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

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

http://www.a2zmenu.com/Blogs/ Silverlight/从 JavaScript.aspx 调用 Silverlight-Method-from-JavaScript.aspx

访问 iframe 中的表单

Finally I’ve successfully called the function inside C# code of Silverlight application from Ribbon button click.

Here is the screenshot of final output.

OutPut

Here is what PoC is doing

  1. There is Silverlight application hosted in CRM on custom area-subarea. This in-turn needs two web resources
  2. There is Custom button added in Ribbon
  3. Third web resource is hosting JavaScript function
  4. On click of Custom Button on CRM Ribbon function in JavaScript web resource is called which in-turn calls the method in C# code of Silverlight application. String input is passed to this method
  5. C# method is converting the input string to upper case and returning it.
  6. Finally alert is displayed with upper case string.

Here are the details to create the PoC

  1. Created new solution in CRM
  2. Created new Area and Sub-Area for this PoC by editing Site Map XML. Here is the XML added in customizations.xml.

  3. Added custom button in Application Ribbon. Here is the updated XML for Ribbon

    Sequence="101">

  4. Created Silverlight Application. Here is important C# code.

Note
System.Windows.Browser.HtmlPage.RegisterScriptableObject("SilverlightCode",
this); and [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();
        }
    }

SLCode

Here is important HTML code.

Note the <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. Hosted Silverlight Application in CRM. For this we need to create two web resources – one to host HTML and second for XAP.

  2. Created one more web resource to host JavaScript function. Developer tools (F12) in IE 8 helped me to find exact location of my Silverlight Object (SLFromJS) in HTML DOM. Here is the JavaScript –

Note 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!');
}

My CRM solution looks like following now

CRMSolu

  1. Done! Now test the work by opening link of HTML web resource.

Thanks to following blog posts which I referred.

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

accessing a form that is in an iframe

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文