尝试使用标记扩展时出错

时间:2013-10-10 18:57:07

标签: c# xaml

我有一个小窗口,我试图在我的应用程序启动时加载。这是(松散的)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/>

或事件向下移动一行,我收到以下错误:

  

其他信息:'无法创建未知类型   '{http://schemas.microsoft.com/winfx/2006/xaml/presentation} RESX'“。   行号'13'和行位置'33'。

对我来说真正奇怪的是,如果我将ConnectionStatusIndicator替换为来自同一程序集的另一个自定义控件,我会收到错误。另一个自定义控件非常相似,但还有一些属性。

有谁能解释这里发生了什么?

1 个答案:

答案 0 :(得分:1)

Resx标记扩展属于Infralution.Localization.Wpf命名空间,但也做了一些hackish并试图将自己注册到http://schemas.microsoft.com/winfx/2006/xaml/presentation xml命名空间,以允许开发人员将其用作{{Resx ...} 1}}而不是必须在XAML中声明名称空间,并使用前缀为{resxNs:Resx ...}的扩展名。

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

Infralution.Localization.Wpf

此外,对于任何感兴趣的人,“hack”都在本地化库的这些行中:

<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>
相关问题