c# - 如何在代码 c# 中从 Resource 创建 WPF 图像 SVG?
问题描述
我将图像 svg 转换为 xaml,然后将其保存在字符串资源“打印机”中。从代码隐藏我想创建这个图像/printer.svg 现在printer.xaml/ 并将它作为孩子添加到Canvas。请帮助我如何在 c# 代码中做到最好?谢谢彼得
我做错了什么...
string resVal = Resource1.ResourceManager.GetString("printer");
UserControl u = new UserControl();
u.DataContext = resVal;
Canvas.SetLeft(u, 150);
Canvas.SetTop(u, 150);
front_canvas.Children.Add(u);
我的printer.svg 转换为xaml
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="358" Width="368">
<Viewbox Stretch="Fill" Width="107.9580078125" Height="107.9580078125">
<Canvas Width="407.9580078125" Height="407.9580078125">
<Canvas>
<Canvas>
<Canvas>
<Path Fill="Black" StrokeThickness="1" Data="F1M84.979,307.916L33.153,307.916C14.873,307.916,0,293.04,0,274.756L0.197,149.068C0.197,130.794,15.075,115.917,33.363,115.917L60.479,115.917C64.897,115.917 68.479,119.499 68.479,123.917 68.479,128.335 64.897,131.917 60.479,131.917L33.363,131.917C23.898,131.917,16.197,139.617,16.197,149.081L16,274.768C16,284.218,23.695,291.916,33.153,291.916L84.979,291.916C89.397,291.916 92.979,295.498 92.979,299.916 92.979,304.334 89.397,307.916 84.979,307.916z"/>
..............
解决方案
假设.svg
图像被转换为.xaml
格式,使用用户控件“装饰”并保存到应用程序资源中。
字符串资源名称: UserControl1
字符串资源值:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid Margin="10">
<Path x:Name="shape1" Stretch="Fill" Fill="BlueViolet" StrokeThickness="1"
Data="m 187.906,186.34 c 1.282,7.861 2.571,15.76 3.859,23.646 3.836,23.54 7.629,46.766 11.073,67.894 7.675,47.046 13.162,80.674 13.162,80.674 -0.751,-0.653 -1.489,-1.316 -2.232,-1.967 0.139,0.857 0.275,1.721 0.414,2.586 1.056,0.94 2.117,1.88 3.187,2.822 0.733,-1.112 1.451,-2.23 2.184,-3.338 -0.145,-0.865 -0.296,-1.73 -0.435,-2.593 -0.516,0.776 -1.022,1.555 -1.535,2.338 0,0 -5.566,-33.659 -13.357,-80.744 -3.496,-21.139 -7.338,-44.378 -11.237,-67.929 -1.211,-7.313 -2.422,-14.64 -3.629,-21.935 7.294,1.208 14.619,2.421 21.933,3.631 23.552,3.898 46.79,7.74 67.931,11.237 47.084,7.792 80.743,13.355 80.743,13.355 -0.782,0.514 -1.564,1.018 -2.34,1.536 0.863,0.14 1.729,0.291 2.598,0.436 1.105,-0.731 2.223,-1.452 3.337,-2.184 -0.945,-1.071 -1.885,-2.131 -2.825,-3.188 -0.864,-0.139 -1.727,-0.275 -2.588,-0.413 0.653,0.743 1.317,1.479 1.971,2.232 0,0 -33.628,-5.486 -80.677,-13.164 -21.127,-3.442 -44.352,-7.234 -67.891,-11.074 -7.887,-1.286 -15.787,-2.574 -23.646,-3.858" />
</Grid>
</UserControl>
以下代码演示了如何从资源中加载用户控件并将其添加到主窗口:
// MainWindow.xaml.cs
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Xml;
namespace LoadFromXamlDynamically
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
StringReader stringReader = new StringReader(Properties.Resources.UserControl1);
using (XmlReader xmlReader = XmlReader.Create(stringReader))
{
var control = (UserControl)System.Windows.Markup.XamlReader.Load(xmlReader);
LayoutRoot.Children.Add(control);
}
}
}
}
主窗口.xaml
<Window ...>
<Grid x:Name="LayoutRoot">
</Grid>
</Window>
推荐阅读
- postgresql - 如何回顾以前的同类型记录
- python-3.x - 如何根据数据框值和字典键向数据框添加行
- c++ - pugixml 包含设置为未从 pugixml 配置 cmake 文件中找到的目录
- java - 代理背后的 UnknownHostException
- macros - 如何用宏获取类方法的参数类型?
- ios - 如何在表格视图上显示信息
- java - 如何在 Android 中以编程方式在 Spinner 上设置重叠锚点?
- reactjs - 如何从 SharePoint 列表项中获取数组中的总数
- python - 在 groupby 之后切片组并在子图中绘图
- python - 挂钩烧瓶的文件更改检测的方法?