结果发现不支持事件Click="",不支持x:Name,这就只能简单用用了。
大气象
<
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 >
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 >
大气象
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 属性
*/
}
}
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 属性
*/
}
}