[英]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.