如代码所示,我添加了一支圆珠笔,它支持 30 种颜色,但还不够。
我用其他一些方式得到了colorSelected(Color type),这里不讨论。 现在我想单击圆珠笔,使用我的 colorSelected 进行绘制。 如何?谢谢。
<Grid>
<InkToolbar TargetInkCanvas="{x:Bind inkCanvas}" InitialControls="AllExceptPens" VerticalAlignment="Top">
<InkToolbarBallpointPenButton x:Name="ballpointPen" Click="xxx_Click"/>
<InkToolbarCustomToolButton x:Name="toolButtonColorPicker" Click="ToolButton_ColorPicker">
<Image Height="20" Width="20" Source="ms-appx:///Assets/Palette.png"/>
<ToolTipService.ToolTip>
<ToolTip Content="ColorPicker"/>
</ToolTipService.ToolTip>
</InkToolbarCustomToolButton>
</InkToolbar>
<InkCanvas x:Name="inkCanvas" Margin="0,48,0,0"/>
</Grid>
下面的代码似乎不起作用......
private void xxx_Click(object sender, RoutedEventArgs e)
{
if(bUserDefinedColor)
{
InkDrawingAttributes drawingAttributes = inkCanvas.InkPresenter.CopyDefaultDrawingAttributes();
drawingAttributes.Color = colorSelected;
inkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(drawingAttributes);
}
}
顺便把测试项目上传到GitHub https://github.com/hupo376787/Test.git
最佳答案
这是解决您问题的更好方法,无需直接调用 UpdateDefaultDrawingAttributes
。
我要做的是,每当用户从 ColorPicker
中选择一种新颜色并点击确定时,将此颜色添加到调色板
InkToolbarBallpointPenButton
的,然后将 SelectedBrushIndex
设置为新创建的颜色的索引。
这样您就可以完全删除您的 xxx_Click
处理程序,并将 LeftClick
中的内容替换为以下内容
cpx.LeftClick += (ss, ee) =>
{
bUserDefinedColor = true;
colorSelected = cpx.pickerColor;
ballpointPen.Palette.Add(new SolidColorBrush(colorSelected));
ballpointPen.SelectedBrushIndex = ballpointPen.Palette.Count - 1;
};
就是这个了!您将看到笔图标上所选的颜色视觉效果自动反射(reflect)新颜色,从而提供出色的用户体验。
为了进一步增强用户体验,您可能还需要做两件事。
- 缓存添加的颜色,并在应用启动时手动将它们添加回
调色板
,以便用户下次打开应用时,它们仍然可用。 - 不要添加另一个图标来显示
ColorPicker
,而是尝试将其放入InkToolbarBallpointPenButton
的颜色弹出窗口中,以便所有与颜色相关的内容都位于同一位置。此弹出窗口内的控件称为InkToolbarPenConfigurationControl
。您应该能够找到其样式(请参阅下面的路径)并向其添加ColorPicker
。
C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.xxxxx.0\Generic\generic.xaml
希望这有帮助!
关于c# - 如何设置inktoolbar的笔颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45768338/