Created
October 9, 2019 04:32
-
-
Save danielplawgo/974d517c79c0e5249a245a0356da0bb5 to your computer and use it in GitHub Desktop.
Jak budować okno ustawień w aplikacji
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class AdminSettingsView : Window, ISettingsView | |
{ | |
public AdminSettingsView() | |
{ | |
InitializeComponent(); | |
} | |
public double OrderNumber => 10; | |
public bool CanShow(ApplicationContext context) | |
{ | |
return context.IsAdmin; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class App : Application | |
{ | |
protected IContainer BuildContainer() | |
{ | |
var builder = new ContainerBuilder(); | |
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()) | |
.AsImplementedInterfaces(); | |
var applicationContext = new ApplicationContext() { IsAdmin = true }; | |
builder.RegisterInstance(applicationContext) | |
.AsSelf(); | |
return builder.Build(); | |
} | |
protected override void OnStartup(StartupEventArgs e) | |
{ | |
base.OnStartup(e); | |
var container = BuildContainer(); | |
MainWindow = container.Resolve<ISettingsWindowWindow>() as Window; | |
MainWindow.Show(); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ApplicationContext | |
{ | |
public bool IsAdmin { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class ChangePasswordView : Window, ISettingsView | |
{ | |
public ChangePasswordView() | |
{ | |
Title = "Change Password"; | |
InitializeComponent(); | |
} | |
public double OrderNumber => 2; | |
public bool CanShow(ApplicationContext context) | |
{ | |
return true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class GeneralSettingsView : Window, ISettingsView | |
{ | |
public GeneralSettingsView() | |
{ | |
Title = "General Settings"; | |
InitializeComponent(); | |
} | |
public double OrderNumber => 1; | |
public bool CanShow(ApplicationContext context) | |
{ | |
return true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ISettingsView | |
{ | |
string Title { get; } | |
double OrderNumber { get; } | |
void Show(); | |
bool CanShow(ApplicationContext context); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class SettingsWindow : Window, ISettingsWindowWindow | |
{ | |
public ObservableCollection<ISettingsView> SettingsViews { get; set; } | |
public SettingsWindow(IEnumerable<ISettingsView> settingsViews, | |
ApplicationContext applicationContext) | |
{ | |
InitializeComponent(); | |
var processedSettingsViews = settingsViews | |
.Where(v => v.CanShow(applicationContext)) | |
.OrderBy(v => v.OrderNumber); | |
SettingsViews = new ObservableCollection<ISettingsView>(processedSettingsViews); | |
DataContext = this; | |
} | |
private void Button_Click(object sender, RoutedEventArgs e) | |
{ | |
var button = sender as Button; | |
if(button == null) | |
{ | |
return; | |
} | |
var view = button.DataContext as ISettingsView; | |
if(view == null) | |
{ | |
return; | |
} | |
view.Show(); | |
} | |
} | |
public interface ISettingsWindowWindow | |
{ | |
void Show(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="OpenClosePrinciple.SettingsWindow" | |
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:local="clr-namespace:OpenClosePrinciple" | |
mc:Ignorable="d" | |
Title="Open Close Principle" Height="450" Width="800"> | |
<Grid> | |
<ListBox ItemsSource="{Binding SettingsViews}" | |
HorizontalContentAlignment="Stretch"> | |
<ListBox.ItemTemplate> | |
<DataTemplate> | |
<Button Content="{Binding Title}" | |
Click="Button_Click" /> | |
</DataTemplate> | |
</ListBox.ItemTemplate> | |
</ListBox> | |
</Grid> | |
</Window> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment