无法解析DictionaryResource中的wpf ControlTemplate错误 [英] wpf ControlTemplate in DictionaryResource can not be resolved error

查看:78
本文介绍了无法解析DictionaryResource中的wpf ControlTemplate错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ResourceDictionary中定义了一个ControlTemplate,像这样的ControlTemplate

I defined a ControlTemplate within the ResourceDictionary, the ControlTemplate like that

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ControlTemplate x:Key="CTGelButton" TargetType="{x:Type Button}">
        <Grid Width="100" Height="100">
            <Ellipse Stroke="#FF000000" Fill="#FF1C46E7" x:Name="OuterEllipse">
                <Ellipse.BitmapEffect>
                    <BevelBitmapEffect BevelWidth="8" EdgeProfile="BulgedUp" Smoothness="0.745"/>
                </Ellipse.BitmapEffect>
            </Ellipse>
            <Ellipse Margin="8,8,8,8" x:Name="InnerEllipse">
                <Ellipse.Fill>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0" SpreadMethod="Pad">
                        <GradientStop Color="#FF1C46E7" Offset="0"/>
                        <GradientStop Color="#FFFFFFFF" Offset="1"/>
                    </LinearGradientBrush>
                </Ellipse.Fill>
            </Ellipse>
            <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Content}"/>
        </Grid>
    </ControlTemplate>



</ResourceDictionary>



而且我想tp将此ControlTemplate应用于window.xaml代码之类的按钮,如



and I want tp apply this ControlTemplate to a button, within the window.xaml code like that

Window x:Class="ControlTemplate.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>

    </Window.Resources>
    
    <Grid>
        <StackPanel >

            <Button Content="Hello" Width="50" Height="50" Template="{DynamicResource CTGelButton}" Click="Button_Click" />

        </StackPanel>
    </Grid>
</Window>



但是window.xaml显示警告说"CTGelButton无法解析",当我运行它时,没有按钮出现.
我该如何解决?如何将ControlTemplate用作DynamicResource?
我在这里想念什么吗?



but window.xaml shows warning saying "CTGelButton can not be resolved", when I run it there is no button appear.
how can I fix that? how can I use ControlTemplate as DynamicResource?
did I miss something here?
could anyone help me please?

推荐答案

您需要将原始资源字典作为资源包含在Window.XAML文件中.
You need to include the original Resource Dictionary as a resource in your Window.XAML file
<Window x:Class="ControlTemplate.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="yourResourceDictionary.xaml" />
            </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
    etc



或者如果您希望项目中的所有XAML文件都可以使用它,则可以将其包含在App.XAML文件中.



or you could include it in your App.XAML file if you want it available to all XAML files in your project.




我遇到了与您相同的问题,这是它为我工作的解决方案:

1.-右键单击项目名称,然后选择卸载项目".
2.-右键单击项目名称,然后选择编辑<<项目名称>>"
3.-搜索资源文件名,例如说MyResource.您会发现类似的内容:

Hi,

I had the same problem as you and this is the solution it worked for me:

1.- Right click on the project name and select "Unload Project".
2.- Right click on the project name and select "Edit <<Project name>>"
3.- Search the resource file name, let say MyResource. You will find something like:

<Content Include="Skins\MyResource.xaml">
  <SubType>Designer</SubType>
  <Generator>MSBuild:Compile</Generator>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>




4.-替换为:




4.- Replace it with:

<Content Include="Skins\MyResource.xaml">
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  <Generator>MSBuild:Compile</Generator>
  <SubType>Designer</SubType>
  <Generator>MSBuild:Compile</Generator>
  <SubType>Designer</SubType>
</Content>



5.-关闭项目文件.
6.-右键单击项目名称,然后选择重新加载项目".
7.-问题解决了,Visual Studio可以在设计时加载资源.

我没有检查解决方案是否按XML标记的顺序进行更改,或者是否由于双重"Generator"和"SubType"标记而导致.我没有时间找出来.

希望对您有所帮助.

本杰明



5.- Close the project file.
6.- Right click on the project name and select "Reload project".
7.- Cleck the problem is gone and Visual Studio can load resources at Design time.

I did not check if the solution comes for the change in the order of the XML tags or it comes due to the double "Generator" and "SubType" tags. I had no time to find it out.

I hope this may help some one.

Benjamin


这篇关于无法解析DictionaryResource中的wpf ControlTemplate错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆