【问题标题】:How to set inktoolbar's pen color如何设置inktoolbar的笔颜色
【发布时间】:2017-08-19 06:14:50
【问题描述】:

如代码所示,我添加了一支圆珠笔,它支持 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);
            }
        }

顺便把测试项目上传到GitHubhttps://github.com/hupo376787/Test.git

【问题讨论】:

  • 您的代码应该可以工作。你怎么称呼它?
  • @JustinXL 在 InkToolbarBallpointPenButton 单击事件中。 IE。当我点击圆珠笔按钮时,它应该使用我的颜色。
  • 它应该可以工作。请在您的点击处理程序中显示完整的源代码。
  • @JustinXL private void xxx_Click(object sender, RoutedEventArgs e) { if(bUserDefinedColor) { InkDrawingAttributes drawingAttributes = inkCanvas.InkPresenter.CopyDefaultDrawingAttributes();绘图属性.Color = colorSelected; inkCanvas.InkPresenter.UpdateDefaultDrawingAttributes(drawingAttributes); } }
  • bUserDefinedColor 是真的

标签: c# uwp uwp-xaml inkcanvas


【解决方案1】:

这里有一个更好的解决您问题的方法,无需直接调用UpdateDefaultDrawingAttributes

我要做的是,每当用户从您的ColorPicker 中选择一种新颜色并点击OK 时,将此颜色添加到InkToolbarBallpointPenButtonPalette,然后设置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 添加到其中。

C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.xxxxx.0\Generic\generic.xaml

希望这会有所帮助!

【讨论】:

  • 哇,好主意。谢谢,贾斯汀。但我仍然想知道为什么 UpdateDefaultDrawingAttributes 不起作用...
  • 在您打开圆珠笔的颜色弹出窗口之前它不会起作用,这是设计使然。使用您的原始代码,尝试从颜色选择器中选择一种颜色,点击“确定”;单击圆珠笔将其选中;再次单击圆珠笔以打开颜色弹出窗口;单击任意位置以关闭弹出窗口;画什么。这一次,您将在画布上看到您指定的颜色。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-30
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
  • 2014-09-13
  • 1970-01-01
  • 2016-05-24
相关资源
最近更新 更多