【发布时间】:2016-07-25 02:23:26
【问题描述】:
我正在尝试创建一个 WPF 程序(用于练习),该程序将允许基于三个滑块(红色、绿色和蓝色)选择颜色,根据 rgb 值保存(应用)所述颜色滑块,到一个示例框和一个为以后的框保存。
我的问题:我不知道如何将保存的颜色从“稍后保存”框中转换回可以放回滑块值的值。我可以应用保存的颜色,但这是我想要的保存颜色的值。此代码编译并运行,直到我单击并启动 Click_savedColor,然后程序冻结。
WPF 代码:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Margin="10" VerticalAlignment="Center">
<DockPanel VerticalAlignment="Center" Margin="10">
<Label DockPanel.Dock="Left" FontWeight="Bold">R:</Label>
<TextBox Text="{Binding ElementName=slColorR, Path=Value, UpdateSourceTrigger=PropertyChanged}" DockPanel.Dock="Right" TextAlignment="Right" Width="40" />
<Slider Name="slColorR" Maximum="255" TickPlacement="BottomRight" TickFrequency="1" IsSnapToTickEnabled="True" ValueChanged="ColorSlider_ValueChanged" />
</DockPanel>
<DockPanel VerticalAlignment="Center" Margin="10">
<Label DockPanel.Dock="Left" FontWeight="Bold">G:</Label>
<TextBox Text="{Binding ElementName=slColorG, Path=Value, UpdateSourceTrigger=PropertyChanged}" DockPanel.Dock="Right" TextAlignment="Right" Width="40" />
<Slider Name="slColorG" Maximum="255" TickPlacement="BottomRight" TickFrequency="1" IsSnapToTickEnabled="True" ValueChanged="ColorSlider_ValueChanged" />
</DockPanel>
<DockPanel VerticalAlignment="Center" Margin="10">
<Label DockPanel.Dock="Left" FontWeight="Bold">B:</Label>
<TextBox Text="{Binding ElementName=slColorB, Path=Value, UpdateSourceTrigger=PropertyChanged}" DockPanel.Dock="Right" TextAlignment="Right" Width="40" />
<Slider Name="slColorB" Maximum="255" TickPlacement="BottomRight" TickFrequency="1" IsSnapToTickEnabled="True" ValueChanged="ColorSlider_ValueChanged" />
</DockPanel>
</StackPanel>
<GridSplitter Name="test" HorizontalAlignment="Center"/>
<Button Name="compColor" Grid.Column="2" Click="Click_compColor"/>
<GridSplitter HorizontalAlignment="Right"/>
<Button Name="savedColor" Grid.Column="3" Click="Click_savedColor"/>
</Grid>
C#代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ColorSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
Color color = Color.FromRgb((byte)slColorR.Value, (byte)slColorG.Value, (byte)slColorB.Value);
compColor.Background = new SolidColorBrush(color);
}
void Click_compColor(object sender, RoutedEventArgs e)
{
savedColor.Background = compColor.Background;
}
void Click_savedColor(object sender, RoutedEventArgs e)
{
bool bools = Convert.ToBoolean(savedColor.Background);
byte[] bytes = BitConverter.GetBytes(bools);
slColorR.Value = Convert.ToDouble(bytes[0]);
slColorG.Value = Convert.ToDouble(bytes[1]);
slColorB.Value = Convert.ToDouble(bytes[2]);
compColor.Background = savedColor.Background;
}
}
下面是程序的图像: 第一个框包含颜色滑块;第二个框包含颜色滑块的颜色;第三个盒子包含从第二个盒子保存的颜色。
【问题讨论】:
-
你想做什么将
Brush转换为boolbool bools = Convert.ToBoolean(savedColor.Background);??? -
你要保存的颜色值不是什么画笔;一个画笔可以代表各种有多种颜色的事物。您需要在 ColorSlider_ValueChanged 中获得的 Color.FromRGB() 值。只需将其保存在私有字段中即可。
-
我现在意识到我并没有我想的那么清楚。我使用滑块值 0-255 来表示每个相应的色谱,然后将这些值应用于 Color color = Color.FromRgb((byte)slColorR.Value, (byte)slColorG.Value, (byte)slColorB.Value );然后我应用并保存该颜色。我想加载保存的颜色,然后将其分解为各自的 rgb 值并将这些值放回 rgb 滑块中。我只想保存滑块值,但收到错误,我无法在私有 void ColorSlider_ValueChanged 类之外执行此操作。