65.9K
CodeProject 正在变化。 阅读更多。
Home

DotNetNuke Silverlight 3.0 Hello World

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.93/5 (6投票s)

2009年7月25日

Ms-PL

3分钟阅读

viewsIcon

51773

downloadIcon

261

本教程的目标是引导您完成在 DotNetNuke 中创建一个简单的 Silverlight 模块,该模块通过 Web 服务对当前登录的用户进行身份验证。

引言

您必须从某个地方开始。本教程的目标是引导您完成在 DotNetNuke 中创建一个简单的 Silverlight 模块,该模块通过 Web 服务对当前登录的用户进行身份验证。

本教程所需

设置

您首先需要设置一个 DotNetNuke 开发环境。您将使用此环境来开发您的 DotNetNuke Silverlight 模块。之后,您可以创建一个模块包以安装在另一个 DotNetNuke 生产网站中。

请点击相应的链接来设置您的 DotNetNuke 开发环境

注意:如果您使用 IIS,还需要添加 MIME 类型到您的 Web 服务器。

创建应用程序

使用 Visual Studio 打开 DotNetNuke 网站(**文件** -> **打开网站...**)

在**解决方案资源管理器**中,右键单击**DesktopModules**文件夹,然后创建一个名为**HelloWorld3**的新文件夹

右键单击**HelloWorld3**文件夹,然后选择**添加新项...**

创建一个名为**View.ascx**的**Web 用户控件**。请确保“**在单独文件中放置代码**”复选框已**选中**。

在源视图中打开 View.ascx 页面,并将所有代码替换为以下代码

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="View.ascx.cs" Inherits="HelloWorld3.View" %>
<script type="text/javascript">
    function onSilverlightError(sender, args) {
        var appSource = "";
        if (sender != null && sender != 0) {
            appSource = sender.getHost().Source;
        }
        var errorType = args.ErrorType;
        var iErrorCode = args.ErrorCode;
        if (errorType == "ImageError" || errorType == "MediaError") {
            return;
        }
        var errMsg = "Unhandled Error in Silverlight Application " + appSource + "\n";
        errMsg += "Code: " + iErrorCode + "    \n";
        errMsg += "Category: " + errorType + "       \n";
        errMsg += "Message: " + args.ErrorMessage + "     \n";
        if (errorType == "ParserError") {
            errMsg += "File: " + args.xamlFile + "     \n";
            errMsg += "Line: " + args.lineNumber + "     \n";
            errMsg += "Position: " + args.charPosition + "     \n";
        }
        else if (errorType == "RuntimeError") {
            if (args.lineNumber != 0) {
                errMsg += "Line: " + args.lineNumber + "     \n";
                errMsg += "Position: " + args.charPosition + "     \n";
            }
            errMsg += "MethodName: " + args.methodName + "     \n";
        }
        throw new Error(errMsg);
    }
</script>
<asp:Panel ID="silverlightControlHost" align="center" runat="server" 
    HorizontalAlign="Left">
    <object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
        style="height: 50px; width: 500px">
        <param name="source" value="<%=SilverlightApplication %>" />
        <param name="onError" value="onSilverlightError" />
        <param name="background" value="Transparent" /> 
        <param name="windowless" value="true" /> 
        <param name="minRuntimeVersion" value="3.0.40624.0" />
        <param name="autoUpgrade" value="true" />
        <param name="InitParams" value="<%=SilverlightInitParams %>" />
        <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration: none">
            <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"
                style="border-style: none" />
        </a>
    </object>    <iframe id="_sl_historyFrame" style="visibility: hidden; height: 0px; width: 0px;
        border: 0px"></iframe>
</asp:Panel>

在源模式下打开**View.ascx.cs**文件,并将所有代码替换为以下代码

using System;
using DotNetNuke.Entities.Modules;
namespace HelloWorld3
{
    public partial class View : PortalModuleBase
    {
        public string SilverlightApplication { get; set; }
        public string SilverlightInitParams { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            // Register Silverlight.js file
            Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "SilverlightJS",
                (this.TemplateSourceDirectory + "/Silverlight.js"));

            // Set the Web Service URL
            string strWebServiceURL = String.Format(@"http://{0}/{1}", this.PortalAlias.HTTPAlias,
                "/DesktopModules/HelloWorld3/WebService.asmx");        
            
            // Set the path to the .xap file
            SilverlightApplication = String.Format("{0}{1}", TemplateSourceDirectory,
                "/ClientBin/HelloWorld3.xap");
            // Pass the Initialization Parameters to the Silverlight Control
            SilverlightInitParams = string.Format("WebServiceURL={0}", strWebServiceURL);
        }
    }
}

保存页面

下载此文件:Silverlight.js,将其保存到您的本地硬盘驱动器,然后将其放在**DesktopModules/HelloWorld3**目录中

右键单击**HelloWorld3**文件夹,然后选择**添加新项...**

添加一个名为**Webservice.asmx**的**Web 服务**。请确保“**在单独文件中放置代码**”复选框已**取消选中**。

在源视图中打开 Webservice.asmx 文件,并将所有代码替换为以下代码

<%@ WebService Language="C#" Class="HelloWorld3.WebService" %>
using System.Web.Services;
using DotNetNuke.Entities.Users;
namespace HelloWorld3
{
    [WebService(Namespace = "http://ADefWebserver.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class WebService : System.Web.Services.WebService
    {
        #region GetUsername()
        [WebMethod]
        public string GetUsername()
        {
            string strUsername = "World!";
            // Get the current user 
            UserInfo objUserInfo = UserController.GetCurrentUserInfo();
            // If the user is not -1 they are logged in
            if (objUserInfo.UserID > -1)
            {
                strUsername = objUserInfo.DisplayName;
            }
            return strUsername;
        }
        #endregion
    }
}

创建 Silverlight 应用程序

从菜单中,选择**文件**,然后**添加**,然后**新建项目...**

创建一个名为“**HelloWorld3**”的 Silverlight 应用程序

在**添加 Silverlight 应用程序**框中,将项目添加到现有网站。

项目将添加到**解决方案资源管理器**中

右键单击(Silverlight 项目中的)**引用**文件夹,然后选择**添加服务引用...**

单击**发现**按钮以查找您在上一阶段创建的 Web 服务。

输入**HelloWorld**作为命名空间

单击 **确定** 按钮

打开**App.xaml.cs**文件,并将**Application_Startup**方法更改为以下代码

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            this.RootVisual = new Page(e.InitParams["WebServiceURL"]); 
        }

打开**Page.xaml**文件,并将所有代码替换为以下代码

<UserControl x:Class="HelloWorld3.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="50" d:DesignWidth="400">
    <Canvas x:Name="LayoutRoot">
        <TextBlock Canvas.Left="8" Canvas.Top="8" FontSize="26.667" Text="Hello:" TextWrapping="Wrap"/>
        <TextBlock Canvas.Left="91" Canvas.Top="8" FontSize="26.667" x:Name="UserName" TextWrapping="Wrap"/>
    </Canvas>
</UserControl>

打开**Page.xaml.cs**文件,并将所有代码替换为以下代码

 using System;
using System.Windows.Controls;
using HelloWorld3.HelloWorld;
using System.ServiceModel;
namespace HelloWorld3
{
    public partial class Page : UserControl
    {
        private string WebServiceURL;
        public Page(string parmWebServiceURL)
        {
            InitializeComponent();
            // Set the web service URL global values
            WebServiceURL = parmWebServiceURL;
            CallWebService();
        }
        #region CallWebService
        private void CallWebService()
        {
            WebServiceSoapClient objWebServiceSoapClient = new WebServiceSoapClient();
            EndpointAddress MyEndpointAddress = new EndpointAddress(WebServiceURL);
            objWebServiceSoapClient.Endpoint.Address = MyEndpointAddress;
            objWebServiceSoapClient.GetUsernameCompleted +=
                new EventHandler<GetUsernameCompletedEventArgs>(objWebServiceSoapClient_GetUsernameCompleted);
            objWebServiceSoapClient.GetUsernameAsync();
        }
        void objWebServiceSoapClient_GetUsernameCompleted(object sender, GetUsernameCompletedEventArgs e)
        {
            UserName.Text = e.Result;
        }
        #endregion
    }
}



在**解决方案资源管理器**中右键单击 Silverlight 项目,然后选择**生成**

**HelloWorld3.xap**文件将生成到 DotNetNuke 网站中的一个名为**ClientBin**的目录中

在**解决方案资源管理器**中,将**ClientBin**文件夹拖动到**DesktopModules/HelloWorld3**目录下方

配置模块

在您的**Web 浏览器**中,以**主机**帐户登录您的 DotNetNuke 网站。在**主机**菜单中,选择**模块定义**

在**模块定义**页面的底部,选择**创建新模块**

输入**HelloWorld3**作为**名称**和**友好名称**,将**版本**设置为**01 00 00**,然后单击**下一步**按钮

在**模块特定详细信息**页面上,单击**下一步**

在**所有者详细信息**页面上,单击**下一步**

模块将显示在**模块定义**页面上。单击**HelloWorld3**条目旁边的铅笔图标进行编辑

单击**添加定义**按钮

输入**HelloWorld3**作为**友好名称**,然后单击**创建定义**按钮

单击**添加模块控件**按钮

输入“**Hello World**”作为**标题**,选择**DesktopModules/HelloWorld3/View.ascx**作为**源**,然后单击**更新**

导航到您 DotNetNuke 网站中的某个页面,并将**HelloWorld3**模块放置在该页面上

模块将显示。

© . All rights reserved.