0
我们正在利用XamlReader允许用户在我们的应用程序中动态定义UI。使用用户自定义代码的XamlReader动态事件
例如,如果他们想有多个按钮定义网格,可以通过定义XAML字符串来完成。
var xaml = `
<Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Height='600' Background='Gray'>
<Grid.ColumnDefinitions>
<ColumnDefinition Width='2*' />
<ColumnDefinition Width='1*' />
<ColumnDefinition Width='1*' />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height='2*' />
<RowDefinition Height='1*' />
<RowDefinition Height='1*' />
</Grid.RowDefinitions>
<TextBlock Name='FirstName' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Text='TESTING' Grid.Row='0' Grid.Column='0' Height='20' />
<TextBox Name='LastName' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Text='TESTING' Grid.Row='1' Grid.Column='0' Height='20' />
<Button Grid.Column='1'>Button 2</Button>
<Button Grid.Column='2'>Button 3</Button>
<Button Grid.Column='1' Grid.Row='1'>Button 5</Button>
<Button Grid.Column='2' Grid.Row='1'>Button 6</Button>
<Button Grid.Row='2'>Button 7</Button>
<Button Grid.Column='1' Grid.Row='2'>Button 8</Button>
<Button Grid.Column='2' Grid.Row='2'>Button 9</Button>
</Grid>
`;
var control = Windows.UI.Xaml.Markup.XamlReader.Load(xaml) as UIElement;
但是,我们也希望用户能够定义事件。例如,单击按钮1将显示一个消息框。点击按钮2将做其他事情。
我们希望他们能够指定代码本身,类似于XamlReader.Load。
有没有办法做到这一点?