silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在WPF 4应用程序中托管Silverlight 4应用程序?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

有一个Silverlight 4应用程序,我想作为Kiosk应用程序加倍.它必须是全屏,触控功能,最重要的是要完全信任.我正在虑使用WPF应用程序来托管Silverlight XAP.我知道 WPF/XNA SilverlightViewport,但似乎这个解决方案不提供WPF和Silverlight之间的通信. 这真的可以节省我很多时间.我不需要维护两个完全相同的应用程序,每次进行更改时我都
我有一个Silverlight 4应用程序,我想作为Kiosk应用程序加倍.它必须是全屏,触控功能,最重要的是要完全信任.我正在虑使用WPF应用程序来托管Silverlight XAP.我知道 WPF/XNA SilverlightViewport,但似乎这个解决方案不提供WPF和Silverlight之间的通信.

这真的可以节省我很多时间.我不需要维护两个完全相同的应用程序,每次进行更改时我都不需要部署到数百个信息亭.

解决方法

我知道这是一个老问题,但我确实有一个解决方案.

我这样做的确如此.我在WPF主机中托管了一个Silverlight应用程序.我使用自托管WCF来提供双工网络tcp服务.要做到这一点,你还必须有一个http托管的clientaccesspolicy.xml,我也可以从同一个服务中自托管.

我希望我能解释得足以让球滚动.我将尽可能包含一些样本,但由于该项目,我无分享所有内容.

首先,一些虑因素:
1)netlight在Silverlight和WPF之间不安全,因为Silverlight(从版本4开始 – 仅测试版本5)无法进行安全连接,因此如果要使用此连接,则必须使用自己的加密.

2)有两种方法可以进行双工WCF,一种是使用http. http方法“轮询”服务以查看是否有任何命令来自客户端的主机.这固有地使它比net tcp慢得多.另一个是net tcp(如前所述).只有http方法支持开箱即用的加密.

3)当您在WPF主机中托管Silverlight应用程序时,当您开始调试时,您将无法调试Silverlight应用程序,因为调试器将无法连接到(如它所见)外部Silverlight应用程序,只有WPF.要解决此问题,您必须在启动WPF主机时“激活”Silverlight应用程序的附件.

现在代码

远程服务接口:

using System.serviceModel;

namespace Remoteservice
{
    [serviceContract(CallBACkContract = typeof(IRemoteCallBACkservicE))]
    public interface IRemoteservice
    {
        [OperationContract]
        void TESTCallBACk();

        // TODO: Add your service operations here
    }

    [serviceContract]
    public interface IRemoteCallBACkservice
    {
        [OperationContract(IsOneWay = truE)]
        void OnTESTCallBACkComplete();

        // TODO: Add your service operations here
    }
}

您的策略侦听器界面:

using System.IO;
using System.serviceModel;
using System.serviceModel.Web;

namespace Remoteservice
{
    [serviceContract]
    public interface IPolicyRetriever
    {
        [OperationContract,WebGet(UriTemplate = "/clientaccesspolicy.xml")]
        Stream GetSilverlightPolicy();

        [OperationContract,WebGet(UriTemplate = "/crossdomain.xml")]
        Stream GetFlashPolicy();
    }
}

实际的服务实现:

using System.Text;

namespace Remoteservice
{
    public class Remoteservice : IRemoteservice,IPolicyRetriever
    {
        IRemoteCallBACkservice client = null;

        public void TESTCallBACk()
        {
            client = OperationContext.Current.GetCallBACkChAnnel<IRemoteCallBACkservice>();
            client.onTESTCallBACkComplete();
        }

        #region Cross Domain Policy Implementation

        private Stream StringToStream(String result)
        {
            WebOperationContext.Current.outgoingResponse.ContentType = "application/xml";
            return new MemoryStream(Encoding.UTF8.GetBytes(result));
        }

        //<grant-to>
        //<resource path="/" include-subpaths="true"/>
        //<socket-resource port="4502-4534" protocol="tcp" />
        //</grant-to>

        Stream IPolicyRetriever.GetSilverlightPolicy()
        {
            String result = @"<?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""/><socket-resource port=""4502-4534"" protocol=""tcp"" /></grant-to></policy></cross-domain-access></access-policy>";
            return StringToStream(result);
        }

        Stream IPolicyRetriever.GetFlashPolicy()
        {
            String result = @"<?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=""*"" /></cross-domain-policy>";
            return StringToStream(result);
        }

        #endregion Cross Domain Policy Implementation
    }
}

上面应该在一个dll项目中的单独的类中,然后由WPF主机引用.

并在控制台应用程序中托管它(您需要将其转换为WPF):

using System;
using System.serviceModel;
using System.serviceModel.description;
using Localservice;
using Remoteservice;

namespace Host
{
    internal class Program
    {
        /// <sumMary>
        /// Main host entry point,this starts our listeners
        /// </sumMary>
        /// <param name="args">The args.</param>
        private static void Main(String[] args)
        {
            // Start our listeners
            StartListeners(80,4504,truE);
        }

        /// <sumMary>
        /// Starts the listeners.
        /// </sumMary>
        private static void StartListeners(int httpPort = 80,int netTcpPort = 4504,bool StartRemoteservice = truE)
        {
            Console.WriteLine("StarTing Policy Listener");

            String policyaddress = "http://" + Environment.Machinename + ":" + httpPort.ToString();
            String locallistener = "net.tcp://" + Environment.Machinename + ":" + NetTcpPort.ToString();

            // Start our policy listener and (if required) our remote http service:
            using (System.serviceModel.serviceHost policyandremoteservicehost = new System.serviceModel.serviceHost(typeof(Remoteservice.RemoteservicE),new Uri(policyaddress)))
            {
                policyandremoteservicehost.AddserviceEndpoint(typeof(IPolicyRetriever),new System.serviceModel.WebhttpBinding(),"").behaviors.Add(new WebhttpBehavior());

                // if we are to start our remote service here too,then add that endpoint in:
                if (StartRemoteservicE)
                {
                    policyandremoteservicehost.AddserviceEndpoint(typeof(IRemoteservicE),new System.serviceModel.PollingDuplexhttpBinding(),"Remoteservice");
                }

                serviceMetadataBehavior psmb = new serviceMetadataBehavior();
                psmb.httpGetEnabled = true;
                psmb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                policyandremoteservicehost.Description.behaviors.Add(psmb);
                policyandremoteservicehost.open();


                Console.WriteLine("WCF Host Running...");
                Console.WriteLine("Press <enter> to shutdown");
                Console.ReadLine();
                Console.WriteLine("Closing connections,please wait");
                policyandremoteservicehost.Close();
                Console.WriteLine("Closed policy and remote services");
            }
        }
    }
}

哪个应该给你一些工作. startremoteservice bool可用于使其成为策略文件侦听器,然后允许您连接到远程服务,而远程服务不必托管策略文件.

请记住,Silverlight只能连接到http / httpS端口,以及4502-4534范围内的TCP端口.包含的clientaccesspolicy.xml非常不受限制.

我希望这对某人有用:).

如果你愿意,请随时问我问题,我会留意一下.

有几点需要注意:您可以从此解决方案中为托管的xap提供服务,因此不需要IIs.您还可以从浏览器中运行Silverlight xap并仍然使用这些服务.

调试答案如下:Debugging Silverlight hosted in WPF

大佬总结

以上是大佬教程为你收集整理的如何在WPF 4应用程序中托管Silverlight 4应用程序?全部内容,希望文章能够帮你解决如何在WPF 4应用程序中托管Silverlight 4应用程序?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。