[英]How to use IValueConverter implementation from another file?
我有IValueConverter
实现 - IconExtractor
。 我这样使用它:
<Image Source="{Binding Converter={StaticResource iconExtractor},
ConverterParameter=c:\\Windows\\System32\\shell32.dll|72}"/>
But for it to work, the IconExtractor
must reside in the code-behind-file of the XAML, eg: my main window is MainWindow.xaml
, so the class IconExtractor
must be in MainWindow.xaml.cs
. 我不能把它放在其他地方吗?
您的IValueConverter
class 可以在您想要的任何位置,包括在另一个组件中。 根据您的使用情况,您需要从MainWindow
class 访问某个资源。 一个简单的方法是将它放在您的 window 资源中。
<MainWindow.Resources>
<my:IconExtractor x:Key="iconExtractor"/>
</MainWindow.Resources>
您还可以使用App.xaml
中的资源,因为可以从项目中所有基于 XAML 的类访问其资源。 使用App.xaml
对我很有吸引力,因为如果您的项目中有多个 window 将使用IconExtractor
,您可以避免重复工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.