我不知道我缺少什么設定,在螢屏截圖中,看起來我將 Segments 設定為負 X 軸,但事實并非如此。
.xaml
<Window x:Class="Playground.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Title="Test" Height="220" Width="200" Loaded="Window_Loaded">
<Canvas Width="100" Height="100" Background="Black" Margin="50">
<Path Stroke="Red" StrokeThickness="2" Name="myPath" />
</Canvas>
</Window>
。CS
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var _pathFigure = new PathFigure() { Segments = new PathSegmentCollection() };
var _pathCollection = new PathFigureCollection();
_pathCollection.Add(_pathFigure);
myPath.Data = new PathGeometry() { Figures = _pathCollection };
var pts = new List<Point>()
{
new Point(50,50),
new Point(1,50),
new Point(50,50),
};
_pathFigure.Segments.Clear();
_pathFigure.StartPoint = pts.First();
foreach (var p in pts)
{
var segment = new LineSegment();
segment.Point = new Point(p.X, p.Y);
_pathFigure.Segments.Add(segment);
}
}
截屏:

uj5u.com熱心網友回復:
這是由兩個段之間的斜接引起的。線之間的斜接點是它們之間的交叉點。但是,當它們平行時,就像你的情況一樣,這個交叉點會變成無窮大。WPF 有一定的設定 - StrokeMiterLimit,保證斜接延伸不會超過一定的限制,即 X/2 倍線條筆畫粗細。默認情況下它是 10,所以你的“錯誤”是筆畫粗細的 10/2 倍,即 2,因此 - 10。你可以將它設定為 1,或者使用StrokeLineJoin.Bevel,或者StrokeLineJoin.Round,如果你實際上不需要斜接加入。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489986.html
標籤:wpf
