当前位置:网站首页>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}}" />可以看到,这个方法可以显示图片
所以,需要绑定字符串,可以使用这个方法。
有人说,绑定到字符串可以不使用转换,他可以做到,直接使用绑定,但是我暂时没法
边栏推荐
- Global electronics demand slows: Samsung's Vietnam plant significantly reduces capacity
- 开篇-开启全新的.NET现代应用开发体验
- 关于使用腾讯云HiFlow场景连接器每天提醒签到打卡
- Google Earth Engine APP - one-click online viewing of global images from 1984 to this year and loading an image analysis at the same time
- 【AI+医疗】斯坦福大学最新博士论文《深度学习在医学影像理解中的应用》,205页pdf
- 2019年海淀区青少年程序设计挑战活动小学组复赛试题详细答案
- ATF中断处理的设计模型
- EuROC dataset format and related codes
- 基于3D机器视觉的采血试管分拣系统
- 用Excel绘制统计图
猜你喜欢
随机推荐
数仓相关,总结
Google Earth Engine APP - one-click online viewing of global images from 1984 to this year and loading an image analysis at the same time
如何进行自动化测试?【Eolink分享】
The Industrial Metaverse Brings Changes to Industry
阿里云国际版使用ROS搭建WordPress教程
22/8/4 记忆化搜索+博弈论
猜数字游戏
Win10只读文件夹怎么删除
#yyds干货盘点# 面试必刷TOP101:链表相加(二)
【杰神说说】物联大师2.0版本预告
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
July 31, 2022 Summary of the third week of summer vacation
DOM Clobbering的原理及应用
Flask framework implementations registered encryption, a Flask enterprise class learning 】 【
Short-term reliability and economic evaluation of resilient microgrids under incentive-based demand response programs (Matlab code implementation)
如何让 JS 代码不可断点
Kubernetes入门到精通- Operator 模式入门
力扣学习---0804
EuROC 数据集格式及相关代码
Documentary on Security Reinforcement of Network Range Monitoring System (1)—SSL/TLS Encrypted Transmission of Log Data









