Skip to content

Instantly share code, notes, and snippets.

View BrightShadow's full-sized avatar

BrightShadow BrightShadow

  • Poland
View GitHub Profile
@BrightShadow
BrightShadow / GalaSoftCommandCleaningInVM.cs
Created September 26, 2018 19:09
Example command cleaning to prevent memory leaks.
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
internal class TestViewModel : ViewModel
{
private RelayCommand myGreatCommand;
// ... some stuff here
public override void Cleanup()
@BrightShadow
BrightShadow / CommandBinding.xaml
Created September 26, 2018 19:01
Example binding of command in XAML
<Grid>
<Button Command="{Binding MyGreatCommand}" Content="Test"></Button>
</Grid>
@BrightShadow
BrightShadow / MyLeakingGratCommand1.cs
Last active September 26, 2018 19:02
Example WPF command with memory leak possibility.
public RelayCommand MyGreatCommand
{
get
{
return this.myGreatCommand ?? (
this.myGreatCommand = new RelayCommand(
() =>
{
// some code here
})
@BrightShadow
BrightShadow / WPFViewModelResolver.AddDataTemplate.cs
Created October 2, 2017 11:52
AddDataTemplate method for WPFViewModelResolver class.
private void AddDataTemplate(Type viewType, Type viewModelType)
{
DataTemplate dataTemplate = this.CreateTemplate(viewType, viewModelType);
if (!Application.Current.Resources.Contains(dataTemplate.DataTemplateKey))
{
Application.Current.Resources.Add(dataTemplate.DataTemplateKey, dataTemplate);
}
else
{
@BrightShadow
BrightShadow / WPFViewModelResolver.ResourceDictionary.xaml
Created October 2, 2017 11:15
WPF XAML Matching ViewModel to UserControl/View
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:v="clr-namespace:PP.Blog.Views"
xmlns:vm="clr-namespace:PP.Blog.ViewModels">
<DataTemplate DataType="{x:Type vm:HomeViewModel}">
<v:HomeView />
</DataTemplate>
@BrightShadow
BrightShadow / WPFViewModelResolver.CreateTemplate.cs
Created October 2, 2017 11:10
CreateTemplate method for WPFViewModelResolver class.
private DataTemplate CreateTemplate(Type viewType, Type viewModelType)
{
const string xamlTemplate =
"<DataTemplate DataType=\"{{x:Type xViewModels:{0}}}\"><xViews:{1}></xViews:{1}></DataTemplate>";
var xaml = String.Format(xamlTemplate, viewModelType.Name, viewType.Name);
var context = new ParserContext();
context.XamlTypeMapper = new XamlTypeMapper(new string[0]);
context.XamlTypeMapper.AddMappingProcessingInstruction("xViewModels", viewModelType.Namespace, viewModelType.Assembly.FullName);
context.XamlTypeMapper.AddMappingProcessingInstruction("xViews", viewType.Namespace, viewType.Assembly.FullName);
@BrightShadow
BrightShadow / WPFViewModelResolver-Method.cs
Created October 2, 2017 10:56
Class that allows to get rid of Locator in MVVM/WPF - automatic view models resolving in runtime
public void ConfigureViewModels(IUnityContainer container)
{
Assembly currentAssembly = Assembly.GetCallingAssembly();
IEnumerable<Type> allAssemblyTypes = currentAssembly.GetTypes();
foreach (var type in allAssemblyTypes)
{
if (this.ViewModelNamesOnly(type) != null)
{
Type viewType = this.GetMatchingViewType(type);
@BrightShadow
BrightShadow / WPFViewModelResolver.cs
Created October 2, 2017 10:43
Class that allows to get rid of Locator in MVVM/WPF - automatic view models resolving in runtime
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;
using System.Windows.Markup;
namespace PP.Blog.WpfViewModelsResolver
{
class ViewModelsResolver