2

MainWindow に動的に読み込まれるスタンドアロンの XAML ファイルがあります。XAML ファイルにはアニメーションが含まれています。XAML を読み込むのは簡単ですが、XAML が読み込まれた後にアニメーションを動作させる正しい方法は何ですか。これは私のテスト コードで、通常の WPF ウィンドウに貼り付けると正しく動作しますが、動的にロードするとアニメーションが開始されないように見えます。TargetName と TargetProperty への参照に問題があると思われます。

XAML:

<Window 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Page1" Width="1080" Height="1920">


    <Grid x:Name="GridWrapper">
        <Viewbox x:Name="ViewboxWrapper">
            <Grid x:Name="GridMain" Width="1080" Height="1920" Background="Black">
                <Grid.Resources>
                    <Storyboard x:Key="StoryboardPhotos" Duration="0:0:5">
                        <DoubleAnimation Storyboard.TargetName="LabelPhoto_01" Storyboard.TargetProperty="Opacity" BeginTime="0:0:0" Duration="0:0:5" To="1.0"></DoubleAnimation>
                    </Storyboard>
                </Grid.Resources>
                <Grid.RowDefinitions>
                    <RowDefinition Height="100*"></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Label x:Name="LabelPhoto_01" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource StyleLabelPhoto}" Opacity="0">
                    <Image x:Name="ImagePhoto_01" Source="../../../Content/FPO/1.jpg"></Image>
                </Label>
            </Grid>
        </Viewbox>
    </Grid>
</Window>

C# コード:

 private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            Window window = null;
            using (FileStream fs = new FileStream("Templates/16x9/Portrait/1.xaml", FileMode.Open, FileAccess.Read)) //load xaml file
            {
                window = (Window)XamlReader.Load(fs);
            }

            Viewbox wrapper = (Viewbox)window.FindName("ViewboxWrapper");
            Grid mainGrid = wrapper.FindName("GridMain") as Grid;
            var sb = (Storyboard)mainGrid.FindResource("StoryboardPhotos"); 

            //disconnect grid from the parent window
            ((Grid)wrapper.Parent).Children.Remove(wrapper);

            // attach the grid to the main window
            GridParent.Children.Add(wrapper);
            sb.Begin();
        }
4

1 に答える 1

1

の後に以下のコードを挿入しますGridParent.Children.Add(wrapper);

sb.Begin(window);
window.Close();

そうしないと、Namescope エラーが発生します。

于 2015-12-16T15:35:23.167 に答える