Win10 UWP uses Geometry resources in XAML

Source: Internet
Author: User
Tags creative commons attribution

Original: Win10 UWP uses Geometry resources in XAML

Often encountered in XAML using vector graph, for SVG vector diagram, generally can be taken out to write in the Path of Data, so can be written as a resource, but the writing is a string, how to bind Geometry to string resources?

If you write a picture in a resource, it looks like the following code

<Page.Resources>    <x:String x:Key="HomeIconGeometry">F1 M 24.0033,56.0078L 24.0033,38.0053L 22.0031,40.0056L 19.0027,35.0049L 38.0053,20.0028L 45.0063,25.5299L 45.0063,21.753L 49.0068,21.0029L 49.0068,28.6882L 57.008,35.0049L 54.0075,40.0056L 52.0073,38.0053L 52.0073,56.0078L 24.0033,56.0078 Z M 38.0053,26.9204L 27.0038,36.005L 27.0038,53.0074L 33.0046,53.0074L 33.0046,42.006L 43.006,42.006L 43.006,53.0074L 49.0068,53.0074L 49.0068,36.005L 38.0053,26.9204 Z</x:String></Page.Resources>

Then I found that using a string, if you create a user control, and write a property, see the code

public Geometry IconData{    getreturn (Geometry)GetValue(IconDataProperty); }    setvalue); }}publicstaticreadonly DependencyProperty IconDataProperty =     typeoftypeofnew PropertyMetadata(null);

The interface uses code directly

<local:Header x:Name="HeaderPanel" IconData="{StaticResource HomeIconGeometry}"/>

Can run when the runtime fails to convert from string, but how to change the user control to Path

So how to use a resource string in a user control

You can use bindings, and if you cannot convert, you can write a transform

Create a transformation class first

 Public classgeometryconvert:ivalueconverter{ Public Object Convert(Object value, Type TargetType,ObjectParameterstringLanguage) {if(value  is stringSTR) {varGeometry = (geometry) xamlreader.load ("<geometry xmlns= ' http://schemas.microsoft.com/winfx/2006/xaml/presentation ' >"+ str +"</Geometry>");returnGeometry }return NULL; } Public Object Convertback(Object value, Type TargetType,ObjectParameterstringLanguage) {Throw NewNotImplementedException (); }}

Then use the transform where the bindings are used

<local:GeometryConvert x:Key="GeometryConvert"></local:GeometryConvert><local:Header x:Name="HeaderPanel" IconData="{Binding Source={StaticResource HomeIconGeometry},Converter={StaticResource GeometryConvert}}"/>

As you can see, this method can display pictures

Therefore, you need to bind the string, you can use this method.

Some people say that binding to a string can be done without using a transform, and he can do that directly using bindings, but I'm temporarily unable to

https://stackoverflow.com/a/45142555/6116637


This work is licensed under the Creative Commons Attribution-NonCommercial use-Share 4.0 International license agreement in the same way. Welcome to reprint, use, republish, but be sure to keep the article Attribution Lindesi (including Link: http://blog.csdn.net/lindexi_gd), not for commercial purposes, based on the modified works of this article must be issued with the same license. If you have any questions, please contact me.

Win10 UWP uses Geometry resources in XAML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.