at中文_the controller has detected a

at中文_the controller has detected aConstructing Objects at Run Time

大家好,又见面了,我是你们的朋友全栈君。


(1)Constructing Objects at Run Time in Silverlight


Silverlight的内容在你的页面上就像一个对象层次在树的结构中样.这是单个的.一个在这个结构中最顶上的对象是root object,也是Silverlight plug-in的Source指定相应的XAML中的root.这 个root对象通常是一个Canvas对象,因为Canvas能包括像extBlock或MediaElement等其他对象.

动态的为这个结构添加XAML内容,你首先使用CreateFromXaml方法创建XAML片段.这里的要点,XAML片段不能连接Silerlight对象层次.这个意思片段不能重新呈现,可以,你能修改在这个片段中的object属性.对象的方法不能被调用.

这个XAML就是你指定CreateFromXaml参数,必须是单个root(它必须被有很好的XML格式.).如果你试图使用有过个root或其他不是很好格式的XML样的字符串传入所调用的CreateFromXaml方法.这个CreateFromXaml方法调用将失败并且报出一个error.这个XML root不需要指定Silverlight xmlns,只要它最终添加内容时指定Silverlight

xmlns到它的root上.

注意:

为保证你的Silverlight应用程序遭受安全攻击,我们强烈推存不要为不信任的XAML使用CreateFromXAML方法.如不信任的XAML能包括不信任的XAML能包括伪装界面的网站,导致一个欺骗类型的安全威胁.还有XAML包括脚本事件引用.正在添加XAML进入树中是可能就会有不信任的脚本执行.始终要通过验证的来于你的Silverlight应用程序确认信任的source的XAML才能给CreateFromXAML方法使用.

下面图Showilverlight object hierarchy 和这个XAML fragment的关系.

at中文_the controller has detected a

当你在XAML片段中创建一个不连贯对象集合,你能添加它去激活Silverlight对象层次.这个片段既能添加子对象到支持子对象的父对象上也能设置一个对象属性值.当片段是在Silverlight对象层次结构之前,这个对象在XAML片段中就能被重新呈现.下面的图片是Silverlight object hierarchy 和这XAML片段在他们被连接是的情况


at中文_the controller has detected a

动态的添加XAML内容到Silverlight对象层次中必须的他条件:

1:必须已有XAML内容与Silvrlight plug-in相结合;你不能创建CreateFromXaml替换完成的内容数,—你必须必须完整的保护最初的root元素.如果你要替换这个树,你能设置Source,通过注意Source需要的是在URI中已经存在的文件.并且不能是字符串.可以你能像在using inline XAML中样使用Source来发布.并且使用DOM的document.write些一段XAML.看Using Inline XAML.

2:CreateFromXaml方法能被Silerlight plug-in调用 .

3:使用CreateFromXaml方法创建XAML内容只能对一个对象中操作.如果你要从同一XAML中创建对象添加到不同的应用程序区域中,你必须调用CreateFromXaml多次并且为返回值使用不同的变量.(如果这样做,小心命名冲突).

4:Silverlight object将包括新的XAML内容必须封装成一个对象,既是一个子节点也是一个属性值.


(2)Creating a Single XAML Object


下面是怎样添加一个TextBlock进入Canvas对象中

at中文_the controller has detected a
//
 MouseLeftButtonUp event handler for the root Canvas object.

at中文_the controller has detected a

function
 onMouseLeftButtonUp(sender, eventArgs)
at中文_the controller has detected aat中文_the controller has detected a

at中文_the controller has detected a
{

at中文_the controller has detected a    
// Retrieve a reference to the plug-in.
at中文_the controller has detected a
    var plugin = sender.getHost();
at中文_the controller has detected a   
at中文_the controller has detected a    
// Define a XAML fragment and create it.
at中文_the controller has detected a
    var xamlFragment = <TextBlock Canvas.Top=”200″ Text=”Click for more infoat中文_the controller has detected a” />;
at中文_the controller has detected a    textBlock 
= plugin.content.createFromXaml(xamlFragment, false);
at中文_the controller has detected a
at中文_the controller has detected a    
// Add the XAML fragment as a child of the root Canvas object.
at中文_the controller has detected a
    sender.children.add(textBlock);
at中文_the controller has detected a}

如果你要使用x:Name attribute值,你要在你的XAML内容中提供XML namescpace提供,如.在上面的事例中你要使用x:Name attribute 值,你将要向下面一样写:

 

at中文_the controller has detected a
//
 Define a XAML fragment and create it.

at中文_the controller has detected a

    
var
 xamlFragment 
=
 

<TextBlock xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” 

;
at中文_the controller has detected a       xamlFragment 

+=
 

x:Name=”myCanvas” Canvas.Top=”200″ Text=”Click for more info” />

;
at中文_the controller has detected a
at中文_the controller has detected a    textBlock 

=
 plugin.content.createFromXaml(xamlFragment, 
false
);

(3)Creating a LinearGradient Brush Object


你还能创建XAML内容一个对象的属性值.在下面事例中怎样创建一个LinearGradientBrush并且为Ellipse对象它添加Fill属性.

at中文_the controller has detected a
//
 MouseEnter event handler for the Ellipse object.

at中文_the controller has detected a

function
 onMouseEnter(sender, eventArgs)
at中文_the controller has detected aat中文_the controller has detected a

at中文_the controller has detected a
{

at中文_the controller has detected a    
// Set the Fill property of the Ellipse to the dynamically generated LinearGradientBrush.
at中文_the controller has detected a

at中文_the controller has detected a    
try
at中文_the controller has detected aat中文_the controller has detected a    
at中文_the controller has detected a{

at中文_the controller has detected a    sender.fill 
= createLinearGradientBrush(sender.getHost());
at中文_the controller has detected a    }

at中文_the controller has detected a    
catch(error)
at中文_the controller has detected aat中文_the controller has detected a    
at中文_the controller has detected a{

at中文_the controller has detected a        alert(error.message);
at中文_the controller has detected a    }

at中文_the controller has detected a}


at中文_the controller has detected a
at中文_the controller has detected a

function
 createLinearGradientBrush(plugin)
at中文_the controller has detected aat中文_the controller has detected a

at中文_the controller has detected a
{

at中文_the controller has detected a    
// Define a XAML fragment.
at中文_the controller has detected a
    var xamlFragment = <LinearGradientBrush>;
at中文_the controller has detected a       xamlFragment 
+=   <GradientStop Color=”Yellow” Offset=”0.0″ />;
at中文_the controller has detected a       xamlFragment 
+=   <GradientStop Color=”Orange” Offset=”0.5″ />;
at中文_the controller has detected a       xamlFragment 
+=   <GradientStop Color=”Red” Offset=”1.0″ />;
at中文_the controller has detected a       xamlFragment 
+= </LinearGradientBrush>;
at中文_the controller has detected a
at中文_the controller has detected a    
// Create the XAML fragment and return it.
at中文_the controller has detected a
    return plugin.content.createFromXaml(xamlFragment, false);
at中文_the controller has detected a}

在上面的事例中,如果你还可以为更多对象动态的创建LinearGradientBrush,你还能为每一个对象调用createLinearGradientBrush函数,如下面:

at中文_the controller has detected a
function
 initFill(objectArray)
at中文_the controller has detected aat中文_the controller has detected a

at中文_the controller has detected a
{

at中文_the controller has detected a    
// Set the Fill property for each item in the object array.
at中文_the controller has detected a
    for (i = 0; i < objectArray.length; i++)
at中文_the controller has detected aat中文_the controller has detected a    
at中文_the controller has detected a{

at中文_the controller has detected a        objectArray[i].fill 
= createLinearGradientBrush();
at中文_the controller has detected a    }

at中文_the controller has detected a}

(4)Defining Events in XAML Fragments


XAML片段能为内容中定义事件.下面事例Show的是为Rectangle对象中定义MouseEnter和MouseLeave事件.在case中,当鼠标进入Rectangle时边界会增加.并当鼠标离开时边界回减小.

at中文_the controller has detected a
function
 onLoaded(sender, eventArgs)
at中文_the controller has detected aat中文_the controller has detected a

at中文_the controller has detected a
{

at中文_the controller has detected a    
// Retrieve a reference to the plug-in.
at中文_the controller has detected a
    var plugin = sender.getHost();
at中文_the controller has detected a
at中文_the controller has detected a    
// Define a XAML fragment.
at中文_the controller has detected a
    var xamlFragment = <Rectangle Width=”100″ Height=”40″ Fill=”OldLace” ;
at中文_the controller has detected a       xamlFragment 
+= StrokeThickness=”1″ Stroke=”Black” ;
at中文_the controller has detected a       xamlFragment 
+= MouseEnter=”onMouseEnter” MouseLeave=”onMouseLeave” />;
at中文_the controller has detected a
at中文_the controller has detected a    
// Create the XAML fragment.
at中文_the controller has detected a
    var rectangle = plugin.content.createFromXaml(xamlFragment, false);
at中文_the controller has detected a
at中文_the controller has detected a    
// Add the XAML fragment as a child of the root Canvas object.
at中文_the controller has detected a
    sender.children.add(rectangle);
at中文_the controller has detected a}


at中文_the controller has detected a
at中文_the controller has detected a

function
 onMouseEnter(sender, eventArgs)
at中文_the controller has detected aat中文_the controller has detected a

at中文_the controller has detected a
{

at中文_the controller has detected a    
// Increase the stroke on entering.
at中文_the controller has detected a
    sender.StrokeThickness = 2;
at中文_the controller has detected a}


at中文_the controller has detected a
at中文_the controller has detected a

function
 onMouseLeave(sender, eventArgs)
at中文_the controller has detected aat中文_the controller has detected a

at中文_the controller has detected a
{

at中文_the controller has detected a    
// Decrease the stroke on leaving.
at中文_the controller has detected a
    sender.StrokeThickness = 1;
at中文_the controller has detected a}

(5)Creating a Tree of XAML Objects


还允许你创建完整的XAML对象树.还有在你需要时删除这个树.不是删除对象分配.能够动态的添加和删除内容.让你的应用程序能拥有像工具提示样,当你鼠标经过页面某一部分就会显示.

在下面JavaScript事例显示的是怎样一个自定义的工具提示,这个工具提示由Canvas, Rectangle,和TextBlock对象分层次结构组成.当鼠标进入这个Canvas对象边界时就会出现工具提示.离开时就结束提示.

at中文_the controller has detected a
//
 Define global variables.

at中文_the controller has detected a

var
 plugin, toolTip, mainCanvas;
at中文_the controller has detected a
at中文_the controller has detected a

//
 Set global variables for the plug-in and main Canvas objects.

at中文_the controller has detected a

function
 onLoaded(sender, eventArgs)
at中文_the controller has detected aat中文_the controller has detected a

at中文_the controller has detected a
{

at中文_the controller has detected a    plugin 
= sender.getHost();
at中文_the controller has detected a    mainCanvas 
= sender;
at中文_the controller has detected a}


at中文_the controller has detected a
at中文_the controller has detected aat中文_the controller has detected a

function
 onMouseEnter(sender, args) 
at中文_the controller has detected a
{

at中文_the controller has detected a    
// Determine whether the tooltip is created.
at中文_the controller has detected aat中文_the controller has detected a
    if (toolTip == nullat中文_the controller has detected a{

at中文_the controller has detected a
at中文_the controller has detected a        
// Define the XAML fragment for the tooltip.
at中文_the controller has detected a
        var xamlFragment = <Canvas Width=”150″ Height=”30″ Background=”#FFFFE1″>;
at中文_the controller has detected a           xamlFragment 
+=   <Rectangle Width=”150″ Height=”30″ Stroke=”Black” />;
at中文_the controller has detected a           xamlFragment 
+=   <TextBlock Text=”Hello, World” Canvas.Left=”10″ Canvas.Top=”5″ />;
at中文_the controller has detected a           xamlFragment 
+= </Canvas>;
at中文_the controller has detected a
at中文_the controller has detected a        
// Create the XAML fragment for the tooltip.
at中文_the controller has detected a
        toolTip = plugin.content.createFromXaml(xamlFragment, false);
at中文_the controller has detected a
at中文_the controller has detected a        
// Position the tooltip at a relative x/y coordinate value.
at中文_the controller has detected a
        var cursorPosition = args.getPosition(sender);
at中文_the controller has detected a        toolTip[
Canvas.Left= cursorPosition.x + 30;
at中文_the controller has detected a        toolTip[
Canvas.Top= cursorPosition.y + 40;
at中文_the controller has detected a    }

at中文_the controller has detected a
at中文_the controller has detected a    
// Add the tooltip to the Canvas object.
at中文_the controller has detected a
    mainCanvas.children.add(toolTip);
at中文_the controller has detected a}


at中文_the controller has detected a
at中文_the controller has detected a

function
 onMouseLeave(sender, args)
at中文_the controller has detected aat中文_the controller has detected a

at中文_the controller has detected a
{

at中文_the controller has detected a    
// Determine whether the tooltip is created.
at中文_the controller has detected a
    if (toolTip != null)
at中文_the controller has detected aat中文_the controller has detected a    
at中文_the controller has detected a{

at中文_the controller has detected a        
// Remove the tooltip from the Canvas object.
at中文_the controller has detected a
        mainCanvas.children.remove(toolTip);
at中文_the controller has detected a    }

at中文_the controller has detected a}

上面的事例你能追加修改的XAML片段的属性到Silverlight对象层次中.可以你在任意对象中调用方法.注意修改Canvas.Left和Canvas.Top的属性.这些属性呈现对象的位置都要先找回父元素的位置.在这个case中,父元素是Canvas对虾感,并且子元素就是为Canvas对象做提示的.

at中文_the controller has detected a
//
 Position the tooltip at a relative x/y coordinate value.

at中文_the controller has detected a

var
 cursorPosition 
=
 args.getPosition(sender);
at中文_the controller has detected atoolTip[


Canvas.Left


=
 cursorPosition.x 
+
 
30
;
at中文_the controller has detected atoolTip[


Canvas.Top


=
 cursorPosition.y 
+
 
40
;

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107092.html原文链接:https://javaforall.net

(0)
上一篇 2022年4月21日 上午10:40
下一篇 2022年4月21日 上午10:40


相关推荐

  • c# openfiledialog如何使用_其在古文中的用法

    c# openfiledialog如何使用_其在古文中的用法1.OpenFileDialogprivatevoidbtnOpen_Click(objectsender,EventArgse){OpenFileDialogofd=newOpenFileDialog();ofd.InitialDirectory=@"C:\Users\LWP1398\Desktop…

    2022年10月8日
    5
  • nginx 接口转发_nginx后端接口转发到内网

    nginx 接口转发_nginx后端接口转发到内网目前开发多数趋于前后端分离,后端开发人员有的时候懒得搭建前端环境,可是写后端又不便于联调,经常被这个困扰中,本文介绍如何用nginx转发。前提:有一套完整的环境,可以访问整个环境。环境地址,eghttp://wangzhi.com背景:开发人员不想搭建前端环境,可是又不便于联调。postman联调的话,参数拼接比较麻烦。步骤:1、本地项目启动,eg:localhost:80802、配置本地host127.0.0.1wangzhi.com说明:需要把环境地址,转到本地,

    2022年10月9日
    6
  • oracle hint中ordered 和leading原理很好的帖子

    oracle hint中ordered 和leading原理很好的帖子问题 请教 HINT 写法我有一个 SQL 添加如下 hint 目的是指定 hash join 方式 select ordereduse hash a b c d Froma b c dWhere 其中 nbsp nbsp nbsp nbsp a 只与 b 有关联关系 b 只与 c 有关联关系 b 只与 c 有关联关系 c 只与 d 有关联关系 nbsp nbsp 数量级 a 1000 条 nbsp nbsp b 100

    2025年7月4日
    3
  • CEMAPI实战攻略(二)——建立与短信信箱的连接

    CEMAPI实战攻略(二)——建立与短信信箱的连接CEMAPI实战攻略by吴春雷QQ:819543772Email:wuchunlei@163.com二.建立与短信信箱的连接上一部分已经讨论过,如何搭建开发和测试环境,以及如何初始化CEMAPI,再

    2022年7月3日
    27
  • 前端和后端开发的异同点_后端开发需要掌握什么技术

    前端和后端开发的异同点_后端开发需要掌握什么技术昨天有朋友问我你写了这么多年的代码,你到底是前端开发人员还是后端开发人员?我被这个问题给愣住了,问题不在前端和后端,而在于这么多年我还是一个开发人员。但我不在乎这件事情,因为这么多年了,我发现我对写代

    2022年8月1日
    10
  • php mysql 经纬度_mysql,php和js根据经纬度计算距离

    php mysql 经纬度_mysql,php和js根据经纬度计算距离根据经纬度计算距离公式图片来自互联网对上面的公式解释如下:Lung1Lat1表示A点经纬度,Lung2Lat2表示B点经纬度;a=Lat1–Lat2为两点纬度之差b=Lung1-Lung2为两点经度之差;6378.137为地球半径,单位为千米;计算出来的结果单位为千米,若将半径改为米为单位则计算的结果单位为米。计算精度与谷歌地图的距离精度差不多,相差范围在0.2米以下。参数说明…

    2026年2月22日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号