' LoadingSpinner'和资源未找到

时间:2013-10-29 12:29:36

标签: c# wpf mvvm

我将Deanchalk Loading Spinner用于WPF APP。 当它隐藏之前加载,并且我再次激活它时,一个例外说

  

'没有找到'LoadingSpinner'资源   /无法将类型为MS.Internal.NamedObject的对象强制转换为System.Windows.Style

请告诉我,

代码在这里:

public class AsyncNotifier
    {
        public static readonly DependencyProperty TriggerProperty =
            DependencyProperty.RegisterAttached("Trigger", typeof(bool),
            typeof(AsyncNotifier),
            new PropertyMetadata(false, TriggerCallback));

        public static readonly DependencyProperty SpinnerTextProperty =
            DependencyProperty.RegisterAttached("SpinnerText", typeof(string),
            typeof(AsyncNotifier));

        private static readonly DependencyProperty SpinnerProperty =
            DependencyProperty.RegisterAttached("Spinner", typeof(Grid),
            typeof(AsyncNotifier));

        public static void SetTrigger(DependencyObject d, bool trigger)
        {
            d.SetValue(TriggerProperty, trigger);
        }

        public static void SetSpinnerText(DependencyObject d, string text)
        {
            d.SetValue(SpinnerTextProperty, text);
        }

        private static void TriggerCallback(DependencyObject d,
DependencyPropertyChangedEventArgs e)
        {
            var parentGrid = d as Grid;
            if (parentGrid == null)
                return;
            string spinnerText = (string)parentGrid.GetValue(SpinnerTextProperty);
            bool trigger = (bool)parentGrid.GetValue(TriggerProperty);
            Grid grid = parentGrid.GetValue(SpinnerProperty) as Grid;
            if (grid == null)
            {
                grid = new Grid();
                parentGrid.SetValue(SpinnerProperty, grid);
                if (parentGrid.ColumnDefinitions.Count > 0)
                    Grid.SetColumnSpan(grid, parentGrid.ColumnDefinitions.Count);
                if (parentGrid.RowDefinitions.Count > 0)
                    Grid.SetRowSpan(grid, parentGrid.RowDefinitions.Count);
            }
            grid.Background = new SolidColorBrush(Colors.White) { Opacity = 0.6 };
            grid.Children.Clear();
            ContentControl cont = new ContentControl();
            cont.Content = new TextBlock() { Text = spinnerText };
            cont.Style = (Style)parentGrid.FindResource("LoadingSpinner");
            grid.Children.Add(cont);
            if (!parentGrid.Children.Contains(grid))
                parentGrid.Children.Add(grid);
            grid.Visibility = trigger ? Visibility.Visible : Visibility.Hidden;
        }
    }

的Xaml:

<Style x:Key="LoadingSpinner" TargetType="ContentControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContentControl">
                    <Grid>
                        <Rectangle Width="160" Height="160">
                            <Rectangle.Fill>
                                <VisualBrush Stretch="None">
                                    <VisualBrush.Visual>
                                        <Canvas RenderTransformOrigin="0.5,0.5">
                                            <Ellipse Canvas.Left="71.1667"
                                                     Canvas.Top="3.00002"
                                                     Width="14.8333"
                                                     Height="14.8333"
                                                     Fill="#FF000000"
                                                     Stretch="Fill" />
                                            <Ellipse Canvas.Left="71.1667"
                                                     Canvas.Top="139.833"
                                                     Width="14.8333"
                                                     Height="14.8333"
                                                     Fill="#85000000"
                                                     Stretch="Fill" />
                                            <Ellipse Canvas.Left="139.583"
                                                     Canvas.Top="71.4167"
                                                     Width="14.8333"
                                                     Height="14.8333"
                                                     Fill="#C2000000"
                                                     Stretch="Fill" />
                                            <Ellipse Canvas.Left="2.75"
                                                     Canvas.Top="71.4167"
                                                     Width="14.8333"
                                                     Height="14.8333"
                                                     Fill="#48000000"
                                                     Stretch="Fill" />
                                            <Ellipse Canvas.Left="22.7888"
                                                     Canvas.Top="23.0388"
                                                     Width="14.8333"
                                                     Height="14.8333"
                                                     Fill="#29000000"
                                                     Stretch="Fill" />
                                            <Ellipse Canvas.Left="119.545"
                                                     Canvas.Top="119.795"
                                                     Width="14.8333"
                                                     Height="14.8333"
                                                     Fill="#A4000000"
                                                     Stretch="Fill" />
                                            <Ellipse Canvas.Left="119.545"
                                                     Canvas.Top="23.0388"
                                                     Width="14.8333"
                                                     Height="14.8333"
                                                     Fill="#E1000000"
                                                     Stretch="Fill" />
                                            <Ellipse Canvas.Left="22.7888"
                                                     Canvas.Top="119.795"
                                                     Width="14.8333"
                                                     Height="14.8333"
                                                     Fill="#67000000"
                                                     Stretch="Fill" />
                                            <Ellipse Canvas.Left="44.9828"
                                                     Canvas.Top="8.20598"
                                                     Width="14.8372"
                                                     Height="14.8372"
                                                     Fill="#1A000000"
                                                     Stretch="Fill" />
                                            <Ellipse Canvas.Left="97.3466"
                                                     Canvas.Top="134.623"
                                                     Width="14.8372"
                                                     Height="14.8372"
                                                     Fill="#94000000"
                                                     Stretch="Fill" />
                                            <Ellipse Canvas.Left="134.373"
                                                     Canvas.Top="45.2328"
                                                     Width="14.8372"
                                                     Height="14.8372"
                                                     Fill="#D2000000"
                                                     Stretch="Fill" />
                                            <Ellipse Canvas.Left="7.95596"
                                                     Canvas.Top="97.5967"
                                                     Width="14.8372"
                                                     Height="14.8372"
                                                     Fill="#57000000"
                                                     Stretch="Fill" />
                                            <Ellipse Canvas.Left="7.95596"
                                                     Canvas.Top="45.2328"
                                                     Width="14.8372"
                                                     Height="14.8372"
                                                     Fill="#39000000"
                                                     Stretch="Fill" />
                                            <Ellipse Canvas.Left="134.373"
                                                     Canvas.Top="97.5966"
                                                     Width="14.8372"
                                                     Height="14.8372"
                                                     Fill="#B3000000"
                                                     Stretch="Fill" />
                                            <Ellipse Canvas.Left="97.3466"
                                                     Canvas.Top="8.20599"
                                                     Width="14.8372"
                                                     Height="14.8372"
                                                     Fill="#F0000000"
                                                     Stretch="Fill" />
                                            <Ellipse Canvas.Left="44.9828"
                                                     Canvas.Top="134.623"
                                                     Width="14.8372"
                                                     Height="14.8372"
                                                     Fill="#76000000"
                                                     Stretch="Fill" />
                                            <Canvas.RenderTransform>
                                                <RotateTransform Angle="0" />
                                            </Canvas.RenderTransform>
                                            <Canvas.LayoutTransform>
                                                <ScaleTransform ScaleX="0.6" ScaleY="0.6" />
                                            </Canvas.LayoutTransform>
                                            <Canvas.Triggers>
                                                <EventTrigger RoutedEvent="ContentControl.Loaded">
                                                    <BeginStoryboard>
                                                        <Storyboard>
                                                            <DoubleAnimation Duration="0:0:02"
                                                                             From="0"
                                                                             RepeatBehavior="Forever"
                                                                             Storyboard.TargetProperty="(Canvas.RenderTransform).Angle"
                                                                             To="360" />
                                                        </Storyboard>
                                                    </BeginStoryboard>
                                                </EventTrigger>
                                            </Canvas.Triggers>
                                        </Canvas>
                                    </VisualBrush.Visual>
                                </VisualBrush>
                            </Rectangle.Fill>
                        </Rectangle>
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

谢谢。

1 个答案:

答案 0 :(得分:0)

使用(Style)parentGrid.TryFindResource("LoadingSpinner");

解决了问题

http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.findresource.aspx

  

如果为无法找到的密钥调用此方法,则会出现异常   被扔了。如果您不想处理由此产生的异常   调用FindResource,调用TryFindResource。 TryFindResource   当找不到请求的资源时返回null,而不是   抛出异常。

相关问题