即使设计人员可以看到它,也无法在运行时找到静态资源

时间:2010-05-27 08:10:09

标签: c# .net wpf wpf-controls

所以我有这个标记: WizardPageTpl.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Werp.MigrationHelper">
     <Style TargetType="{x:Type UserControl}" x:Key="WizardPageControl" x:Name="WizardPageControl">
      <Setter Property="Template">
       <Setter.Value>
        <ControlTemplate TargetType="{x:Type UserControl}">
         <Grid>
          <Grid.ColumnDefinitions>
           <ColumnDefinition Width="10" />
           <ColumnDefinition Width="475" />
           <ColumnDefinition Width="10" />
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
           <RowDefinition Height="10" />
           <RowDefinition Height="410"/>
           <RowDefinition Height="50" />
           <RowDefinition Height="10" />
          </Grid.RowDefinitions>
          <StackPanel Orientation="Vertical" Name="MainContent" Grid.Row="1" Grid.Column="1">
           <ContentPresenter Content="{TemplateBinding Content}"/>
          </StackPanel>
          <StackPanel Grid.Column="1" Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,10" Height="30">
           <Button Name="BackButton" Width="50" xml:space="preserve">&lt; _Back</Button>
           <Button Name="NextButton" Width="50" Margin="10,0,0,0" xml:space="preserve" IsDefault="True">_Next &gt;</Button>
           <Button Name="CancelButton" Width="50" Margin="10,0,0,0" IsCancel="True">_Cancel</Button>
           <Button Name="FinishButton" IsEnabled="True" Width="50" Margin="10,0,0,0">_Finish</Button>
          </StackPanel>
         </Grid>
        </ControlTemplate>
       </Setter.Value>
      </Setter>
     </Style>
    </ResourceDictionary>

的App.xaml

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
         <Application.Resources>
          <ResourceDictionary>
           <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Resources\WizardPageTpl.xaml" />
           </ResourceDictionary.MergedDictionaries>
          </ResourceDictionary>
         </Application.Resources>
        </Application>

WizardPageControl.xaml

<UserControl x:Class="Werp.MigrationHelper.WizardPageControl"
        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:local="clr-namespace:Werp.MigrationHelper" mc:Ignorable="d">
    </UserControl>

然后我尝试使用它:

<PageFunction
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Werp.MigrationHelper" 
    x:Class="Werp.MigrationHelper.WizardPage1"
    x:TypeArguments="local:WizardResult"
    KeepAlive="True"
    WindowTitle="Wizard Page 1"
    >
    <local:WizardPageControl Style="{StaticResource ResourceKey=WizardPageControl}">
        <local:WizardPageControl.Content>
            qweqweqweqweq
        </local:WizardPageControl.Content>
    </local:WizardPageControl>
</PageFunction>

VS设计师显示一切正常,但在运行时我得到了

System.Windows.Markup.XamlParseException occurred
  Message='Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '4' and line position '5'.
  Source=PresentationFramework
  LineNumber=4
  LinePosition=5
  StackTrace:
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
       at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
       at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       at Werp.MigrationHelper.WizardPage1.InitializeComponent() in d:\Home\Docs\Visual Studio 2008\Projects\csharp\wizardpage1.xaml:line 1
       at Werp.MigrationHelper.WizardPage1..ctor(WizardData wizardData) in D:\home\Docs\Visual Studio 2008\Projects\csharp\WizardPage1.xaml.cs:line 12
  InnerException: 
       Message=Cannot find resource named 'WizardPageControl'. Resource names are case sensitive.
       Source=PresentationFramework
       StackTrace:
            at System.Windows.StaticResourceExtension.ProvideValue(IServiceProvider serviceProvider)
            at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension me, IServiceProvider serviceProvider)
       InnerException: 

什么是问题?

3 个答案:

答案 0 :(得分:0)

试试这个

Style =“{StaticResource WizardPageControl}”

答案 1 :(得分:0)

确保构造函数调用 InitializeComponent()方法。

public MainWindow() { InitializeComponent(); ... }

答案 2 :(得分:0)

将WizardPageTpl.xaml上的构建操作设置为Content。

我不知道这是否正确,但它解决了问题。