c# - 如何设置inktoolbar的笔颜色

标签 c# uwp uwp-xaml inkcanvas

如代码所示,我添加了一支圆珠笔,它支持 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)新颜色,从而提供出色的用户体验。


为了进一步增强用户体验,您可能还需要做两件事。

  1. 缓存添加的颜色,并在应用启动时手动将它们添加回调色板,以便用户下次打开应用时,它们仍然可用。
  2. 不要添加另一个图标来显示 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/

相关文章:

c# - 不再是 "If/Then";字典使用?

c# - 使用单独的值/变量构建字节数组[4]

c# - 如何在 UWP 的 Pivot 中获取 Pivot.ItemTemplate 中的控件?

c# - append 到文本文件(不覆盖!)

c# - DropShadowPanel 和边框圆角半径

c# - 以全屏模式运行的 UWP C# 应用程序在全屏观看视频后切换到窗口模式

C# 在新进程启动时引发事件

c# - 使用 Windows 身份验证连接到 SQL Server

c# - 如何禁用 UWP 应用程序暂停?

c# - 如何在 Visual Studio 2017 中使用 SQLite?