【问题标题】:The name 'foo' is not exist in namespace 'http://foo....'名称空间“http://foo....”中不存在名称“foo”
【发布时间】:2013-12-19 08:17:00
【问题描述】:

我有一个 WPF 解决方案,这个解决方案包含 3 个项目: 1-一个内部有多个 WPF 用户控件的项目 2-另一个项目里面有几个 WPF 用户控件 3-A 项目,其中包含上述 2 个 WPF 项目的资源。

如您所知,如果您有这样的视图的通用设置 - 使用相同的字体系列。 - 使用相同的字体大小 - 使用相同的字体粗细 - 对所有用户控件等使用相同的 BackroundBrush。您需要在所有用户控件标签中声明此设置器,如下所示:

<UserControl ....
    FontFamily="{DynamicResource MyFontFamily}" 
    FontSize="{DynamicResource MyFontSize}" 
    FontWeight="{DynamicResource MyFontWeight}" 
    Background="{DynamicResource MyAppBgBrush2}"
    Width="250" d:DesignHeight="350">
    <Grid/>......

但我不想在我的所有用户控件中编写相同的设置器。出于这个原因,我决定将此属性设置移动到一个新的 c# 文件中并在资源项目中找到它。

using System.Windows.Controls;

namespace Resources
{
    public class PageBase : UserControl 
    {
        public PageBase()
        {
            SetResourceReference(FontFamilyProperty, "MyFontFamily");
            SetResourceReference(FontSizeProperty, "MyFontSize");
            SetResourceReference(FontWeightProperty, "MyFontWeight");
            SetResourceReference(BackgroundProperty, "MyAppBgBrush2");
        }
    }
}

所以,在我的资源项目中,我像这样编辑了 AssemlyInfo.cs 文件:

[assembly: System.Windows.Markup.XmlnsDefinition("http://schemas.sat.com/winfx/2010/xaml/internalresources", "Resources")]

此编辑使我能够声明/创建如下用户控件:

<internalresources:PageBase
            xmlns:internalresources="http://schemas.sat.com/winfx/2010/xaml/internalresources">
      <Grid>DoWhatEver<Grid/>
<internalresources:PageBase/>

从现在开始,我不必创建标签开头的用户控件视图 &lt;UserControl....,我可以从&lt;internalresources:PageBase开始......

我的问题是,VisualStudio 2010 可以向我展示我的所有用户控件的设计但 Expression blend 不能。有趣的是,在 VS 和 Blend 中,我的项目编译没有任何错误但是当我尝试在 blend 中打开我的视图时,它说:

-命名空间“PageBase”在命名空间“http://schemas.sat.com/winfx/2010/xaml/internalresources”中不存在

P.S:引用已正确添加到我的项目中,并且我的项目适合使用 blend 打开。

【问题讨论】:

    标签: c# wpf visual-studio-2010 xaml expression-blend


    【解决方案1】:

    当您向其添加新功能或属性时,继承您自己的 UserControl 是有意义的。在您的情况下,您只想覆盖设计。

    您可以通过创建资源 XAML (BasePageResources.xaml) 并在其中使用 Setter 标记定义您的 UI 属性来非常简单地实现这一点。

    <Style TargetType="Button">
        <Setter Property="MinWidth" Value="75"/>
        <Setter Property="MinHeight" Value="23"/>
        <Setter Property="Margin" Value="11,11,0,0"/>
    </Style>
    

    您可以将所有 setter 转储到一个文件中,并为您的 setter 提供键,就像 CSS 类一样。 然后,在您的 App.xaml 中,您可以包含这些文件以使它们在应用程序范围内可访问。

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="BasePageResources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    

    应用程序中的所有按钮现在都应该应用您的样式。它比继承要好得多,而且 WPF 绑定感觉比您的解决方法更自然。如果按照您期望的方式进行设计,Blend 应该会给您带来更少的问题。

    【讨论】:

    • 感谢您的回答。实际上,我在我的资源中使用样式或任何其他自定义内容,例如数据模板、项目模板。我的意思是我正在为我的控件做同样的资源机制。我的问题是,当您想在项目中添加 WPF 用户控件时,它会为您创建一个 cs 和 xaml 文件。有没有办法像 schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft.com/winfx/2006/xaml" 控件和定义,资源
    【解决方案2】:

    我发现了问题。问题只是关于表达式混合。如果您的项目设置没有任何用于 Debug|AnyCPU 的 PropertyGroup,您将遇到此问题。您应该通过文本编辑器将此属性组添加到您的 csproj 文件中:

    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
        <PlatformTarget>x86</PlatformTarget>
        <DebugSymbols>true</DebugSymbols>
        <DebugType>full</DebugType>
        <Optimize>false</Optimize>
        <OutputPath>bin\Debug\</OutputPath>
        <DefineConstants>DEBUG;TRACE</DefineConstants>
        <ErrorReport>prompt</ErrorReport>
        <WarningLevel>4</WarningLevel>
      </PropertyGroup>
    

    【讨论】:

      猜你喜欢
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      • 2016-11-06
      • 1970-01-01
      • 2014-01-11
      • 2013-02-19
      相关资源
      最近更新 更多