c# - 无法在运行时找到静态资源,即使设计师可以看到它

标签 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>

应用.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: 

有什么问题??

最佳答案

试试这个

Style="{StaticResource WizardPageControl}"

关于c# - 无法在运行时找到静态资源,即使设计师可以看到它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2919442/

相关文章:

c# - 将数据从行保存到数据库

Wpf控制内容大小?

javascript - 将空数据从 $.post 发送到 Controller 模型

c# - ASP.Net Identity 2 - 来自 OAuthAuthorizationServerProvider 的自定义响应

c# - Code-First 应用程序中的 XML 列

c# - 如何设置 TcpListener 始终监听并接受多个连接?

c# - SlimDX - 终止线程

c# - Pechkin 和 IIS 中使用的进程

wpf - 如何在 WPF Viewbox 中保持恒定的 FontSize?

wpf - 有没有办法使 WPF 中的控件对鼠标事件透明?