为什么Canvas的Background属性的通用"&的SolidColorBrush QUOT;而不是实际的颜色? [英] Why is the Canvas' Background property a generic "SolidColorBrush" instead of an actual color?
问题描述
我要到龙头响应在画布上,并在画布背景值存储在应用程序设置,这样我可以分配价值的AppBar /命令栏。所以我有这个XAML用户控件:
< StackPanel的方向=横向>
<画布底色=水族WIDTH =20HEIGHT =20VerticalAlignment =中心抽头=CanvasColor_Tapped>< /帆布>
< TextBlock的文本=水族VerticalAlignment =中心抽头=CanvasColor_Tapped>< / TextBlock的>
< / StackPanel的>
..和处理程序是:
私人无效CanvasColor_Tapped(对象发件人,TappedRoutedEventArgs treArgs)
{
如果(发送方是帆布)
{
刷色=((Canvas)的发送者)技术领域;
串brushColorAsStr = colour.ToString();
IAppSettings的appSettings;
如果(App.SaveSettingsLocally)
{
的appSettings =新AppSettingLocal();
}
其他
{
的appSettings =新AppSettingsRoaming();
}
appSettings.SaveSetting(VisitsConsts.APP_BAR_COLOR_SETTING,brushColorAsStr);
}
}
然而brushColorAsStr的价值简直是Windows.UI.XAML.Media.SolidColorBrush(我期待它像品红或Windows.UI.XAML.Media.SolidColorBrush.Magenta)
我怎样才能得到背景的实际值,而不是它的类型?
更新
我不只是如何实现SLaks的回答理解;我试过:
刷brsh =((Canvas)的发送者)技术领域;
// VAR颜色=(的SolidColorBrush)brsh。
// VAR颜色=((的SolidColorBrush)发送方)。颜色;
// VAR颜色=(的SolidColorBrush)((Canvas)的发送者)。颜色。
拓展上SLaks回答:
例如:
VAR帆布=发件人作为画布;
VAR刷= canvas.Background为的SolidColorBrush;
VAR颜色= brush.Color;
请注意,这工作,因为你设置一个单一的,纯色的默认值。这就是为什么它是一个的SolidColorBrush
。你可以将其设置为任何类型的刷,线性渐变,纹理或任何你喜欢的。原因很明显,如果你设置什么,但一个单一的颜色实心画笔上面的code将无法工作。
I want to respond to a tap on a canvas and store the value of the canvas' background in app settings so that I can assign that value to the AppBar / Command Bar. So I have this XAML in a User Control:
<StackPanel Orientation="Horizontal">
<Canvas Background="Aqua" Width="20" Height="20" VerticalAlignment="Center" Tapped="CanvasColor_Tapped"></Canvas>
<TextBlock Text="Aqua" VerticalAlignment="Center" Tapped="CanvasColor_Tapped" ></TextBlock>
</StackPanel>
..and the handler is:
private void CanvasColor_Tapped(object sender, TappedRoutedEventArgs treArgs)
{
if (sender is Canvas)
{
Brush colour = ((Canvas)sender).Background;
String brushColorAsStr = colour.ToString();
IAppSettings appSettings;
if (App.SaveSettingsLocally)
{
appSettings = new AppSettingLocal();
}
else
{
appSettings = new AppSettingsRoaming();
}
appSettings.SaveSetting(VisitsConsts.APP_BAR_COLOR_SETTING, brushColorAsStr);
}
}
Yet the value of brushColorAsStr is simply "Windows.UI.XAML.Media.SolidColorBrush" (I'm expecting it to be something like "Magenta" or "Windows.UI.XAML.Media.SolidColorBrush.Magenta")
How can I get the actual value of the background rather than just its type?
UPDATE
I'm not understanding just how to implement SLaks' answer; I've tried:
Brush brsh = ((Canvas) sender).Background;
//var color = (SolidColorBrush)brsh.
//var color = ((SolidColorBrush) sender).Color;
//var color = (SolidColorBrush).((Canvas)sender).Color;
Expanding on SLaks answer:
Example:
var canvas = sender as Canvas;
var brush = canvas.Background as SolidColorBrush;
var color = brush.Color;
Please note that this works because you set the default of a single, solid color. That's why it's a SolidColorBrush
. You can set it to any kind of brush, linear gradient, textures or whatever you like. For obvious reasons, the code above will not work if you set anything but a single color solid brush.
这篇关于为什么Canvas的Background属性的通用&QUOT;&的SolidColorBrush QUOT;而不是实际的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!