how to use xaml graphic inside the application resource?

旧城冷巷雨未停 提交于 2019-12-02 09:14:31

OK, I'm still not sure about the inside the then the other page, but let me try to help nevertheless.

Lest assume you have in your App.Xaml something like this:

<Application.Resources>

  <Canvas x:Key="TestImage">
    <Path Fill="#ff000000" Data="F1 M 68.240234,42.634277 L 69.554688,6.311035 L 69.414063,7.050293 C 69.665039,6.368164 70.482422,5.000000 71.210938,5.000000 L 79.306641,5.000000 C 80.060547,5.000000 80.898438,6.468750 81.104492,7.052734 L 80.963867,6.326172 L 82.497070,42.649414 L 84.995117,40.043945 L 65.742188,40.043945 L 68.240234,42.634277 Z M 87.493164,42.438477 L 85.959961,6.115234 L 85.944336,5.741211 L 85.819336,5.388672 C 85.113281,3.387207 82.946289,0.000000 79.306641,0.000000 L 71.210938,0.000000 C 67.566406,0.000000 65.401367,3.388672 64.697266,5.391113 L 64.571289,5.750000 L 64.556641,6.130371 L 63.244141,42.453613 L 63.150391,45.043945 L 65.742188,45.043945 L 84.995117,45.043945 L 87.602539,45.043945 L 87.493164,42.438477 Z"/>
  </Canvas>

</Application.Resources>

Please note it's a path, in a canvas (so, it will draw something if you put it in something), and the important bit is the x:Key="TestImage.

Now, lets assume that in a different place/page/control, you want to use that image, that you put in the resource above, all you'll have to do is something like:

<Grid>
    <ContentControl Content="{StaticResource TestImage}" />
</Grid>

And you'll see your image.

You could have the definition in a different xaml file, in a ResourceDictionary, just be sure you merge that dictionary into the resources, so it can be found.

(It'll look like :

<Page.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="your_file_name_here.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Page.Resources>

see here for more details.

Edit: The canvas you added does not have key... you should have that inside a resource dictionary, and it should have a key, so you can refer to it as StaticResource

Another edit ...
You will want to have this code in the window/usercontrol you want to access the code in:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="FolderNameHere/weight.xaml"></ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

(if you're in a usercontrol, change the Window to user control obviously ...) Note there's no / or \ before the folder name (assuming you have your weight in a folder named FolderNameHere, you'll reach it by using the above code.

And yes, the code you put in the end will go in that file, and the file will look like:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas x:Key="TestCanvas" >
    <Path Fill="#ff000000" Data="all the points go here"/>
</Canvas>
</ResourceDictionary>

Only you'll have something else instead of the Canvas.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!