Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从XamlReader.Parse(xmlFragment)调用中引用现有资源_C#_Wpf_Xaml_Datatemplate_Xamlparseexception - Fatal编程技术网

C# 如何从XamlReader.Parse(xmlFragment)调用中引用现有资源

C# 如何从XamlReader.Parse(xmlFragment)调用中引用现有资源,c#,wpf,xaml,datatemplate,xamlparseexception,C#,Wpf,Xaml,Datatemplate,Xamlparseexception,有一种情况是,我正在使用XamlReader.Parse(xamlString)创建自定义数据模板,其中xamlString是包含数据模板的片段: <DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <StackPanel Visibility="{Binding MyBinding, Converter={StaticResource boolToVisibi

有一种情况是,我正在使用XamlReader.Parse(xamlString)创建自定义数据模板,其中xamlString是包含数据模板的片段:

<DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <StackPanel Visibility="{Binding MyBinding, Converter={StaticResource boolToVisibilityConverter}}">
        ...
    </StackPanel>
</DataTemplate>

为什么加载的XAML片段不能引用页面中的现有资源?

当最初创建XAML片段时,我认为它不知道要将其放入的父容器的任何信息,包括父容器中定义的静态资源。尝试使用DataTemplate.Resources引用DataTemplate中的BooltVisibilityConverter。

到目前为止,我的解决方案是将所有资源定义嵌入解析的XAML中;这就是你指的,对吗?XamlReader.Parse成功了,我将加载的元素添加到场景中,直到后来我才发现找不到资源的错误。@vargonian确定,但只是DataTemplate所需的资源。但是如果资源是自定义数据类型,该怎么办,例如,当前组件中定义的自定义类型转换器?我得到了“无法创建未知类型”的例外。显然,我用敲木头的方法回答了自己的问题。我必须将生成的XAML中的本地名称空间从clr名称空间:MyAssembly更改为clr名称空间:MyAssembly;assembly=MyAssembly
System.Windows.Markup.XamlParseException: ''Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.' Line number 'x' and line position 'y'.'

Inner Exception:
Exception: Cannot find resource named 'boolToVisibilityConverter'. Resource names are case sensitive.