应用程序无法在 Generic.xaml 中找到资源

发布于 2024-12-03 12:15:26 字数 5580 浏览 1 评论 0原文

我正在开发一个 WPF 应用程序,该应用程序使用适用于 WPF 的 Telerik RAD 控件。该应用程序将在带有触摸屏的 PC 上使用,因此我需要将 DateTimePicker 控件上的选择器等内容做得更大,以便像我这样拥有香肠手指的人可以轻松按下它们。

我最初使用 Expression Blend 来编辑控件模板的副本。这在我正在设计的 UserControl 的 XAML 文件中创建了一个 ControlTemplate。我现在正在处理另一个 UserControl,它也将使用 DateTimePicker,因此我想重用 ControlTemplate。

我所做的是将修改后的模板移动到项目(WPF 控件库)Generic.XAML 中新命名的 Style 中。这是 Generic.xaml 的一个简短片段:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:CarSystem.CustomControls"
                    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
                    xmlns:Telerik_Windows_Controls_Chromes="clr-namespace:Telerik.Windows.Controls.Chromes;assembly=Telerik.Windows.Controls">

    <Style x:Key="RadDateTimePickerControlTemplate1" TargetType="{x:Type telerik:RadDateTimePicker}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type telerik:RadDateTimePicker}">
                                  . . .
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

这是我引用样式的 XAML 片段:

<UserControl x:Class="CarSystem.CustomControls.ReportCriteria"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
             xmlns:cs="clr-namespace:CarSystem.CustomControls" 
             mc:Ignorable="d" 
             Height="648" 
             Width="1117">

    <Grid Background="{DynamicResource ContentBackground}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <GroupBox BorderBrush="Black" FontSize="20" FontWeight="Bold" Grid.Row="0" Header="Report Criteria: " Margin="5">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="320" />
                    <ColumnDefinition Width="200" />
                    <ColumnDefinition Width="450" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="110" />
                </Grid.ColumnDefinitions>

                <GroupBox BorderBrush="Black" FontSize="20" FontWeight="Bold" Grid.Column="0" Header="Date Range:" Margin="5">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="2*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <TextBlock FontSize="18" 
                                   FontWeight="Bold" 
                                   Grid.Column="0" 
                                   Grid.Row="0" 
                                   HorizontalAlignment="Right" 
                                   Text="Start Date:  " />
                        <telerik:RadDateTimePicker FontSize="18" 
                                                   FontWeight="Bold" 
                                                   Grid.Column="1" 
                                                   Grid.Row="0" 
                                                   Name="StartDatePicker" 
                                                   Style="{DynamicResource RadDateTimePickerControlTemplate1}" />
                        <TextBlock FontSize="18" 
                                   FontWeight="Bold" 
                                   Grid.Column="0" 
                                   Grid.Row="1" 
                                   HorizontalAlignment="Right" 
                                   Text="End Date:  " />
                        <telerik:RadDateTimePicker FontSize="18" 
                                                   FontWeight="Bold" 
                                                   Grid.Column="1" 
                                                   Grid.Row="1" 
                                                   Name="EndDatePicker" 
                                                   Style="{DynamicResource RadDateTimePickerControlTemplate1}" />
                    </Grid>
                </GroupBox>
                        . . .

        </GroupBox>
    </Grid>
</UserControl>

当我在 Expression Blend 中工作时,一切看起来都很好。我看到控件的下拉按钮的宽度发生了变化。回到 Visual Studio,一切都编译得很好,但更改没有显示 - 我只看到控件的默认样式,并且对该样式的引用下面有一条蓝色的波浪线。当您将鼠标悬停在波浪线上时,会显示以下消息:

     The resource "RadDateTimePickerControlTemplate1" cannot be resolved.

如果我在 XAML 中将“DynamicResource”更改为“StaticResource”,也会发生同样的情况。另外,我没有对该项目的 Assembly.Info 文件进行任何更改。

我该如何解决这个问题?

谢谢托尼

I am working on a WPF application that uses the Telerik RAD controls for WPF. The application will be used on a PC with a touch screen, so I need to make things like pickers on the DateTimePicker control larger so they can be easily pressed by people with sausage fingers like my own.

I originally used Expression Blend to edit a copy of the control's template. That created a ControlTemplate in the UserControl's XAML file that I was designing. I have another UserControl I'm working on now that will also use the DateTimePicker, so I want to reuse the ControlTemplate.

What I did was I moved the modified template into a new named Style in the project's (a WPF Control Library) Generic.XAML. Here's a short snippet of the Generic.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:CarSystem.CustomControls"
                    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
                    xmlns:Telerik_Windows_Controls_Chromes="clr-namespace:Telerik.Windows.Controls.Chromes;assembly=Telerik.Windows.Controls">

    <Style x:Key="RadDateTimePickerControlTemplate1" TargetType="{x:Type telerik:RadDateTimePicker}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type telerik:RadDateTimePicker}">
                                  . . .
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

Here's a snippet of the XAML where I reference the style:

<UserControl x:Class="CarSystem.CustomControls.ReportCriteria"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
             xmlns:cs="clr-namespace:CarSystem.CustomControls" 
             mc:Ignorable="d" 
             Height="648" 
             Width="1117">

    <Grid Background="{DynamicResource ContentBackground}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <GroupBox BorderBrush="Black" FontSize="20" FontWeight="Bold" Grid.Row="0" Header="Report Criteria: " Margin="5">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="320" />
                    <ColumnDefinition Width="200" />
                    <ColumnDefinition Width="450" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="110" />
                </Grid.ColumnDefinitions>

                <GroupBox BorderBrush="Black" FontSize="20" FontWeight="Bold" Grid.Column="0" Header="Date Range:" Margin="5">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="2*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <TextBlock FontSize="18" 
                                   FontWeight="Bold" 
                                   Grid.Column="0" 
                                   Grid.Row="0" 
                                   HorizontalAlignment="Right" 
                                   Text="Start Date:  " />
                        <telerik:RadDateTimePicker FontSize="18" 
                                                   FontWeight="Bold" 
                                                   Grid.Column="1" 
                                                   Grid.Row="0" 
                                                   Name="StartDatePicker" 
                                                   Style="{DynamicResource RadDateTimePickerControlTemplate1}" />
                        <TextBlock FontSize="18" 
                                   FontWeight="Bold" 
                                   Grid.Column="0" 
                                   Grid.Row="1" 
                                   HorizontalAlignment="Right" 
                                   Text="End Date:  " />
                        <telerik:RadDateTimePicker FontSize="18" 
                                                   FontWeight="Bold" 
                                                   Grid.Column="1" 
                                                   Grid.Row="1" 
                                                   Name="EndDatePicker" 
                                                   Style="{DynamicResource RadDateTimePickerControlTemplate1}" />
                    </Grid>
                </GroupBox>
                        . . .

        </GroupBox>
    </Grid>
</UserControl>

When I work in Expression Blend, everything looks fine. I see the change in the width of the drop down button for the control. Back in Visual Studio, everything compiles fine, but the change does not show up -- I see the default style for the control only, and the references to the style have a blue squiggly line under them. When you hover the mouse over the squiggly line, the following message is displayed:

     The resource "RadDateTimePickerControlTemplate1" cannot be resolved.

The same thing happens if I change "DynamicResource" to "StaticResource" in the XAML. Also, I have made no changes to the Assembly.Info file for the project.

How do I fix this?

Thanks

Tony

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸短情长 2024-12-10 12:15:26

据我记得你不能在 generic.xaml 中引用你想要引用的命名资源 - 你必须将其放入 app.xaml

你必须在样式的键中提供类型或在控件模板的键中提供 ComponentResourceKey
并在类静态构造函数中覆盖元数据,例如:

  public class FlatStylebutton : Button
  {
    static FlatStylebutton()
    {
      DefaultStyleKeyProperty.OverrideMetadata(typeof(FlatStylebutton), new FrameworkPropertyMetadata(typeof(FlatStylebutton)));
    }
  }

因此,问题的解决方案可以是将样式移动到 app.xaml

或者,您可以将其放入单独的 ResourceDictionary xaml 文件中,并将其导入到 ResourceDictionary.MergedDictionaries 中

As far as I remember you can't have named resources you want to reference in generic.xaml - that you have to put in app.xaml

You have to give the type in key for styles or ComponentResourceKey in key for controltemplates
and in the class static constructor override metadata like:

  public class FlatStylebutton : Button
  {
    static FlatStylebutton()
    {
      DefaultStyleKeyProperty.OverrideMetadata(typeof(FlatStylebutton), new FrameworkPropertyMetadata(typeof(FlatStylebutton)));
    }
  }

So a solution to your problem could be moving the style to app.xaml

Alternatively you can put it in a separate ResourceDictionary xaml file and import it in ResourceDictionary.MergedDictionaries

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文