当前位置:网站首页>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}}" />可以看到,这个方法可以显示图片
所以,需要绑定字符串,可以使用这个方法。
有人说,绑定到字符串可以不使用转换,他可以做到,直接使用绑定,但是我暂时没法
边栏推荐
- HCIA-R&S自用笔记(22)STP状态与计时器、STP拓扑变化、STP配置及实验
- dotnet core 使用 CoreRT 将程序编译为 Native 程序
- FE01_OneHot-Scala Application
- Regardless of whether you are a public, professional or non-major class, I have been sorting out the learning route for a long time here, and the learning route I have summarized is not yet rolled up
- 使用.NET简单实现一个Redis的高性能克隆版(二)
- Scala104-Spark.sql的内置日期时间函数
- ERC721标准与加密猫
- 【简答题】月薪4k和月薪8k的区别就在这里
- 网站设计师:Nicepage 4.15 Crack By Xacker
- ”元宇宙“必须具备这些特点
猜你喜欢

Win10只读文件夹怎么删除
[Distributed Advanced] Let's fill in those pits in Redis distributed locks.

HCIA-R&S自用笔记(22)STP状态与计时器、STP拓扑变化、STP配置及实验

EasyCVR本地接入国标设备映射公网后,本地设备出现无法播放与级联的解决方法

PHP代码审计10—命令执行漏洞

【AI+医疗】斯坦福大学最新博士论文《深度学习在医学影像理解中的应用》,205页pdf

EasyCVR如何通过接口调用设备录像的倍速回放?

How does EasyCVR call the double-speed playback of device recording through the interface?

企业即时通讯软件有哪些功能?对企业有什么帮助?

EasyCVR调用云端录像API接口返回错误且无录像文件生成,是什么原因?
随机推荐
猜数字游戏
敏捷开发项目管理的一些心得
关于使用腾讯云HiFlow场景连接器每天提醒签到打卡
实验室专利书写指南
运力升级助力算力流转,中国数字经济的加速时刻
Day018 继承
Iptables防火墙基础知识介绍
EuROC dataset format and related codes
dotnet core 使用 CoreRT 将程序编译为 Native 程序
合宙Cat1 4G模块Air724UG配置RNDIS网卡或PPP拨号,通过RNDIS网卡使开发板上网(以RV1126/1109开发板为例)
EasyCVR如何通过接口调用设备录像的倍速回放?
npm配置国内镜像(淘宝镜像)
Activity数据库字段说明
如何进行自动化测试?
工业元宇宙对工业带来的改变
如何让 JS 代码不可断点
图解LeetCode——899. 有序队列(难度:困难)
Develop those things: How to obtain the traffic statistics of the monitoring site through the EasyCVR platform?
Kubernetes入门到精通- Operator 模式入门
vantui 组件 van-field 路由切换时,字体样式混乱问题