如何设置转换器以在运行时加载的XAML文件中使用 [英] How to set up a Converter for use within XAML file that it loaded at Runtime

查看:79
本文介绍了如何设置转换器以在运行时加载的XAML文件中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时将一个XAML文件加载到WPF应用程序中。它包含ViewPort3D元素中的3D模型。 XAML文件当前包含作为完整路径字符串的ImageSource值(对于3D上的纹理)。例如:



I load a XAML file at Runtime into a WPF Application. It contains a 3D model within a ViewPort3D element. The XAML File currently contains ImageSource values (for Texture on the 3D) that are full path strings. e.g.:

<ImageBrush ImageSource="C:\Users\Alwyn\Model_1\Diesel_Plan.jpg" />





如果ImageSource值可能是相对路径字符串(因为图像文件始终与3D XAML文件位于同一位置),这将非常有用。这将允许模型及其纹理从一台PC移动到另一台PC,而无需编辑XAML文件。



如果XAML被编译到应用程序本身中可以使用Converter类将硬编码到XAML文件中的相对路径转换为完整路径。但是XAML文件是数据,而不是应用程序本身的一部分。



如果在这种情况下设置转换器?或者是否还有其他方法解决这个问题?



Alwyn



It would be VERY helpful if the ImageSource values could be relative path strings (because the image files are always co-located with the 3D XAML file). This would allow the model and its textures to be moved from one PC to another without having to edit the XAML file.

If the XAML were compiled into the Application itself one could use a Converter class to convert relative paths hard coded into the XAML file to full paths. But the XAML files are 'Data', not a part of the Application itself.

How to set up the Converter in this case? Or is there some other approach to the problem?

Alwyn

推荐答案

我这样做了:

I did this:
m_sharkBitmap = New BitmapImage()
m_sharkBitmap.BeginInit()
m_sharkBitmap.UriSource = New Uri("pack://application:,,,/shark.jpg")
m_sharkBitmap.EndInit()





我猜赌你可以试试在XAML中也是如此。图像作为资源添加到程序中。



Ill bet you could try that in XAML too. The image is added as a resource in the progam.


这篇关于如何设置转换器以在运行时加载的XAML文件中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆