我将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;
}
}
xml:
<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>
谢谢你。