how to use xaml graphic inside the application resource?

左心房为你撑大大i 提交于 2019-12-02 14:49:28

问题


I have an XAML graphic that converted from vector graphic (SVG) already. The way i want to use it is make XAML file for each image then put it in some folder in the project. Inside the application.resources, make a reference key to use it within the project.

The problem i found it seem what i did in the application.resources is wrong. I used

'ResourceDictionary'

but it throw an exception.

'Set property 'System.Windows.ResourceDictionary.Source' threw an exception.'

Please help me how to do this. Thank you in advance.

I want to use the xaml image in the resources file (in my project is app.xaml) inside the Application.Resources, then the other page can refer this as StaticResource

Additional, I want to use this image as button.

I have the following xaml, and i saved it to a new file as 'button.xaml'. Then I want to add a reference link in the application.resources to use this button in the other page as a button.

<Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Stretch="Uniform"><Canvas Name="Layer_2" Width="609" Height="757" Canvas.Left="0" Canvas.Top="0"><Canvas.RenderTransform><TranslateTransform X="0" Y="0"/></Canvas.RenderTransform><Canvas.Resources/><!--Unknown tag: metadata--><!--Unknown tag: sodipodi:namedview--><Canvas Name="g3"><Canvas Name="g5"><Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path7" Fill="#FFAF946D"><Path.Data><PathGeometry Figures="M256.721 112.144c-0.616-2.478-2.333-8.133-5.919-12.168c-0.219-0.247-0.276-0.906 0.349-1.113    c1.241-0.413 3.354-0.915 4.918-0.236c0.342 0.148 0.907-0.111 0.859-0.481c-0.71-5.537-6.52-10.925-10.595-11.907    c-0.432-0.104-0.818-0.417-0.176-0.739c1.43-0.717 4.007-1.752 5.76-0.974c0.287 0.127 0.604-0.138 0.495-0.465    c-1.686-5.043-6.322-8.537-16.146-11.298c-0.26-0.073-0.657-0.538-0.325-0.952c0.912-1.141 3.307-3.001 8.446-0.909    c0.371 0.151 0.747-0.245 0.506-0.617c-4-6.191-14.502-10.717-28.461-10.982c-0.433-0.008-0.969-0.316-0.164-0.659    c0.859-0.366 2.25-0.775 4.329-0.906c0.435-0.027 0.463-0.576 0.009-0.835c-8.062-4.602-14.098-7.61-36.706-5.506    c-0.247 0.023-0.704-0.207-0.536-0.641c0.431-1.113 1.688-3.258 5.18-4.41c0.495-0.164 0.521-0.694-0.014-0.805    c-9.716-2.006-16.028 1.253-23.719 5.46c-0.362 0.198-0.973 0.163-0.616-0.583c0.513-1.07 1.51-2.776 3.324-4.736    c0.218-0.235 0.018-0.712-0.32-0.702c-8.369 0.237-17.41 3.461-26.267 11.968c-0.279 0.268-0.867 0.435-0.862-0.163    c0.01-1.217 0.165-3.315 1.016-4.829c0.188-0.333-0.073-0.863-0.463-0.792c-5.211 0.953-12.42 8.045-15.353 15.061    c-0.122 0.292-0.686 0.476-0.812 0.249c-0.885-1.591-0.364-4.012 0.07-5.422c0.21-0.683-0.316-0.756-0.7-0.532    c-5.949 3.467-15.856 12.94-15.856 27.901c-5.96 3.221-9.458 8.835-9.458 13.541c-2.212 2.212-13.048-5.441-20.368-1.622    c-6.353 3.315-8.392 16.198 0.757 25.38c-5.604 6.426-10.652 11.246-16.033 16.628c-5.528 5.528-11.396 5.264-15.648 6.016h-0.003    c-2.727 0.48-4.79 1.381-5.671 4.486c-1.712 6.028 0.7 11.97 5.69 17.555c4.821 5.395 12.042 10.454 20.275 14.941    c0.633 3.275-0.172 6.951-2.416 9.194c-3.5 3.501 0 9.951 11.457 9.951c8.656 0 15.575 0.268 20.61-6.518    c11.094 3.667 21.293 5.974 27.763 6.433c5.707 0.4 12.106-0.637 18.624-2.808c2.718 3.695 2.463 9.882-0.761 13.105    c-3.501 3.501 2.106 12.059 13.563 12.059s25.804-0.538 31.586-16.311c2.147-5.851 7.522-9.881 13.417-12.972    c0.264-0.139 1.025 0.136 1.153 0.402c1.796 3.746 5.314 6.608 9.935 6.608c13.691 0 25.46-7.994 28.006-21.545    c2.546-13.548-0.318-27.868 7.638-41.235c3.322-5.579 5.376-16.652 4.348-29.047c-0.023-0.273 0.414-0.775 0.659-0.743    c1.024 0.135 2.056 0.369 2.879 0.767C256.374 112.881 256.915 112.924 256.721 112.144z" FillRule="NonZero"/></Path.Data></Path><Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path9" Stroke="#FFCDAD7F" StrokeLineJoin="Round" StrokeStartLineCap="Round" StrokeEndLineCap="Round"><Path.Data><PathGeometry Figures="M214.991 180.538    c-10.025 0-14.002 5.988-13.804 11.977" FillRule="NonZero"/></Path.Data></Path></Canvas><Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path11" Fill="#FFA48C67"><Path.Data><PathGeometry Figures="M211.39 151.48c-0.91-1.93-2.36-3.55-4-5.07c-2.46-2.28-5.42-4.34-7.94-6.38c-1.25-1.02-2.4-2.03-3.3-3   c-0.44-0.48-0.82-0.95-1.14-1.4c0.24-0.14 0.48-0.27 0.7-0.37c0.28-0.13 0.53-0.19 0.58-0.19c0.57-0.07 1.07-0.22 1.63-0.55   c0.28-0.18 0.57-0.4 0.84-0.77c0.27-0.35 0.51-0.9 0.51-1.51c0-0.11-0.01-0.22-0.02-0.32c-0.23-1.79-0.91-3.79-2.13-5.78   c-0.6-0.97-1.33-1.94-2.21-2.87c0.31 0.1 0.61 0.15 0.89 0.15c0.65 0 1.21-0.17 1.75-0.53c0.27-0.19 0.54-0.43 0.77-0.79   c0.22-0.34 0.39-0.82 0.39-1.33c0-0.15-0.01-0.3-0.04-0.45c-0.52-2.87-2.19-5.41-4.61-7.37c-1.76-1.44-3.92-2.6-6.4-3.46   c0.47 0.08 0.94 0.2 1.41 0.39c0.31 0.13 0.65 0.21 1.01 0.21c0.69 0 1.4-0.31 1.85-0.81c0.46-0.5 0.68-1.15 0.68-1.76   c0-0.51-0.15-1-0.38-1.42c-0.88-1.56-1.96-3.14-3.29-4.61c-1.34-1.47-2.94-2.85-4.86-4.04c-3.2-1.98-7.27-3.39-12.41-3.92   c0.27-0.28 0.49-0.56 0.64-0.88c0.18-0.38 0.3-0.8 0.3-1.3c0-0.31-0.05-0.66-0.2-1.02c-0.15-0.36-0.4-0.73-0.74-1   c-1.65-1.36-3.61-2.47-6-3.21c-2.4-0.74-5.21-1.12-8.66-1.12c-0.67 0-1.37 0.02-2.09 0.05c0.15-0.32 0.23-0.67 0.23-1.03   c0-0.59-0.22-1.17-0.58-1.59c-0.35-0.43-0.8-0.71-1.25-0.88c-2.45-0.89-5.12-1.32-7.82-1.33c-3.58 0.01-7.22 0.78-10.47 2.32v-0.01   c-0.01-0.62-0.16-1.18-0.54-1.74c-0.19-0.28-0.45-0.55-0.81-0.78c-0.36-0.22-0.83-0.37-1.31-0.37c-0.06 0-0.14 0.01-0.23 0.01   c-0.01 0-0.03 0-0.04 0.01h-0.01c-1.87 0.16-3.65 0.56-5.35 1.14c-1.71 0.58-3.34 1.34-4.91 2.26c-1.51 0.88-2.96 1.91-4.39 3.03   c-0.12-0.44-0.34-0.84-0.66-1.17c-0.48-0.5-1.2-0.81-1.93-0.8c-0.16 0-0.33 0.01-0.49 0.04c-2.29 0.38-4.4 1.07-6.31 1.98   c-0.05 0.74-0.08 1.5-0.08 2.28c-5.96 3.22-9.46 8.84-9.46 13.54c-2.21 2.21-13.04-5.44-20.36-1.62c-6.36 3.32-8.4 16.2 0.75 25.38   c-5.6 6.43-10.65 11.25-16.03 16.63c-5.53 5.53-11.4 5.26-15.65 6.01c-1.36 0.24-2.56 0.59-3.53 1.26   c-0.97 0.68-1.7 1.68-2.14 3.23c-1.71 6.03 0.7 11.97 5.69 17.56c4.82 5.39 12.04 10.45 20.27 14.94c0.15 0.77 0.22 1.57 0.21 2.37   c8.87 4.74 18.75 8.78 28.06 11.89c0.47-0.49 0.93-1.04 1.38-1.64c11.1 3.67 21.3 5.98 27.77 6.44c5.7 0.4 12.1-0.64 18.62-2.81   c0.4 0.54 0.73 1.14 0.99 1.77c3.94-1.33 7.9-3.05 11.77-5.1c3.99-2.11 7.89-4.57 11.58-7.31c7.03-5.22 13.28-11.45 17.94-18.28   c0.79 0.1 1.58 0.17 2.37 0.17c5.41-0.01 10.66-2.13 14.88-5.01c2.11-1.45 3.97-3.09 5.46-4.82c1.49-1.74 2.63-3.54 3.26-5.4   c0.28-0.85 0.42-1.71 0.42-2.55C212.22 153.79 211.9 152.58 211.39 151.48z" FillRule="NonZero"/></Path.Data></Path><Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path13" Fill="#FFCDAD7F"><Path.Data><PathGeometry Figures="M192.386 135.029c0 0 2.153-1.79 3.7-1.947c0.516-0.052 1.243-0.4 1.178-0.909   c-0.382-2.963-2.312-6.974-6.734-10.063c-0.624-0.436-0.993-1.443 0.389-1.481s3.116 0.084 4.525 0.722   c0.526 0.238 1.357-0.142 1.259-0.671c-0.858-4.659-5.421-8.482-12.428-10.077c-0.504-0.115-1.355-1.423-1.014-1.844   c1.165-1.436 4.022-1.781 6.569-0.727c0.559 0.231 1-0.333 0.653-0.948c-3.293-5.84-9.229-11.391-22.084-11.786   c-0.484-0.015-1.332-0.766-0.714-1.536c0.463-0.579 1.189-1.173 2.31-1.482c0.492-0.135 0.863-1.031 0.408-1.405   c-4.027-3.31-9.765-4.822-21.92-3.253c-0.586 0.076-1.52-0.41-0.641-1.152c0.878-0.742 2.352-1.566 4.731-1.896   c0.68-0.094 0.83-0.862 0.061-1.144c-6.428-2.357-14.969-1.247-20.443 3.202c-0.424 0.345-1.249 0.502-0.964-0.462   c0.244-0.824 0.721-1.92 1.651-3.148c0.356-0.47 0.043-1.433-0.56-1.38c-6.843 0.597-12.425 4.499-18.018 9.872   c-0.455 0.438-1.387 0.522-1.032-0.653c0.314-1.041 0.923-2.361 2.102-3.643c0.423-0.46 0.037-1.209-0.668-1.09   c-2.49 0.418-4.747 1.224-6.73 2.293c-5.96 3.221-9.458 8.835-9.458 13.541c-2.212 2.212-13.048-5.441-20.368-1.622   c-6.353 3.315-8.392 16.198 0.757 25.38c-5.604 6.426-10.652 11.246-16.033 16.628c-5.528 5.528-11.396 5.264-15.648 6.016h-0.003   c-2.727 0.48-4.79 1.381-5.671 4.486c-1.712 6.028 0.7 11.97 5.69 17.555c4.821 5.395 12.042 10.454 20.275 14.941   c9.264 5.051 19.811 9.376 29.651 12.628c11.094 3.667 21.293 5.974 27.763 6.433c5.707 0.4 12.106-0.637 18.624-2.808   c15.75-5.238 32.207-17.067 41.337-31.07c10.699 2.113 22.761-6.795 25.011-13.541   C212.764 148.396 195.578 142.667 192.386 135.029z M138.976 177.193c-2.183 10.477-6.828 15.627-16.567 18.368   c-1.688 0.476-3.551 0.716-5.538 0.716c-8.825 0-20.323-4.741-32.376-13.352c-1.997-1.427-1.837-2.754-1.675-3.259   c0.208-0.651 0.871-1.348 2.103-1.348c0.43 0 0.908 0.084 1.421 0.251c8.156 2.644 15.996 3.984 23.3 3.984   c10.237 0 19.07-2.634 25.543-7.614c0.429-0.331 0.938-0.505 1.47-0.505c0 0 0 0 0 0c0.718 0 1.414 0.329 1.862 0.88   C138.949 175.843 139.115 176.526 138.976 177.193z" FillRule="NonZero"/></Path.Data></Path><Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path15" Fill="#FFE2CFB4"><Path.Data><PathGeometry Figures="M182.982 170.734C163.284 172.37 149 158.455 149 145c0-13.601-14.678-23.525-30.512-11.484   c-0.331 0.251-1.225 0.458-1.22-0.58c0.016-3.513 0.26-12.719-6.257-17.546c-7.294-5.404-18.862-4.322-32.108 10.33   c-5.604 6.426-10.652 11.246-16.033 16.628c-5.528 5.528-11.396 5.264-15.648 6.016h-0.003c-2.727 0.48-4.79 1.381-5.671 4.486   c-1.712 6.028 0.7 11.97 5.69 17.555c4.821 5.395 12.042 10.454 20.275 14.941c9.264 5.051 19.811 9.376 29.651 12.628   c11.094 3.667 21.293 5.974 27.763 6.433c5.707 0.4 12.106-0.637 18.624-2.808c15.096-5.021 30.842-16.097 40.163-29.338   C184.118 171.688 183.612 170.682 182.982 170.734z M137.73 176.934c-2.07 9.934-6.242 14.75-15.666 17.402   c-9.051 2.548-22.646-2.314-36.829-12.446c-1.972-1.409-1.461-2.815 0.717-2.109c21.796 7.066 39.442 4.303 50.012-3.832   C136.771 175.327 137.925 175.999 137.73 176.934z" FillRule="NonZero"/></Path.Data></Path><Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path17" Fill="#FF77461C"><Path.Data><PathGeometry Figures="M95.331 128.664c0 1.758-1.425 3.183-3.182 3.183c-1.758 0-3.183-1.425-3.183-3.183   c0-1.757 1.425-3.183 3.183-3.183C93.906 125.481 95.331 126.907 95.331 128.664z M131.896 150.029   c-1.757 0-3.182 1.425-3.182 3.182c0 1.758 1.425 3.183 3.182 3.183c1.758 0 3.183-1.425 3.183-3.183   C135.079 151.454 133.653 150.029 131.896 150.029z" FillRule="NonZero"/></Path.Data></Path><Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path19" Fill="#FF77461C"><Path.Data><PathGeometry Figures="M47.222 148.364c10.381-1.566 25.695 7.363 23.789 12.761c-1.907 5.401-8.911 10.982-23.773 9.28   c-4.99-5.585-7.402-11.526-5.69-17.555c0.882-3.105 2.944-4.006 5.671-4.486H47.222z" FillRule="NonZero"/></Path.Data></Path><Ellipse xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Canvas.Left="92.9" Canvas.Top="127" Width="1.2" Height="1.2" Name="circle21" Fill="#FFA58060"/><Ellipse xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Canvas.Left="132.9" Canvas.Top="152.1" Width="1.2" Height="1.2" Name="circle23" Fill="#FFA58060"/></Canvas></Canvas></Viewbox>

回答1:


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.



来源:https://stackoverflow.com/questions/19788073/how-to-use-xaml-graphic-inside-the-application-resource

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