ResourceDictionary.Source 属性 中的 MarkupExtension
MarkupExtension in ResourceDictionary.Source property
我正在尝试创建一个标记扩展,以简化为 WPF ResourceDictionary 的源 属性 编写 URI。
问题的最小示例如下:
CS:
public class LocalResourceExtension : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return new Uri("Resources.xaml", UriKind.Relative);
}
}
XAML:
<UserControl ...>
<UserControl.Resources>
<ResourceDictionary Source="{mw:LocalResource}" /> <!-- error MC3022 -->
<!-- <ResourceDictionary Source="Resources.xaml" /> --> <!-- Works fine -->
</UserControl.Resources>
<!-- ... -->
</UserControl>
编译时出现以下错误:
error MC3022: All objects added to an IDictionary must have a Key attribute or some other type of key associated with them.
但是,如果我用常量值替换标记扩展(如注释行所示),一切正常。
为什么带有标记扩展的版本不起作用?有解决方法吗?
我正在使用 MSVC 2015。
这对我有用:
public class LocalResource : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return new ResourceDictionary() {
Source = new Uri("Resources.xaml", UriKind.Relative)
};
}
}
XAML
<Window.Resources>
<myNamespace:LocalResource />
</Window.Resources>
XAML 编辑器在设计时用蓝色波浪线 <myNamespace:LocalResource />
,这会杀死设计视图。所以这只有在您不使用设计视图时才有效。我不知道,但有些人知道。
我一直告诉我的女朋友我是自伽利略以来最伟大的天才,她就是不相信我。 Galileo 会找到使设计视图工作的方法。
更新
第二个解决方案:
public class LocalResourceDictionary : ResourceDictionary
{
public LocalResourceDictionary()
{
Source = new Uri("Resources.xaml", UriKind.Relative);
}
}
XAML
<Window.Resources>
<myNamespace:LocalResourceDictionary />
</Window.Resources>
这在运行时工作正常,消除了波浪形,并允许设计人员工作。但是,它在设计模式下无法合并资源文件。还是不理想。
更新
OP比我聪明。这会做所有事情:
public class LocalResourceDictionary : ResourceDictionary
{
public LocalResourceDictionary()
{
Source = new Uri("pack://application:,,,/MyAssemblyName;component/Resources.xaml", UriKind.Absolute);
}
}
我正在尝试创建一个标记扩展,以简化为 WPF ResourceDictionary 的源 属性 编写 URI。
问题的最小示例如下:
CS:
public class LocalResourceExtension : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return new Uri("Resources.xaml", UriKind.Relative);
}
}
XAML:
<UserControl ...>
<UserControl.Resources>
<ResourceDictionary Source="{mw:LocalResource}" /> <!-- error MC3022 -->
<!-- <ResourceDictionary Source="Resources.xaml" /> --> <!-- Works fine -->
</UserControl.Resources>
<!-- ... -->
</UserControl>
编译时出现以下错误:
error MC3022: All objects added to an IDictionary must have a Key attribute or some other type of key associated with them.
但是,如果我用常量值替换标记扩展(如注释行所示),一切正常。
为什么带有标记扩展的版本不起作用?有解决方法吗?
我正在使用 MSVC 2015。
这对我有用:
public class LocalResource : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return new ResourceDictionary() {
Source = new Uri("Resources.xaml", UriKind.Relative)
};
}
}
XAML
<Window.Resources>
<myNamespace:LocalResource />
</Window.Resources>
XAML 编辑器在设计时用蓝色波浪线 <myNamespace:LocalResource />
,这会杀死设计视图。所以这只有在您不使用设计视图时才有效。我不知道,但有些人知道。
我一直告诉我的女朋友我是自伽利略以来最伟大的天才,她就是不相信我。 Galileo 会找到使设计视图工作的方法。
更新
第二个解决方案:
public class LocalResourceDictionary : ResourceDictionary
{
public LocalResourceDictionary()
{
Source = new Uri("Resources.xaml", UriKind.Relative);
}
}
XAML
<Window.Resources>
<myNamespace:LocalResourceDictionary />
</Window.Resources>
这在运行时工作正常,消除了波浪形,并允许设计人员工作。但是,它在设计模式下无法合并资源文件。还是不理想。
更新
OP比我聪明。这会做所有事情:
public class LocalResourceDictionary : ResourceDictionary
{
public LocalResourceDictionary()
{
Source = new Uri("pack://application:,,,/MyAssemblyName;component/Resources.xaml", UriKind.Absolute);
}
}