当前位置:网站首页>win10 uwp 使用 Geometry resources 在 xaml
win10 uwp 使用 Geometry resources 在 xaml
2022-08-04 18:36:00 【林德熙】
经常会遇到在 xaml 使用矢量图,对于 svg 的矢量图,一般都可以拿出来写在 Path 的 Data ,所以可以写为资源,但是写出来的是字符串,如何绑定 Geometry 到字符串资源?
假如在资源写一个图片,看起来就是下面的代码
<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>然后发现使用的是 string ,如果这时创建了一个用户控件,里面写了一个属性,请看代码
public Geometry IconData
{
get { return (Geometry)GetValue(IconDataProperty); }
set { SetValue(IconDataProperty, value); }
}
public static readonly DependencyProperty IconDataProperty =
DependencyProperty.Register(nameof(IconData), typeof(Geometry), typeof(Header), new PropertyMetadata(null);界面直接使用代码
<local:Header x:Name="HeaderPanel" IconData="{StaticResource HomeIconGeometry}" />就会在运行出现无法从string转换,但是如何把用户控件改为 Path ,就可以运行
那么如何在用户控件使用资源的字符串
可以使用绑定,如果无法转换,可以写一个转换
先创建一个转换类
public class GeometryConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value is string str)
{
var geometry = (Geometry) XamlReader.Load(
"<Geometry xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>"
+ str + "</Geometry>");
return geometry;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}然后在使用绑定的地方使用转换
<local:GeometryConvert x:Key="GeometryConvert"></local:GeometryConvert>
<local:Header x:Name="HeaderPanel" IconData="{Binding Source={StaticResource HomeIconGeometry},Converter={StaticResource GeometryConvert}}" />可以看到,这个方法可以显示图片
所以,需要绑定字符串,可以使用这个方法。
有人说,绑定到字符串可以不使用转换,他可以做到,直接使用绑定,但是我暂时没法
边栏推荐
- Scala104-Spark.sql的内置日期时间函数
- 斯坦福:未来的RGB LED可以贴在你的皮肤上
- 基于激励的需求响应计划下弹性微电网的短期可靠性和经济性评估(Matlab代码实现)
- Hezhou Cat1 4G module Air724UG is configured with RNDIS network card or PPP dial-up, and the development board is connected to the Internet through the RNDIS network card (taking the RV1126/1109 devel
- 猜数字游戏
- 开篇-开启全新的.NET现代应用开发体验
- flink-cdc支持并行读取一张mysql表的binlog不?
- 链表的经典入门LeetCode题目
- Flask framework implementations registered encryption, a Flask enterprise class learning 】 【
- 袋鼠云思枢:数驹DTengine,助力企业构建高效的流批一体数据湖计算平台
猜你喜欢

unity中实现ue眼球的渲染

ptables基本语法使用规则

Win10只读文件夹怎么删除

2019 Haidian District Youth Programming Challenge Activity Elementary Group Rematch Test Questions Detailed Answers

【RTOS训练营】关于上课和答疑

EuROC dataset format and related codes

入选爱分析·银行数字化厂商全景报告,网易数帆助力金融数字化场景落地
[Distributed Advanced] Let's fill in those pits in Redis distributed locks.

Babbitt | Metaverse daily must-read: Weibo animation will recruit all kinds of virtual idols around the world and provide support for them...

【注册荣耀开发者】赢【荣耀70】手机
随机推荐
Flink/Scala - Storing data with RedisSink
【RTOS训练营】关于上课和答疑
如何进行自动化测试?
Nintendo won't launch any new hardware until March 2023, report says
Flink / Scala - 使用 RedisSink 存储数据
作业8.3 线程同步互斥机制条件变量
EasyCVR calls the cloud recording API and returns an error and no recording file is generated. What is the reason?
npm配置国内镜像(淘宝镜像)
Scala105-Spark.sql中collect_list用法
22/8/4 记忆化搜索+博弈论
开篇-开启全新的.NET现代应用开发体验
EasyCVR本地接入国标设备映射公网后,本地设备出现无法播放与级联的解决方法
斯坦福:未来的RGB LED可以贴在你的皮肤上
数仓建模面试
敏捷开发项目管理的一些心得
July 31, 2022 Summary of the third week of summer vacation
在表格数据集上训练变分自编码器 (VAE)示例
Enterprise survey correlation analysis case
股票开户广发证券,网上开户安全吗?
工业元宇宙对工业带来的改变