简体   繁体   中英

importing vector image (XAML file) into WPF

EDIT

When using files for programming they need to be in the same file directory as the code so you can reference then. This is why there is normally a resource folder when ever you create a new project. From there you can import it into your project and use it(look at answer bellow on how to do that).

set up

I've created a vector image with a xaml file format. I used Microsoft expression design 4 and follow the guide from this question: Best way to use a vector image in WPF? This was good and allowed me to make my image however when it creates the file type it turned it into window markup file(I'm telling you this just in case this is a problem but this isn't the question). I don't fully understand what The code in the question is doing and they said they could just drag and drop the xaml file into the window, When I tried that method all I get is errors.

problem

Now that I have my image that I want to use and import into my WPF file. Preferably I would like to import it directly into my grid that I have on my windows(This is just the default set up that you get when you open a new WPF) but I also looked at using a image box(I don't know if you can use XAML in image box so I don't know if that's possible). With both attempts I had no success. So I don't know if has something to do with the file and not importing it right or that I need a subroutine to load it into.

What you have there is a ResourceDictonary with a DrawingBrush , which would be imported like this (provided that the file is part of your Visual Studio project and its Build Action is set to Page ):

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="And gate.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

and used as Background of a UI element or Fill of a Rectangle:

<Rectangle Width="100" Height="100" 
           Fill="{StaticResource UnnamedObject0}"/>

Note that a simple Path like

<Window.Resources>
    <Geometry x:Key="AndGate">M0,2 L5,2 A3,3 1 1 1 5,8 L0,8Z</Geometry>
</Window.Resources>

<Path Width="100" Height="100" Stretch="Uniform" Stroke="Black" StrokeThickness="2"
      Data="{StaticResource AndGate}"/>

would generate a similar output.

You may want to take a look at Shapes and Basic Drawing in WPF Overview .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM