Lai*_*ith 7 c# xaml win-universal-app uwp
在你回答之前,请看这篇文章.
首先得到答案可能会解决以下问题.
这是一个刚刚开始使用Windows 10(Universal Apps)的问题.在Windows 8.1和其他所有XAML技术中,这种技术完美无瑕.这是空白通用应用程序项目中的设置:
1.具有附加属性的静态类
创建一个包含附加属性的类Brush
.把它放在项目的任何地方.
public static class MySettings
{
public static Brush GetAccentBrush(DependencyObject d)
{
return (Brush)d.GetValue(AccentBrushProperty);
}
public static void SetAccentBrush(DependencyObject d, Brush value)
{
d.SetValue(AccentBrushProperty, value);
}
public static readonly DependencyProperty AccentBrushProperty = DependencyProperty.RegisterAttached(
"AccentBrush",
typeof(Brush),
typeof(MySettings),
null
);
}
Run Code Online (Sandbox Code Playgroud)
2.使用附加属性将控件添加到MainPage.xaml
在主页面中,添加一个ContentControl
自定义模板,Grid
其背景颜色设置为重点画笔.重点画笔以风格设置.
<Page x:Class="UniversalTest.MainPage"
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"
xmlns:local="using:UniversalTest"
mc:Ignorable="d">
<Page.Resources>
<Style x:Key="MyControl"
TargetType="ContentControl">
<Setter Property="local:MySettings.AccentBrush"
Value="Green" /> <!-- Setting value here -->
</Style>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ContentControl Style="{StaticResource MyControl}">
<ContentControl.Template>
<ControlTemplate TargetType="ContentControl">
<Grid Background="{TemplateBinding local:MySettings.AccentBrush}"> <!-- Using value here -->
<TextBlock Text="Howdy World!" />
</Grid>
</ControlTemplate>
</ContentControl.Template>
</ContentControl>
</Grid>
</Page>
Run Code Online (Sandbox Code Playgroud)
如果您现在运行该应用程序,它将显示绿色背景.一切正常.但是,如果将值设置为{x:Null}
,则会引发异常.
<Page.Resources>
<Style x:Key="MyControl"
TargetType="ContentControl">
<Setter Property="local:MySettings.AccentBrush"
Value="{x:Null}" /> <!-- Null value here -->
</Style>
</Page.Resources>
Run Code Online (Sandbox Code Playgroud)
有人想拍这个吗?
归档时间: |
|
查看次数: |
479 次 |
最近记录: |