简体   繁体   English

如何在xaml窗口中引用应用程序资源?

[英]How to reference an app resource in a xaml Window?

I've added a reference to an app resource in my View's window. 我在“视图”窗口中添加了对应用程序资源的引用。 But when I declare the resource I get the error: 但是当我声明资源时,我得到了错误:

Error   10  RegexValidationRule is not supported in a Windows Presentation Foundation (WPF) project.

This is how I added the resource to the View: 这是我将资源添加到视图的方式:

  <Window.Resources>
        <validators:RegexValidationRule x:Key="localRegexValidationRule"/>
    </Window.Resources>

Does anyone know how to reference the resource correctly in WPF? 有谁知道如何在WPF中正确引用资源?

I've followed this solution but still get the same error on the resource. 我已遵循此解决方案,但在资源上仍然遇到相同的错误。 This is how I've declared the resource in app.xaml within a resource dictionary: 这是我在资源字典中的app.xaml中声明资源的方式:

<Application x:Class="MongoDBApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:converters="clr-namespace:MongoDBApp.Converters"
             xmlns:validators="clr-namespace:MongoDBApp.Validators"
             StartupUri="pack://application:,,,/Views/MainView.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <converters:BoolToNonVisibilityConverter x:Key="localBoolToNonVisibilityConverter" />
            <converters:BoolToVisibilityConverter x:Key="localBoolToVisibilityConverter" />
            <validators:RegexValidationRule x:Key="localRegexValidationRule" />
        </ResourceDictionary>
    </Application.Resources>
</Application>

Have you added the appropriate using statement in your View file? 您是否在View文件中添加了适当的using语句? It should have the same statement as your App.xaml file: 它应该与您的App.xaml文件具有相同的语句:

         xmlns:validators="clr-namespace:MongoDBApp.Validators"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM