Skip to content

Commit

Permalink
Can add/override a custom resource for a window.
Browse files Browse the repository at this point in the history
Also updated sample app to show how.
  • Loading branch information
ashokgelal committed Mar 16, 2017
1 parent 1ab5f61 commit 0cdc4d1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 20 deletions.
18 changes: 17 additions & 1 deletion src/Magpie/Magpie.Example/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Reflection;
using System.Windows;
using MagpieUpdater.Interfaces;
using MagpieUpdater.Services;
using MagpieUpdater.Views;

namespace MagpieExample
{
Expand All @@ -24,7 +26,7 @@ public MainWindow()
InitializeComponent();
CurrentVersion.Content = "Current version: " + Assembly.GetEntryAssembly().GetName().Version;
SelectedChannel = 1;
_magpie = new Magpie(MakeAppInfo(SelectedChannel));
_magpie = new ExampleMagpie(MakeAppInfo(SelectedChannel));
_magpie.CheckInBackground();
}

Expand Down Expand Up @@ -64,4 +66,18 @@ private void DailyBuildChannel_OnClick(object sender, RoutedEventArgs e)
_magpie.SwitchSubscribedChannel(SelectedChannel);
}
}

public class ExampleMagpie : Magpie
{
public ExampleMagpie(AppInfo appInfo, IDebuggingInfoLogger debuggingInfoLogger = null, IAnalyticsLogger analyticsLogger = null)
: base(appInfo, debuggingInfoLogger, analyticsLogger)
{
}

protected override void SetOwner(Window window)
{
base.SetOwner(window);
window.AddCustomResource("_downloadNow", "What?");
}
}
}
3 changes: 2 additions & 1 deletion src/Magpie/Magpie/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
Margin="10"
HorizontalAlignment="Stretch"
LastChildFill="False">
<Button Click="SkipThisVersion_OnClick"
<Button MinWidth="150"
Click="SkipThisVersion_OnClick"
Content="{DynamicResource _skipVersion}"
DockPanel.Dock="Left"
IsCancel="True"
Expand Down
53 changes: 35 additions & 18 deletions src/Magpie/Magpie/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,8 @@ public MainWindow()
{
InitializeComponent();
SetValue(NoIconBehavior.ShowIconProperty, false);
AddCustomStyles();
}

private void AddCustomStyles()
{
try
{
var styles = string.Format("/{0};component/{1}", new AssemblyAccessor().AssemblyName, CustomStylesFilename);
var res = (ResourceDictionary) Application.LoadComponent(new Uri(styles, UriKind.Relative));
if (res != null)
{
Resources.MergedDictionaries.Add(res);
}
}
catch (Exception)
{
// no problem
}
var styles = string.Format("/{0};component/{1}", new AssemblyAccessor().AssemblyName, CustomStylesFilename);
this.AddCustomStyles(styles);
}

private void PoweredBy_RequestNavigate(object sender, RequestNavigateEventArgs e)
Expand Down Expand Up @@ -106,6 +90,39 @@ static void OnHtmlChanged(DependencyObject dependencyObject, DependencyPropertyC
}
}

public static class WindowExtension
{
public static bool AddCustomStyles(this Window window, string styleResourceFile)
{
try
{
var res = (ResourceDictionary)Application.LoadComponent(new Uri(styleResourceFile, UriKind.Relative));
if (res != null)
{
window.Resources.MergedDictionaries.Add(res);
}
return true;
}
catch (Exception)
{
return false;
}
}

public static bool AddCustomResource(this Window window, string key, string value)
{
try
{
window.Resources[key] = value;
return true;
}
catch (Exception)
{
return false;
}
}
}

internal class NoIconBehavior
{
private const int GwlExstyle = -20;
Expand Down

0 comments on commit 0cdc4d1

Please sign in to comment.