在开发 UWP XAML 应用程序时是否可以从代码修改高级 UI?
Is it possible to modify high level UI from code while developing UWP XAML application?
例如在浏览器中我可以从 JS 操作 DOM。
有没有类似的API?
有可能吗?
更新:
我们在开发 UWP 应用程序时使用 XAML 定义 UI。
我可以更改 XAML 定义的 UI 还是永远固定?
您绝对可以在 运行 时更改 UI。
添加控件、删除控件、更改属性、应用转换或行为、重新设置样式。一切皆有可能。
举个简单的例子。
鉴于此 XAML:
<Page
x:Class="SO33419586.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Name="TheGrid"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock>bye bye</TextBlock>
</Grid>
</Page>
我们可以在后面的代码中做如下操作:
this.TheGrid.Background = new SolidColorBrush(Colors.Chartreuse);
this.TheGrid.Children.Clear();
this.TheGrid.Children.Add(new Button {Content = "click me"});
当 运行 这将创建一个具有绿色(黄绿色)背景并包含按钮但没有文本块的视图。
这里的关键部分是在 XAML 中分配的 x:Name
,因为这使得引用以这种方式创建的控件变得简单。
例如在浏览器中我可以从 JS 操作 DOM。 有没有类似的API? 有可能吗?
更新: 我们在开发 UWP 应用程序时使用 XAML 定义 UI。 我可以更改 XAML 定义的 UI 还是永远固定?
您绝对可以在 运行 时更改 UI。
添加控件、删除控件、更改属性、应用转换或行为、重新设置样式。一切皆有可能。
举个简单的例子。
鉴于此 XAML:
<Page
x:Class="SO33419586.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Name="TheGrid"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBlock>bye bye</TextBlock>
</Grid>
</Page>
我们可以在后面的代码中做如下操作:
this.TheGrid.Background = new SolidColorBrush(Colors.Chartreuse);
this.TheGrid.Children.Clear();
this.TheGrid.Children.Add(new Button {Content = "click me"});
当 运行 这将创建一个具有绿色(黄绿色)背景并包含按钮但没有文本块的视图。
这里的关键部分是在 XAML 中分配的 x:Name
,因为这使得引用以这种方式创建的控件变得简单。