Skip to content

Commit

Permalink
Improving the stability of the IconBehavior, by using the IsLoaded an…
Browse files Browse the repository at this point in the history
…d Loaded event.

[release]
  • Loading branch information
Lakritzator committed Nov 2, 2016
1 parent 1634f3f commit a49fd9f
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
<HintPath>..\packages\Caliburn.Micro.3.0.1\lib\net45\Caliburn.Micro.Platform.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapplo.Addons, Version=0.3.86.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Addons.0.3.86\lib\net45\Dapplo.Addons.dll</HintPath>
<Reference Include="Dapplo.Addons, Version=0.3.87.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Addons.0.3.87\lib\net45\Dapplo.Addons.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapplo.Config, Version=0.3.54.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down
2 changes: 1 addition & 1 deletion Dapplo.CaliburnMicro.Demo.Addon/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<packages>
<package id="Caliburn.Micro" version="3.0.1" targetFramework="net452" />
<package id="Caliburn.Micro.Core" version="3.0.1" targetFramework="net452" />
<package id="Dapplo.Addons" version="0.3.86" targetFramework="net452" />
<package id="Dapplo.Addons" version="0.3.87" targetFramework="net452" />
<package id="Dapplo.Config" version="0.3.54" targetFramework="net452" />
<package id="Dapplo.InterfaceImpl" version="0.1.37" targetFramework="net452" />
<package id="Dapplo.Log.Facade" version="0.5.4" targetFramework="net452" />
Expand Down
8 changes: 4 additions & 4 deletions Dapplo.CaliburnMicro.Demo/Dapplo.CaliburnMicro.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
<HintPath>..\packages\Caliburn.Micro.3.0.1\lib\net45\Caliburn.Micro.Platform.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapplo.Addons, Version=0.3.86.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Addons.0.3.86\lib\net45\Dapplo.Addons.dll</HintPath>
<Reference Include="Dapplo.Addons, Version=0.3.87.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Addons.0.3.87\lib\net45\Dapplo.Addons.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapplo.Config, Version=0.3.54.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down Expand Up @@ -86,8 +86,8 @@
<HintPath>..\packages\MahApps.Metro.1.3.0\lib\net45\MahApps.Metro.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MahApps.Metro.IconPacks, Version=1.5.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MahApps.Metro.IconPacks.1.5.0\lib\net45\MahApps.Metro.IconPacks.dll</HintPath>
<Reference Include="MahApps.Metro.IconPacks, Version=1.6.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MahApps.Metro.IconPacks.1.6.0\lib\net45\MahApps.Metro.IconPacks.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Caliburn.Micro;
using Dapplo.CaliburnMicro.Configuration;
using Dapplo.CaliburnMicro.Demo.Languages;
Expand All @@ -39,7 +37,6 @@
using Dapplo.Config.Language;
using Dapplo.Log.Facade;
using MahApps.Metro.Controls.Dialogs;
using MahApps.Metro.IconPacks;

#endregion

Expand All @@ -50,10 +47,9 @@ namespace Dapplo.CaliburnMicro.Demo.UseCases.Configuration.ViewModels
/// It is a conductor where one item is active.
/// </summary>
[Export]
public class ConfigViewModel : Config<IConfigScreen>, IHaveIcon, IPartImportsSatisfiedNotification
public class ConfigViewModel : Config<IConfigScreen>, IPartImportsSatisfiedNotification
{
private static readonly LogSource Log = new LogSource();
private Control _icon;

[Import]
public ICoreTranslations CoreTranslations { get; set; }
Expand Down Expand Up @@ -91,16 +87,6 @@ public class ConfigViewModel : Config<IConfigScreen>, IHaveIcon, IPartImportsSat
[Import]
private IDialogCoordinator Dialogcoordinator { get; set; }

/// <summary>
/// Set the default config icon for the view
/// </summary>
public Control Icon => _icon ?? (_icon = new PackIconMaterial
{
Kind = PackIconMaterialKind.Settings,
Margin = new Thickness(10),
Spin = true
});

public void OnImportsSatisfied()
{
// automatically update the DisplayName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
xmlns:mahapps="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:behaviors="clr-namespace:Dapplo.CaliburnMicro.Behaviors;assembly=Dapplo.CaliburnMicro"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
Width="500" Height="400">
<behaviors:IconBehavior.Icon>
<iconPacks:PackIconMaterial Kind="Settings" Margin="10"/>
</behaviors:IconBehavior.Icon>
<mahapps:MetroWindow.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibility" />
</mahapps:MetroWindow.Resources>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ protected override void OnActivate()
// Set the title of the icon (the ToolTipText) to our IContextMenuTranslations.Title
this.BindDisplayName(ContextMenuTranslations, nameof(IContextMenuTranslations.Title));

var items = ContextMenuItems.ToList();
var items = new List<IMenuItem>();
items.AddRange(ContextMenuItems);
// Use IMenuItemProvider, if any
items.AddRange(ContextMenuItemProviders.SelectMany(itemProvider => itemProvider.ProvideMenuItems()));
items.Add(new MenuItem
{
Expand All @@ -95,7 +97,7 @@ protected override void OnActivate()
// Make sure the margin is set, do this AFTER the icon are set
items.ApplyIconMargin(new Thickness(2, 2, 2, 2));

WpfIconBehaviors.SetIcon(TrayIcon as FrameworkElement, new PackIconMaterial
IconBehavior.SetIcon(TrayIcon as FrameworkElement, new PackIconMaterial
{
Kind = PackIconMaterialKind.Apps,
Background = Brushes.White,
Expand Down
4 changes: 2 additions & 2 deletions Dapplo.CaliburnMicro.Demo/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<packages>
<package id="Caliburn.Micro" version="3.0.1" targetFramework="net452" />
<package id="Caliburn.Micro.Core" version="3.0.1" targetFramework="net452" />
<package id="Dapplo.Addons" version="0.3.86" targetFramework="net452" />
<package id="Dapplo.Addons" version="0.3.87" targetFramework="net452" />
<package id="Dapplo.Config" version="0.3.54" targetFramework="net452" />
<package id="Dapplo.InterfaceImpl" version="0.1.37" targetFramework="net452" />
<package id="Dapplo.Log.Facade" version="0.5.4" targetFramework="net452" />
<package id="Dapplo.Log.Loggers" version="0.5.4" targetFramework="net452" />
<package id="Dapplo.Utils" version="0.1.122" targetFramework="net452" />
<package id="Hardcodet.NotifyIcon.Wpf" version="1.0.8" targetFramework="net452" />
<package id="MahApps.Metro" version="1.3.0" targetFramework="net452" />
<package id="MahApps.Metro.IconPacks" version="1.5.0" targetFramework="net452" />
<package id="MahApps.Metro.IconPacks" version="1.6.0" targetFramework="net452" />
</packages>
3 changes: 2 additions & 1 deletion Dapplo.CaliburnMicro.Metro/MetroWindowManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ protected override Window EnsureWindow(object model, object view, bool isDialog)
var haveIcon = model as IHaveIcon;
if (haveIcon != null && window.Icon == null)
{
WpfIconBehaviors.SetIcon(window, haveIcon.Icon);
// Now ussu the attached behavior to set the icon
IconBehavior.SetIcon(window, haveIcon.Icon);
}
// Just in case, make sure it's activated
window.Activate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
<HintPath>..\packages\Caliburn.Micro.3.0.1\lib\net45\Caliburn.Micro.Platform.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapplo.Addons, Version=0.3.86.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Addons.0.3.86\lib\net45\Dapplo.Addons.dll</HintPath>
<Reference Include="Dapplo.Addons, Version=0.3.87.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Addons.0.3.87\lib\net45\Dapplo.Addons.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapplo.Log.Facade, Version=0.5.4.0, Culture=neutral, processorArchitecture=MSIL">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public FrameworkElement Icon
set
{
var taskbarIcon = TrayIcon as FrameworkElement;
WpfIconBehaviors.SetIcon(taskbarIcon, value);
IconBehavior.SetIcon(taskbarIcon, value);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Dapplo.CaliburnMicro.NotifyIconWpf/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<packages>
<package id="Caliburn.Micro" version="3.0.1" targetFramework="net452" />
<package id="Caliburn.Micro.Core" version="3.0.1" targetFramework="net452" />
<package id="Dapplo.Addons" version="0.3.86" targetFramework="net45" />
<package id="Dapplo.Addons" version="0.3.87" targetFramework="net45" />
<package id="Dapplo.Log.Facade" version="0.5.4" targetFramework="net45" />
<package id="Dapplo.Utils" version="0.1.122" targetFramework="net45" />
<package id="Hardcodet.NotifyIcon.Wpf" version="1.0.8" targetFramework="net45" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ namespace Dapplo.CaliburnMicro.Behaviors
{

/// <summary>
/// This class contains logic to assist in setting an Icon of e.g. Window or e.g. TaskbarIcon from a FrameworkElement
/// This class contains a behavior to assist in setting an Icon of a e.g. Window or TaskbarIcon with a FrameworkElement
/// </summary>
public static class WpfIconBehaviors
public static class IconBehavior
{
private static readonly LogSource Log = new LogSource();
/// <summary>
Expand All @@ -46,7 +46,28 @@ public static class WpfIconBehaviors
/// <param name="frameworkElement"></param>
public static void SetIcon(DependencyObject dependencyObject, FrameworkElement frameworkElement)
{
dependencyObject.SetValue(WpfIconProperty, frameworkElement);
// Just set the framework element if it's null or loaded
if (frameworkElement == null || frameworkElement.IsLoaded)
{
dependencyObject.SetValue(WpfIconProperty, frameworkElement);
return;
}

var targetFrameworkElement = dependencyObject as FrameworkElement;
if (targetFrameworkElement == null)
{
// Use the Loaded event of the supplied frameworkElement instead of the target
targetFrameworkElement = frameworkElement;
// We can't register the Loaded event on the target,
Log.Warn().WriteLine("Using a not loaded FrameworkElement on a target which doesn't support the Loaded event.");
}
var handlers = new RoutedEventHandler[1];
handlers[0] = (sender, args) =>
{
dependencyObject.SetValue(WpfIconProperty, frameworkElement);
targetFrameworkElement.Loaded -= handlers[0];
};
targetFrameworkElement.Loaded += handlers[0];
}

/// <summary>
Expand All @@ -63,7 +84,7 @@ public static void RefreshIcon(DependencyObject dependencyObject)
/// <summary>
/// Use to set an Icon with a FrameworkElement
/// </summary>
public static readonly DependencyProperty WpfIconProperty = DependencyProperty.RegisterAttached("WpfIconProperty", typeof(FrameworkElement), typeof(WpfIconBehaviors), new PropertyMetadata(null, PropertyChangedCallback));
public static readonly DependencyProperty WpfIconProperty = DependencyProperty.RegisterAttached("WpfIconProperty", typeof(FrameworkElement), typeof(IconBehavior), new PropertyMetadata(null, PropertyChangedCallback));

private static void SetIconInternally(DependencyObject dependencyObject, FrameworkElement icon)
{
Expand Down
14 changes: 7 additions & 7 deletions Dapplo.CaliburnMicro/Dapplo.CaliburnMicro.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@
<HintPath>..\packages\Caliburn.Micro.3.0.1\lib\net45\Caliburn.Micro.Platform.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapplo.Addons, Version=0.3.86.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Addons.0.3.86\lib\net45\Dapplo.Addons.dll</HintPath>
<Reference Include="Dapplo.Addons, Version=0.3.87.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Addons.0.3.87\lib\net45\Dapplo.Addons.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapplo.Addons.Bootstrapper, Version=0.3.86.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Addons.Bootstrapper.0.3.86\lib\net45\Dapplo.Addons.Bootstrapper.dll</HintPath>
<Reference Include="Dapplo.Addons.Bootstrapper, Version=0.3.87.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapplo.Addons.Bootstrapper.0.3.87\lib\net45\Dapplo.Addons.Bootstrapper.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Dapplo.Config, Version=0.3.54.0, Culture=neutral, processorArchitecture=MSIL">
Expand All @@ -71,8 +71,8 @@
<HintPath>..\packages\Dapplo.Utils.0.1.122\lib\net45\Dapplo.Utils.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MahApps.Metro.IconPacks, Version=1.5.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MahApps.Metro.IconPacks.1.5.0\lib\net45\MahApps.Metro.IconPacks.dll</HintPath>
<Reference Include="MahApps.Metro.IconPacks, Version=1.6.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MahApps.Metro.IconPacks.1.6.0\lib\net45\MahApps.Metro.IconPacks.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="PresentationCore" />
Expand All @@ -89,7 +89,7 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="Behaviors\WpfIconBehaviors.cs" />
<Compile Include="Behaviors\IconBehavior.cs" />
<Compile Include="CaliburnLogger.cs" />
<Compile Include="CaliburnMicroBootstrapper.cs" />
<Compile Include="CaliburnStartOrder.cs" />
Expand Down
6 changes: 3 additions & 3 deletions Dapplo.CaliburnMicro/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<packages>
<package id="Caliburn.Micro" version="3.0.1" targetFramework="net452" />
<package id="Caliburn.Micro.Core" version="3.0.1" targetFramework="net452" />
<package id="Dapplo.Addons" version="0.3.86" targetFramework="net45" />
<package id="Dapplo.Addons.Bootstrapper" version="0.3.86" targetFramework="net45" />
<package id="Dapplo.Addons" version="0.3.87" targetFramework="net45" />
<package id="Dapplo.Addons.Bootstrapper" version="0.3.87" targetFramework="net45" />
<package id="Dapplo.Config" version="0.3.54" targetFramework="net45" />
<package id="Dapplo.InterfaceImpl" version="0.1.37" targetFramework="net45" />
<package id="Dapplo.Log.Facade" version="0.5.4" targetFramework="net45" />
<package id="Dapplo.Utils" version="0.1.122" targetFramework="net45" />
<package id="MahApps.Metro.IconPacks" version="1.5.0" targetFramework="net45" />
<package id="MahApps.Metro.IconPacks" version="1.6.0" targetFramework="net45" />
</packages>

0 comments on commit a49fd9f

Please sign in to comment.