简体   繁体   English

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

[英]Unable to find static resource in runtime even while designer can see it

So i have this markup: WizardPageTpl.xaml 所以我有这个标记: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 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>

Then I try to use it: 然后我尝试使用它:

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

The VS designer show everything Ok, but in runtime i get 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: 

Whats the prolem?? 问题是什么?

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

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

Set the build action on WizardPageTpl.xaml to Content. 将WizardPageTpl.xaml上的生成操作设置为Content。

I don't know if this is right but it fixes the problem. 我不知道这是对的,但可以解决问题。

just try this 试试这个

Style="{StaticResource WizardPageControl}" 样式=“ {StaticResource WizardPageControl}”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM