如何设置inktoolbar的笔颜色

时间:2017-08-19 06:14:50

标签: c# uwp uwp-xaml inkcanvas

如图所示,我添加了一个圆珠笔,它支持30种颜色,但还不够。

我使用其他方式获得了colorSelected(颜色类型),这里不再讨论。 现在我想点击ballpointPen,使用我的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

1 个答案:

答案 0 :(得分:2)

这是一个更好的解决方案,无需直接调用UpdateDefaultDrawingAttributes

我要做的是,每当用户从您的ColorPicker中选择新颜色并点击确定时,请将此颜色添加到Palette 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;
};

就是这样!您将在钢笔图标上看到所选的颜色视觉效果自动反映新颜色,从而提供出色的用户体验。

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

  1. 缓存添加的颜色并在应用启动时手动将其添加回Palette,以便下次用户打开应用时,它们仍然可用。
  2. 不要添加其他图标来显示ColorPicker,而是将其放在InkToolbarBallpointPenButton的颜色弹出窗口中,以便所有与颜色相关的内容都在同一个位置。位于此弹出窗口内的控件称为InkToolbarPenConfigurationControl。您应该能够找到它的样式(请参阅下面的路径)并将ColorPicker添加到其中。
  3.   

    C:\ Program Files(x86)\ Windows   试剂盒\ 10 \设计时\ CommonConfiguration \中性\ UAP \ 10.0.xxxxx.0 \通用\ generic.xaml

    希望这有帮助!

相关问题