2013-10-10 32 views
2

我有一个小窗口,当我尝试加载应用程序启动时。这里是(松散的)XAML:尝试使用标记扩展时出错

<ctrl:MainWindow 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:ctrl="clr-namespace:Controls;assembly=Controls"> 
<Grid> 
    <ctrl:ConnectionStatusIndicator/> 
    <TextBlock Grid.Row="2" Text="{Resx ResxName=MyApp.MainDialog, Key=MyLabel}"/> 
</Grid> 
</ctrl:MainWindow> 

请注意称为ConnectionStatusIndicator的自定义控件。它的代码是:

using System.Windows; 
using System.Windows.Controls; 

namespace Controls 
{ 
    public class ConnectionStatusIndicator : Control 
    { 
     public ConnectionStatusIndicator() 
     { 
     } 

     static ConnectionStatusIndicator() 
     { 
      DefaultStyleKeyProperty.OverrideMetadata(typeof(ConnectionStatusIndicator), 
                new FrameworkPropertyMetadata(typeof(ConnectionStatusIndicator))); 
      IsConnectedProperty = DependencyProperty.Register("IsConnected", typeof(bool), typeof(ConnectionStatusIndicator), new FrameworkPropertyMetadata(false)); 
     } 

     public bool IsConnected 
     { 
      set { SetValue(IsConnectedProperty, value); } 
      get { return (bool)GetValue(IsConnectedProperty); } 
     } 

     private static DependencyProperty IsConnectedProperty; 
    } 
} 

现在,这里是它变得奇怪(对我来说,至少)。使用上面显示的XAML,我的应用程序将构建并运行得很好。但是,如果我删除以下行:

<ctrl:ConnectionStatusIndicator/> 

或事件将其移动一行下来,我得到以下错误:

Additional information: 'Cannot create unknown type '{ http://schemas.microsoft.com/winfx/2006/xaml/presentation }Resx'.' Line number '13' and line position '33'.

什么是真奇怪,我是说,如果我更换ConnectionStatusIndicator与来自同一个程序集的另一个自定义控件一起,我得到错误。另一个自定义控件非常相似,但有更多的属性。

任何人都可以解释这里发生了什么?

+0

不应该命名空间为:CNC.UI.Controls在XAML? – CSharpie

+0

这是什么?{ResxName = MyApp.MainDialog,Key = MyLabel}? –

+0

之前我曾经被咬过的一件事是,如果依赖项属性不公开,设计师喜欢采取行动。尝试将“IsConnectedProperty”公开标记,看看它是否有帮助。 – Psytronic

回答

1

Resx标记扩展属于Infralution.Localization.Wpf命名空间,但也做了一个有点hackish,并试图将其自身注册为http://schemas.microsoft.com/winfx/2006/xaml/presentation XML命名空间,允许开发人员使用它作为{Resx ...}而不必声明命名空间的XAML和使用扩展名前缀为{resxNs:Resx ...}

我相信如果你清理你的解决方案,并可能删除你的* .sou文件,项目将按预期构建,但解决这个问题的一个可靠方法是为Infralution.Localization.Wpf添加一个xmlns声明,并使用扩展名的xmlns前缀:

<ctrl:MainWindow 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:ctrl="clr-namespace:Controls;assembly=Controls" 
    xmlns:loc="clr-namespace:Infralution.Localization.Wpf;assembly=Infralution.Localization.Wpf"> 
    <Grid> 
     <ctrl:ConnectionStatusIndicator/> 
     <TextBlock Grid.Row="2" Text="{loc:Resx ResxName=MyApp.MainDialog, Key=MyLabel}"/> 
    </Grid> 
</ctrl:MainWindow> 

此外,对于有兴趣的人,在 “黑客” 是在本地化库这些行:

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "Infralution.Localization.Wpf")] 
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2007/xaml/presentation", "Infralution.Localization.Wpf")] 
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2008/xaml/presentation", "Infralution.Localization.Wpf")] 
+0

谢谢!我尝试添加名称空间,但从未使用它。谢谢! – JAB

+0

@Adi Lester,如何在应用程序的其他(除Shell/MainWindow)xaml文件中使用此(Resx)。即使所提供的例子是MainWindow,我也找不到语法。在另一个项目下可能出现的其他申请意见如何? – kamlendra

+1

@kamlendra我不是一个关于扩展的权威,也没有与它合作过很长一段时间,但我认为它的用法对于其他视图/用户控件没有任何不同。 –

相关问题