【问题标题】:'storyboardName' resource not found. Call XAML to C#未找到“故事板名称”资源。将 XAML 调用到 C#
【发布时间】:2015-10-05 03:06:30
【问题描述】:

如果这是一个重复的问题,我很抱歉,但我只有 11 个声望,我无法评论其他问题或做任何事情,所以我不得不再次提问。

我有此代码:code - Quaternion Structure,我想将其添加到我的项目中,但找不到“QuaternionWText”、“endQuaternion”、“myQuaternionRotation3D”和“myRotateTransform3D”。

然后我从这里提出的另一个问题中找到了这部分代码:

XAML 代码

 <Storyboard x:Key="TheStoryboard" x:Name="storyboardName">
        <DoubleAnimation Storyboard.TargetProperty="Height"
                         To="500"
                         Duration="0:0:0.10" />
           <DoubleAnimation From="0" 
                            To="1" 
                            Duration="0:0:0.25"
                            Storyboard.TargetName="MainFrame" 
                            Storyboard.TargetProperty="(FrameworkElement.LayoutTransform).(ScaleTransform.ScaleY)"/>
    </Storyboard>

C#代码

 Storyboard story = (Storyboard)this.FindResource("storyboardName");
            this.BeginStoryboard(story);

但是当我运行代码时,我得到了找不到'storyboardName'资源的错误。

我的类的 XAML 名称是 XplorerMainWindow.xaml,cs 项目名称是 XplorerMainWindow.xaml.cs。我要在其中添加上述 C# 代码的类的名称名为“private void CreateWorker()”。我的 XAML 代码中的所有 x:name 都是:“MainFrame”、“DockingManager”、“MainPanel”、“MainDocPanel”、“DrawingControl”。

我想要做的是我有一个 3D 模型,我想使用四元数旋转它。但是我发现的所有示例都使用 XAML,但我总是收到错误,即他们找不到应该从 XAML 调用的资源。那么我对目标名称做错了什么吗?我应该根据我的班级和姓名进行任何更改吗?我应该将 c# 代码添加到其他地方而不是 CreatWorker() 吗?

编辑

<Window x:Class="XbimXplorer.XplorerMainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:xbim="http://schemas.Xbim.com/Presentation"
     xmlns:xbimView="clr-namespace:Xbim.IO.ViewModels;assembly=Xbim.IO"
    xmlns:local="clr-namespace:XbimXplorer"
xmlns:pt="clr-namespace:PropertyTools.Wpf;assembly=PropertyTools.Wpf" 
xmlns:avalonDock="http://schemas.xceed.com/wpf/xaml/avalondock"
Title="Xbim Xplorer" Height="600" Width="800" 
x:Name="MainWindow"
    Icon="xBIM.ico">
<Window.Resources>
    <ObjectDataProvider x:Key="ModelProvider"  IsInitialLoadEnabled="False" ></ObjectDataProvider>
    <DataTemplate DataType="{x:Type xbimView:XbimModelViewModel}">
        <Grid>
            <Grid.ColumnDefinitions>
                ....
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="1" Text="{Binding Name}" FontWeight="Bold"/>
        </Grid>
    </DataTemplate>

    <DataTemplate DataType="{x:Type xbimView:SpatialViewModel}">
        <Grid>
            <Grid.ColumnDefinitions>
               <....
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="1" Text="{Binding Name}" FontWeight="Bold"/>
        </Grid>
    </DataTemplate>

    <DataTemplate DataType="{x:Type xbimView:ContainedElementsViewModel}">
        <Grid>
            <Grid.ColumnDefinitions>
               .....
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="1" Text="{Binding Name}"/>
        </Grid>
    </DataTemplate>

    <DataTemplate DataType="{x:Type xbimView:IfcProductModelView}">
        <Grid>
            <Grid.ColumnDefinitions>
                .....
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="1" Text="{Binding Name}"/>
        </Grid>
    </DataTemplate>

 //Here is the Storyboard

      <Storyboard x:Key="TheStoryboard" x:Name="storyboardName">
        <DoubleAnimation Storyboard.TargetProperty="Height"
                     To="500"
                     Duration="0:0:0.10" />
        <DoubleAnimation From="0" 
                        To="1" 
                        Duration="0:0:0.25"
                        Storyboard.TargetName="MainFrame" 
                        Storyboard.TargetProperty="   (FrameworkElement.LayoutTransform).(ScaleTransform.ScaleY)"/>
    </Storyboard>

</Window.Resources>

这就是我在 XplorerMainWindow.xaml.cs 中的称呼

 private void SpatialControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {

        Storyboard story = (Storyboard)this.FindResource("storyboardName");
        this.BeginStoryboard(story);
}

【问题讨论】:

    标签: c# wpf xaml storyboard quaternions


    【解决方案1】:

    您似乎试图从另一个窗口获取Storyboard。这意味着,如果您想在代码隐藏中获取一些资源,您应该将该资源放在当前窗口中。 欲了解更多信息,请访问How to: Retrieve Resources in Code

    PS

    这里是一个声明和使用资源的例子:

    <Window x:Class="XbimXplorer.XplorerMainWindow" ...>
        <Window.Resources>
            <Storyboard x:Key="TheStoryboard" />
        </Window.Resources>
    ...
    </Window>
    

    而且你可以在 .cs 中获得这个故事板

    【讨论】:

    • 所以我的 XMAL 的开头是这样的 "
    • 很难帮助你,因为我没有看到完整的 XAML 代码。确保此情节提要放置在窗口的资源中。 PS 我在上面的帖子中添加了一个示例。
    • 我已经编辑了我的问题,并添加了我认为您需要知道的 XAML 部分,以及我称之为 Storyboard 的 .cs 类。我确实在资源中有故事板,但是当我双击模型“PresentationFramework.dll 中发生'System.Windows.ResourceReferenceKeyNotFoundException'类型的未处理异常附加信息:'storyboardName'资源未找到”时,我仍然得到这个。我找不到我做错了什么:/
    • 好的。我刚刚意识到一个问题。您必须使用密钥而不是名称来获取资源。她就是一个例子this.Resources["TheStoryboard"]
    • 是的,就是这样。问题解决了。谢谢。这就是我所做的故事板故事; story = (Storyboard)this.Resources["TheStoryboard"];
    猜你喜欢
    • 2021-02-16
    • 2017-01-19
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    • 2013-04-03
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多