2017-07-01 25 views
0

我对这些东西很新,所以任何帮助将不胜感激。另外,我看了很多解决方案,但没有找到具体的解决方案。 所以问题是:如何在c中重用形状#

我试图创建一个程序,从文件读取一些图形矢量形状,绘制它们,然后可以选择其中之一,修改形状并重新保存整个文件。 我在想,建立一个形状的xaml资源文件将是一个开始。 第一个问题:如果我在xaml资源中定义了一个简单的形状(椭圆,矩形,路径),如何在代码中重用它?一个简单的例子会对我有很大帮助。 第二个问题:为了修改形状,我可能必须将所有基本形状转换为路径,才能在点(顶点)上应用代码。我怎么能这样做? 第三个问题:一些形状是几个基本形状相结合的结果。假设我多次使用组合几何体以达到最终形状。但是,如何获得组合几何的路径数据,以便解决第二个问题?

我知道,这很多,而且还有更多,但对于这些我还找不到任何答案,所以请和我一起裸照。 谢谢。

+1

何不你是不是在寻找和开始一些事情并回到你正面临的确切问题?另请检查:[我如何问一个好问题](https://stackoverflow.com/help/how-to-ask) –

回答

1

要“重复使用”形状,您可以将样式定义为App.xaml。例如:

App.xaml

<Application x:Class="WpfApp4.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:WpfApp4" 
      StartupUri="MainWindow.xaml"> 

    <Application.Resources> 

     <Style x:Key="YellowEllipseWithBlackBorder" TargetType="Ellipse"> 

      <Setter Property="Fill" Value="Yellow" /> 
      <Setter Property="Width" Value="100" /> 
      <Setter Property="Height" Value="100" /> 
      <Setter Property="StrokeThickness" Value="5" /> 
      <Setter Property="Stroke" Value="Black" /> 

     </Style> 

    </Application.Resources> 

</Application> 

MainWindow.xaml

<Window x:Class="WpfApp4.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 

    <Grid> 

     <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> 
      <Ellipse Style="{StaticResource YellowEllipseWithBlackBorder}" /> 
      <Ellipse Style="{StaticResource YellowEllipseWithBlackBorder}" /> 
      <Ellipse Style="{StaticResource YellowEllipseWithBlackBorder}" /> 
     </StackPanel> 

    </Grid> 

</Window> 

结果:

Three ellipses with the same style

正如你所看到的,你应该定义一个样式为你塑造和t母鸡从窗口这个语法应用它:<Ellipse Style="{StaticResource YellowEllipseWithBlackBorder}" />


如果您希望以编程方式做同样的,你可以在前面的例子中定义形状的风格一样,然后在窗口的后台代码:

public partial class MainWindow : Window 
{ 
    private StackPanel _stackPanel; 

    public MainWindow() 
    { 
     InitializeComponent(); 

     _stackPanel = new StackPanel() 
     { 
      Orientation = Orientation.Horizontal, 
      HorizontalAlignment = HorizontalAlignment.Center, 
      VerticalAlignment = VerticalAlignment.Center 
     }; 

     Content = _stackPanel; 

     AddEllipse(); 
     AddEllipse(); 
     AddEllipse(); 
    } 

    public void AddEllipse() 
    { 
     var ellipse = new Ellipse() 
     { 
      Style = FindResource("YellowEllipseWithBlackBorder") as Style 
     }; 

     _stackPanel.Children.Add(ellipse); 
    } 
} 

要绘制自定义形状,你可以使用Polygon控制,您可以在其中定义形状的点,例如:

public void AddPolygon() 
    { 
     var polygon = new Polygon() 
     { 
      Fill = new SolidColorBrush(Colors.Red), 
      Stroke = new SolidColorBrush(Colors.Black), 
      StrokeThickness = 2.0 
     }; 

     polygon.Points.Add(new Point(0, 0)); 
     polygon.Points.Add(new Point(10, 0)); 
     polygon.Points.Add(new Point(5, -10)); 

     _stackPanel.Children.Add(polygon); 
    } 

结果:

Polygon


画一些曲线,你可以使用Path控制与BezierSegment,例如:

public void AddPath() 
    { 
     var canvas = new Canvas(); 

     var path = new Path() 
     { 
      Fill = new SolidColorBrush(Colors.Red), 
      Stroke = new SolidColorBrush(Colors.Black), 
      StrokeThickness = 2.0 
     }; 

     var geometry = new PathGeometry(); 
     geometry.Figures.Add(new PathFigure(
      new Point(0, 0), 
      new List<BezierSegment>() 
      { 
       new BezierSegment(
        new Point(0, 0), 
        new Point(100, 0), 
        new Point(50, -100), 
        true) 
      }, 
      false)); 

     path.Data = geometry; 
     canvas.Children.Add(path); 
     _stackPanel.Children.Add(canvas); 
    } 

Path with BezierSegment

+0

谢谢你的例子。但是,我试图从代码中重用椭圆,而不是从xaml窗口中,我不知道是否有可能。除此之外,我想要做的一个例子是将椭圆转换为路径,因此在代码中,我可以移动椭圆的单个点,因此最终会变成不规则形状。 – Calin

相关问题