当前位置:网站首页>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}}" />可以看到,这个方法可以显示图片
所以,需要绑定字符串,可以使用这个方法。
有人说,绑定到字符串可以不使用转换,他可以做到,直接使用绑定,但是我暂时没法
边栏推荐
- 实验室专利书写指南
- 猜数字游戏
- 数仓相关,总结
- 离线同步odps到mysql 中文乱码是因为?mysql已是utf8mb4
- 合宙Cat1 4G模块Air724UG配置RNDIS网卡或PPP拨号,通过RNDIS网卡使开发板上网(以RV1126/1109开发板为例)
- The upgrade of capacity helps the flow of computing power, the acceleration moment of China's digital economy
- 【RTOS训练营】关于上课和答疑
- 网站设计师:Nicepage 4.15 Crack By Xacker
- Flask framework implementations registered encryption, a Flask enterprise class learning 】 【
- 阿里云技术专家秦隆:云上如何进行混沌工程?
猜你喜欢

CAN光纤转换器CAN光端机解决消防火灾报警

智能视频监控平台EasyCVR如何使用接口批量导出iframe地址?

DHCP&OSPF combined experimental demonstration (Huawei routing and switching equipment configuration)

【注册荣耀开发者】赢【荣耀70】手机

Develop those things: How to obtain the traffic statistics of the monitoring site through the EasyCVR platform?

#yyds干货盘点# 面试必刷TOP101:链表相加(二)

巴比特 | 元宇宙每日必读:微博动漫将招募全球各类虚拟偶像并为其提供扶持...

After EasyCVR is locally connected to the national standard device to map the public network, the local device cannot play and cascade the solution

自己经常使用的三种调试:Pycharm、Vscode、pdb调试

curl命令的那些事
随机推荐
实验室专利书写指南
如何让 JS 代码不可断点
使用.NET简单实现一个Redis的高性能克隆版(二)
方法的重写
路由懒加载
22/8/4 记忆化搜索+博弈论
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
DHCP&OSPF组合实验演示(Huawei路由交换设备配置)
BigDecimal 使用注意!!“别踩坑”
【杰神说说】物联大师2.0版本预告
运力升级助力算力流转,中国数字经济的加速时刻
How does EasyCVR call the double-speed playback of device recording through the interface?
PHP代码审计8—SSRF 漏洞
#yyds干货盘点# 面试必刷TOP101:链表相加(二)
火灾报警联网FC18中CAN光端机常见问题解答和使用指导
Alibaba Cloud International Edition uses ROS to build WordPress tutorial
通配符SSL证书不支持多域名吗?
群友求助,一周没有搞定的需求,3分钟就解决了?
EasyCVR调用云端录像API接口返回错误且无录像文件生成,是什么原因?
Scala104 - Built-in datetime functions for Spark.sql