代码之家  ›  专栏  ›  技术社区  ›  Vincent

如何设置inktoolbar的笔颜色

  •  2
  • Vincent  · 技术社区  · 7 年前

    我用其他方法选择了颜色(颜色类型),这里不讨论。

    <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 回复  |  直到 7 年前
        1
  •  3
  •   Justin XL    7 年前

    这里有一个更好的解决你的问题的方法,不需要打电话 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. 缓存添加的颜色并手动将其添加回 调色板
    2. ,尝试将其放入 所以所有与颜色相关的东西都在同一个地方。位于该弹出窗口中的控件称为 InkToolbarPenConfigurationControl . 您应该能够找到其样式(请参见下面的路径)并添加您的 颜色选择器 对它。

    套件\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0。xxxxx。0 \通用\通用。xaml

    希望这有帮助!