Skip to content

Commit

Permalink
Refactored to a common base class for all user controls
Browse files Browse the repository at this point in the history
Issue #14
  • Loading branch information
lprichar committed Apr 13, 2017
1 parent e8f660e commit c142178
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 64 deletions.
6 changes: 4 additions & 2 deletions SirenOfShame.Uwp.Ui/Controls/BuildDefinitions.xaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<UserControl
<controls:UserControlBase
x:Class="SirenOfShame.Uwp.Ui.Controls.BuildDefinition"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converters="using:SirenOfShame.Uwp.Ui.Converters"
xmlns:watcher="using:SirenOfShame.Uwp.Watcher.Watcher"
xmlns:controls="using:SirenOfShame.Uwp.Ui.Controls"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
Expand Down Expand Up @@ -62,4 +63,5 @@
>
</GridView>
</Grid>
</UserControl>
</controls:UserControlBase>

19 changes: 2 additions & 17 deletions SirenOfShame.Uwp.Ui/Controls/BuildDefinitions.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236
// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace SirenOfShame.Uwp.Ui.Controls
{
public sealed partial class BuildDefinition : UserControl
public sealed partial class BuildDefinition
{
public BuildDefinition()
{
Expand Down
6 changes: 4 additions & 2 deletions SirenOfShame.Uwp.Ui/Controls/Leaders.xaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<UserControl
<controls:UserControlBase
x:Class="SirenOfShame.Uwp.Ui.Controls.Leaders"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:models="using:SirenOfShame.Uwp.Ui.Models"
xmlns:converters="using:SirenOfShame.Uwp.Ui.Converters"
xmlns:controls="using:SirenOfShame.Uwp.Ui.Controls"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
Expand Down Expand Up @@ -74,4 +75,5 @@
>
</GridView>
</Grid>
</UserControl>
</controls:UserControlBase>

25 changes: 3 additions & 22 deletions SirenOfShame.Uwp.Ui/Controls/Leaders.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,18 @@
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls;
using SirenOfShame.Uwp.Ui.Views;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace SirenOfShame.Uwp.Ui.Controls
{
public sealed partial class Leaders
{
public Leaders()
{
this.InitializeComponent();
InitializeComponent();
}

private void ListViewBase_OnItemClick(object sender, ItemClickEventArgs e)
{
var page = FindParent<Page>();
page.Frame.Navigate(typeof(ViewUser));
}

private T FindParent<T>() where T : FrameworkElement
{
return FindParent<T>(this);
}

private T FindParent<T>(FrameworkElement element) where T : FrameworkElement
{
if (element == null) return default(T);
var parent = element.Parent as FrameworkElement;
var parentAsT = parent as T;
if (parentAsT != null)
return parentAsT;
return FindParent<T>(parent);
Navigate<ViewUser>();
}
}
}
6 changes: 4 additions & 2 deletions SirenOfShame.Uwp.Ui/Controls/News.xaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<UserControl
<controls:UserControlBase
x:Class="SirenOfShame.Uwp.Ui.Controls.News"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:models="using:SirenOfShame.Uwp.Ui.Models"
xmlns:converters="using:SirenOfShame.Uwp.Ui.Converters"
xmlns:controls="using:SirenOfShame.Uwp.Ui.Controls"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
Expand Down Expand Up @@ -51,4 +52,5 @@
>
</GridView>
</Grid>
</UserControl>
</controls:UserControlBase>

21 changes: 2 additions & 19 deletions SirenOfShame.Uwp.Ui/Controls/News.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace SirenOfShame.Uwp.Ui.Controls
namespace SirenOfShame.Uwp.Ui.Controls
{
public sealed partial class News : UserControl
public sealed partial class News
{
public News()
{
Expand Down
29 changes: 29 additions & 0 deletions SirenOfShame.Uwp.Ui/Controls/UserControlBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace SirenOfShame.Uwp.Ui.Controls
{
public class UserControlBase : UserControl
{
private T FindParent<T>() where T : FrameworkElement
{
return FindParent<T>(this);
}

private T FindParent<T>(FrameworkElement element) where T : FrameworkElement
{
if (element == null) return default(T);
var parent = element.Parent as FrameworkElement;
var parentAsT = parent as T;
if (parentAsT != null)
return parentAsT;
return FindParent<T>(parent);
}

protected void Navigate<T>()
{
var page = FindParent<Page>();
page.Frame.Navigate(typeof(T));
}
}
}
1 change: 1 addition & 0 deletions SirenOfShame.Uwp.Ui/SirenOfShame.Uwp.Ui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<Compile Include="Controls\BuildDefinitions.xaml.cs">
<DependentUpon>BuildDefinitions.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\UserControlBase.cs" />
<Compile Include="Controls\Leaders.xaml.cs">
<DependentUpon>Leaders.xaml</DependentUpon>
</Compile>
Expand Down

0 comments on commit c142178

Please sign in to comment.