有没有办法在后面的代码中解析 UWP 中的矢量路径几何? [英] Is there a way to parse a vector path geometry in UWP in code behind?

查看:13
本文介绍了有没有办法在后面的代码中解析 UWP 中的矢量路径几何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不同平台上渲染矢量图形时,SVG 和 SVG 路径格式的效果非常好.但目前我的挑战是在通用 Windows 平台上实现这一点.我有一个给定的路径(取自 SVG 文件),例如:

When rendering vector graphics across different platforms, SVG and the SVG path format works really great. But at the moment its my challenge to realize this on the Universal Windows Platform. I've a given path (taken from SVG file) for example:

<代码>中号198.09375 723.80859Ç177.18295 774.80866 156.88493 827.84014 140.58594 884.89844大号140.72461 885.21875大号142.49609 885.21875Ç161.1353 855.01082 143.6824 808.69757 193.58008 822.50977Ç220.7388 819.58475 192.23555 868.86205 206.17383 873.92578Ç203.13499 858.15989 212.17478 843.34361 213.20312 827.64648Ç216.4534 822.98862 226.9422 827.32963 232.86719 826.05078ç239.35348 827.82466 259.48464 823.52777 253.90234 835.38867ç252.16143 848.23286 247.39181 868.36181 258.57812 876.86133ç275.17557 871.67116 293.30624 867.51489 306.24805 855.00195ç309.22224 855.27087 320.1309 878.89646 334.30078 869.88281ç342.81953 864.33463 353.03187 845.67608 358.37891 844.69922ç358.70487 862.9873 381.18689 865.55311 395.26562 864.39258ç403.5087 857.10727 407.91814 844.31119 413.10742 836.13867ç413.72804 849.59522 421.61104 870.22176 438.42969 865.75586ç449.52198 855.46609 450.22121 835.34205 450.98242 820.66016ç440.08469 801.05876 470.99505 805.89669 482.05469 800.25ç476.90476 794.47714 457.46328 802.62763 447.61328 805.58594Ç444.73527 800.59751 438.61005 793.84755 431.14453 800.40625Ç416.99321 808.72215 417.23603 829.81536 405.94141 841.74219Ç400.48764 848.08657 398.31259 865.19549 387.45703 861.5625Ç367.90276 864.11872 352.66459 843.12021 367.92773 827.74219Ç382.01652 798.42739 389.36227 773.69059 391.92773 757.58398Ç383.28497 784.25969 367.28961 818.5148 363.43164 822.48633Ç364.07428814.47709 365.77944 799.37383 369.08398 783.79688ç366.4449 793.18051 363.69922 804.07698 360.89648 816.71875ç361.48939 835.64228 349.1191 850.24112 336.55859 862.86523ç322.97859 878.53876 301.18236 851.31729 318.63086 839.64844ç336.61687 805.09083 357.61323 769.08136 367.24609 728.10938ç333.91868 727.40516 318.03705 810.21063 310.98828 840.47461ç305.31469 859.28359 283.07607 865.19391 266.34961 870.80273ç247.72901 876.83871255.8139 843.70138 257.98438 832.47461 C 266.45336 827.2867 286.39364 829.07792 287.33594 811.068913 4.068913866 263.56198 808.14794 260.3457 819.18164Ç251.10098 831.98222 228.75757 819.36332 214.10156 822.01172Ç221.58584 792.07737 236.75917 758.58129 242.01172 726.59766Ç228.826 757.22131 219.89381 789.50311 210.12695 821.36328Ç195.25141 821.35043 181.74363 813.75195 166.88086 814.64844Ç176.85082 787.46046 194.56222 754.36904 198.09375 723.80859 Z M 369.08398 783.79688Ç384.88883 727.60052 396.70513 727.59038 391.92773757.58398ç395.19032 747.51409 397.4116 738.51802 397.52734 732.64844ç382.96385 734.97676 374.14438 759.94316 369.08398 783.79688 Z M 352.36328 749.70117ç354.77494 749.90766 347.99282 769.96334 320.71484 828.82227ç319.68801 830.81418 318.67223 832.81204 317.5625 834.75977ç306.90155 826.44708 347.59605 749.29301 352.36328 749.70117 Z M 437.77539 799.73438ç438.30605 799.60547 439.62352 800.55657 442.17773803.56641ç441.681 810.22158 436.18342 800.12108 437.77539 799.73438 Z M 281.73633 803.74805ç288.13496 803.44121 281.42291 821.46784 274.14062 824.54883Ç249.75318 832.98346 268.85036 814.16512 275.05859 807.29688Ç278.08583 804.86441 280.25972 803.81885 281.73633 803.74805 Z M 436.71484 807.25781Ç439.20814 807.30595 441.96247 808.89375 445.0332 812.71484Ç451.81999 828.77301 444.47494 846.59531 438.73438 861.81836Ç424.83443 868.53922 415.18535 844.58764 417.39258 834.13086Ç422.22834 826.55898 427.81022 807.08588 436.71484 807.25781 Z M 424.49805 813.01172C 424.74071 812.91442 424.78138 813.46109 424.44141 815.08984 C 418.59702 825.96246 423.44649 834649 834140 831340

(这个例子应该只是你好"这个词,手写有一些错误和漏洞,这代表了我的要求的平均水平.)

(This example should just be the word 'Hello', handwritten with some errors and holes, which represents a good average of my requirements.)

目标是将代码中的这条路径解析为Windows.UI.Xaml.Media.Geometry不是来自XAML,因为我需要使用结果 Windows.UI.Xaml.Shapes.Path UI 元素.由于我已经在其他平台上解决了这个问题,所以我得到了一个简单的WPF解决方案,应该接近UWP,因为它是windows.因此,对于 WPF,我只需使用以下代码来创建 Path 并解析该几何图形:

The goal is to parse this path in code behind into a Windows.UI.Xaml.Media.Geometry, not from XAML, because I need to work with the resulting Windows.UI.Xaml.Shapes.Path UI element. Since I've already solved this problem on other platforms, I've got an easy solution for WPF, which should be close to UWP, because it is windows. So for WPF I simply use the following Code to create a Path and parse that geometry:

new System.Windows.Shapes.Path
{
    Data = System.Windows.Media.Geometry.Parse(myVectorDataStringFromAbove),
    Fill = myColorBrush
};

自从 Microsoft 统一了 API,我的第一个想法是在 UWP 中以同样的方式进行:

Since Microsoft unified the APIs, my first thought was doing it the same way in UWP:

new Windows.UI.Xaml.Shapes.Path
{
    Data = Windows.UI.Xaml.Media.Geometry.Parse(myVectorDataStringFromAbove),
    Fill = myColorBrush
};

唯一的问题是,在 Windows.UI.Xaml.Media.Geometry 中没有像 WPF 那样的 Parse 方法.我什至无法在诸如 Windows.UI.Xaml.Media.PathGeometry 之类的专门课程中找到一个.但据我所知,有可能从 XAML (?) 解析此类路径定义.所以我的问题是:如何以编程方式执行此操作?

The only problem is, that there is no such Parse method at Windows.UI.Xaml.Media.Geometry like in WPF. I cannot even find one at specialized classes such as Windows.UI.Xaml.Media.PathGeometry. But as far as I know, it is possible, to parse such path definitions from XAML (?). So my question is: how can I do this programmatically?

我知道这种解决方案也适用于 UWP:将路径转换为几何形状但是没有更好的方法吗?

I know that this solution kind of works for UWP as well: Convert path to geometric shape But isn't there be a better way?

推荐答案

XamlReader 解析几何数据:

<代码> VAR数据=M 198.09375 723.80859Ç177.18295 774.80866 156.88493 827.84014 140.58594 884.89844大号140.72461 885.21875大号142.49609 885.21875Ç161.1353 855.01082 143.6824 808.69757 193.58008 822.50977Ç220.7388 819.58475 192.23555 868.86205 206.17383 873.92578Ç203.13499 858.15989 212.17478 843.34361 213.20312 827.64648Ç216.4534 822.98862226.9422 827.32963 232.86719 826.05078ç239.35348 827.82466 259.48464 823.52777 253.90234 835.38867ç252.16143 848.23286 247.39181 868.36181 258.57812 876.86133ç275.17557 871.67116 293.30624 867.51489 306.24805 855.00195ç309.22224 855.27087 320.1309 878.89646 334.30078 869.88281ç342.81953 864.33463 353.03187 845.67608 358.37891 844.69922ç358.70487 862.9873 381.18689 865.55311 395.26562 864.39258ç403.5087 857.10727 407.91814844.31119 413.10742 836.13867ç413.72804 849.59522 421.61104 870.22176 438.42969 865.75586ç449.52198 855.46609 450.22121 835.34205 450.98242 820.66016ç440.08469 801.05876 470.99505 805.89669 482.05469800.25Ç476.90476 794.47714 457.46328 802.62763 447.61328 805.58594Ç444.73527 800.59751 438.61005 793.84755 431.14453 800.40625Ç416.99321 808.72215 417.23603 829.81536 405.94141 841.74219Ç400.48764 848.08657 398.31259 865.19549 387.45703 861.5625Ç367.90276 864.11872 352.66459 843.12021 367.92773 827.74219Ç382.01652 798.42739 389.36227 773.69059 391.92773 757.58398Ç383.28497 784.25969 367.28961 818.5148 363.43164 822.48633ç364.07428 814.47709 365.77944 799.37383 369.08398 783.79688ç366.4449 793.18051 363.69922 804.07698 360.89648 816.71875ç361.48939 835.64228 349.1191 850.24112 336.55859 862.86523ç322.97859 878.53876 301.18236 851.31729 318.63086 839.64844ç336.61687 805.09083 357.61323 769.08136 367.24609 728.10938ç333.91868 727.40516 318.03705 810.21063 310.98828 840.47461ç305.31469 859.28359 283.07607 865.19391 266.34961 870.80273ç247.72901 876.83871 255.8139 843.70138 257.98438 832.47461 C 266.45336 827.2867 286.39364 829.835907 929.81874 13.87966934 788.31866 263.56198 808.14794 260.3457 819.18164Ç251.10098 831.98222 228.75757 819.36332 214.10156 822.01172Ç221.58584 792.07737 236.75917 758.58129 242.01172 726.59766Ç228.826 757.22131 219.89381 789.50311 210.12695 821.36328Ç195.25141 821.35043 181.74363 813.75195 166.88086 814.64844Ç176.85082 787.46046 194.56222 754.36904 198.09375 723.80859 Z M 369.08398 783.79688Ç384.88883 727.60052 396.70513 727.59038391.92773 757.58398ç395.19032 747.51409 397.4116 738.51802 397.52734 732.64844ç382.96385 734.97676 374.14438 759.94316 369.08398 783.79688 Z M 352.36328 749.70117ç354.77494 749.90766 347.99282 769.96334 320.71484 828.82227ç319.68801 830.81418 318.67223 832.81204 317.5625 834.75977ç306.90155 826.44708 347.59605 749.29301 352.36328 749.70117 Z M 437.77539 799.73438ç438.30605 799.60547 439.62352 800.55657442.17773 803.56641ç441.681 810.22158 436.18342 800.12108 437.77539 799.73438 Z M 281.73633 803.74805ç288.13496 803.44121 281.42291 821.46784 274.14062 824.54883Ç249.75318 832.98346 268.85036 814.16512 275.05859 807.29688Ç278.08583 804.86441 280.25972 803.81885 281.73633 803.74805 Z M 436.71484 807.25781Ç439.20814 807.30595 441.96247 808.89375 445.0332 812.71484Ç451.81999 828.77301 444.47494 846.59531 438.73438 861.81836Ç424.83443 868.53922 415.18535 844.58764 417.39258 834.13086Ç422.22834 826.55898 427.81022 807.08588 436.71484 807.25781 Z M424.49805 813.01172 C 424.74071 812.91442 424.78138 813.46109 424.44141 815.08984 C 418.59702 .330702 8265.430.49 4359 43596var geometry = (Geometry)XamlReader.Load(<几何 xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>"+ 数据 + "</几何>");路径.数据 = 几何;

这篇关于有没有办法在后面的代码中解析 UWP 中的矢量路径几何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆