Невозможно найти статический ресурс во время выполнения, даже когда его видит дизайнер - PullRequest
2 голосов
/ 27 мая 2010

Итак, у меня есть эта разметка: 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 голосов
/ 14 марта 2013

Убедитесь, что ваш конструктор вызывает InitializeComponent () Метод.

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

0 голосов
/ 28 февраля 2015

Установите для действия сборки в WizardPageTpl.xaml значение Content.

Я не знаю, правильно ли это, но это решает проблему.

0 голосов
/ 27 мая 2010

просто попробуйте это

Style = "{StaticResource WizardPageControl}"

...