Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.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# 从ControlTemplate字符串调用转换器_C#_Wpf_Xaml - Fatal编程技术网

C# 从ControlTemplate字符串调用转换器

C# 从ControlTemplate字符串调用转换器,c#,wpf,xaml,C#,Wpf,Xaml,我正在尝试将controlTemplate绑定到generic.xaml中。我的controlTemplate中包含转换器。绑定时,它会引发异常,因为“System.Windows.Markup.StaticResourceHolder”上的值为时会引发异常 在MyView.cs中 templateString = @"<ControlTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""

我正在尝试将controlTemplate绑定到generic.xaml中。我的controlTemplate中包含转换器。绑定时,它会引发异常,因为“System.Windows.Markup.StaticResourceHolder”上的值为
时会引发异常

在MyView.cs中

 templateString = @"<ControlTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" >
                                            <Canvas><Polygon Points=""{Binding Size,Converter={StaticResource SizeConverter}}"" Fill=""red""/></Canvas>
                                       </ControlTemplate>";

this.Template = XamlReader.Load(new System.Xml.XmlTextReader(new StringReader(templateString ))) as ControlTemplate;
templateString=@”
";
this.Template=XamlReader.Load(new System.Xml.XmlTextReader(new StringReader(templateString)))作为ControlTemplate;
在generic.xaml中

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:src="clr-namespace:MySample"
                    xmlns:vsm="clr-namespace:System.Windows;assembly=PresentationFramework"
                    xmlns:srcview="clr-namespace:MySample.Views"
                    xmlns:converters="clr-namespace:MySample.Converters"
                    >
    <converters:SizeConverter x:Key="SizeConverter" />

    <Style TargetType="{x:Type srcview:MyView}">
        <Setter Property="Template">
            <Setter.Value>
               <ControlTemplate TargetType="{x:Type srcview:MyView}">      
                   <Canvas>
                      <ContentControl Template="{TemplateBinding Template}" Name="contentControl" >                      
                      </ContentControl>

                   </Canvas>
               </ControlTemplate>
           </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>


如何解决这个问题???

当静态资源在引用之前没有定义时,通常会遇到这个问题

对于测试,请尝试使用xaml的resources部分中定义的静态资源读取xaml usercontrol。如果这样做有效,那么您就知道问题不在于引用转换器,而在于定义转换器的时间和地点

确保转换器实际加载的更快方法是将其放入app.xaml中。这将确保在启动时加载资源字典。下面是一个例子:

<Application x:Class="TeslaFrame.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:metro="http://schemas.codeplex.com/elysium"
         >
 <Application.Resources>      
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/Elysium;component/Themes/Generic.xaml"/>
            <ResourceDictionary>
                <BooleanToVisibilityConverter x:Key="globalBoolToVisConverter" />                 
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
 </Application.Resources>
</Application>

您能详细解释一下您想做什么吗?