XamlReader用法

本来以为Silverlight也可以像Response.Write("<input>");一样输出xaml标签。
结果发现不支持事件Click="",不支持x:Name,这就只能简单用用了。
ExpandedBlockStart.gif 大气象
< UserControl  x:Class ="HCLoad.uc_XamlReader"
    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
="300"  d:DesignWidth ="400" >
    
    
< Grid  x:Name ="LayoutRoot"  Background ="White" >
        
    
</ Grid >
</ UserControl >

 

ExpandedBlockStart.gif 大气象
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Net;
using  System.Windows;
using  System.Windows.Controls;
using  System.Windows.Documents;
using  System.Windows.Input;
using  System.Windows.Media;
using  System.Windows.Media.Animation;
using  System.Windows.Shapes;

using  System.Windows.Markup;
using  System.Text;

namespace  HCLoad
{
    
public   partial   class  uc_XamlReader : UserControl
    {
        
public  uc_XamlReader()
        {
            InitializeComponent();
            Bind();
        }
        
private   void  Bind()
        {
            StringBuilder sb 
=   new  StringBuilder();
            sb.Append(
" <Button Content=\ " ok\ "  Width=\ " 100 \ "  Height=\ " 50 \ "   " );
            
// XamlReader.Load()不接受事件处理程序。不允许设置事件
            
// sb.Append("Click=\"Button_Click\" ");
            sb.Append( " xmlns=\ " http: // schemas.microsoft.com/winfx/2006/xaml/presentation\"></Button>");
            Button btn  =  XamlReader.Load(sb.ToString())  as  Button;
            LayoutRoot.Children.Add(btn);
        }

        
private   void  Button_Click( object  sender, RoutedEventArgs e)
        {
            MessageBox.Show(
" ok " );
        }
        
/*
         *需要注意的是:
         * 1. XamlReader 位于命名空间 System.Windows.Markup 中     
         * 2. 导入的XAML格式字符串最上层只能包含一个对象     
         * 3. 必须和待导入的文件拥有相同的 xmlns      
         * 4. 导入的XAML格式字符中的对象不能拥有 x:name 属性 
         
*/
    }
}

 

转载于:https://www.cnblogs.com/greatverve/archive/2010/07/19/silverlight-xamlreader.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值