这里有很大一部分问题,但通过进一步调查,我发现它与此无关。
我想从代码隐藏中更改Rectangle
dynamicaly的填充,因此我在资源字典中定义了canvas(它是来自包MahApps.Metro的Icons.xml)并应用于矩形填充图标。我创建了仅带矩形的空窗口:
<Window
x:Class="VKPlaylist.GUI.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
Height="300"
Width="300"
>
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Icons.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Rectangle Name="hurrdurr" HorizontalAlignment="Left" Height="190" Margin="53,37,0,0" VerticalAlignment="Top" Width="193">
<Rectangle.Resources>
<SolidColorBrush x:Key="BlackBrush" Color="Black" />
</Rectangle.Resources>
<Rectangle.Fill>
<VisualBrush Stretch="Fill" Visual="{StaticResource appbar_settings}" />
</Rectangle.Fill>
</Rectangle>
</Grid>
</Window>
Icons.xml条目如下所示:
<Canvas Width="48" Height="48" Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0" x:Key="appbar_add">
<Path Width="24" Height="24" Canvas.Left="12" Canvas.Top="12" Stretch="Fill" Fill="{DynamicResource BlackBrush}" Data="F1 M 22,12L 26,12L 26,22L 36,22L 36,26L 26,26L 26,36L 22,36L 22,26L 12,26L 12,22L 22,22L 22,12 Z " />
</Canvas>
从代码隐藏后的两次三次交换后,图标消失了。我用过这个测试:
public partial class Window1
{
public Window1()
{
InitializeComponent();
this.Loaded += (sender, args) =>
new Thread(() => {
Dispatcher.Invoke(new Action(() => hurrdurr.Fill = new VisualBrush(ResourceProvider.GetIcon("appbar_pause"))));
Thread.Sleep(1000);
Dispatcher.Invoke(new Action(() => hurrdurr.Fill = new VisualBrush(ResourceProvider.GetIcon("appbar_play"))));
Thread.Sleep(1000);
Dispatcher.Invoke(new Action(() => hurrdurr.Fill = new VisualBrush(ResourceProvider.GetIcon("appbar_pause")))); // rectangle became blank on this iteration
Thread.Sleep(1000);
Dispatcher.Invoke(new Action(() => hurrdurr.Fill = new VisualBrush(ResourceProvider.GetIcon("appbar_play"))));
}).Start();
}
}
internal static class ResourceProvider
{
private static ResourceDictionary Icons;
public static Canvas GetIcon(string key)
{
if (Icons == null)
{
Icons = new ResourceDictionary();
Icons.Source = new Uri("pack://application:,,,/VKPlaylist;component/Resources/Icons.xaml");
}
return Icons.Contains(key) ? Icons[key] as Canvas : null;
}
}
对我来说这看起来像个错误,但我不知道它是否是WPF的错误,或者是来自MahApps.Metro的Icons.xml中Fill="{DynamicResource BlackBrush}"
操作的后果