开发者

RecourceDictionaries and custom converters

I have a WPF Application containing MainWindow which just display a MainUserControl and it also contains App.xaml with Merged dictionaries:

<Application.Resources>
    <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources\Styles.xaml"/>
                <ResourceDictionary Source="Resources\DataTemplates.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
</Application.Resources>

Then I decided to use MainUserControl in another project where it should be displayed in the ElementHost. Unfortunately, it doesn’t work because styles and DataTemplates from the resource dictionaries are not found. I don’t know the right way to solve it out but I decided that I may move code ResourceDictionaries code into the MainUserControl’s resources:

<UserControl.Resources>
    <ResourceDictionary>
        <!-- the same code --> 
    </ResourceDictionary>
</UserControl.Resources>

It caused the error with my converters I used in Resource dictionaries - Cannot create unknown type '{clrnamespace:MyApplication.Converters;assembly=MyApplication}LengthToStringConverter . I tried to move converters outside the resource dictionaries by adding one more ResourceDictionary with my converters:

        <ResourceDictionary>
            <con:LengthToStringConverter x:Key="textConverter"/>
            <开发者_如何转开发;con:DateToTextConverter x:Key="dateConverter"/>
        </ResourceDictionary>

It also caused a xaml exception (Provide value on 'System.Windows.StaticResourceExtension' threw an exception.).

So what I’m looking for is the way to solve problem with converters which could not be found in the resource dictionaries or another way to solve problem with using MainUserControl class in another project (it can’t find resources at all, perhaps, there is a way to specify them?).


It seems like you have defined your resources in your main project. That means that you can use the resources only within this project. If you then use your MainUserControl in another project, this project does not know the main project and its resources - that's why the error occurred.

If you want to reuse your resources in another application/project, you should move all your resources to a separate base assembly/project. You can then reference this base project from both applications/projects, so that the resources can be loaded from both applications.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜