Skip to content

Instantly share code, notes, and snippets.

@wi7a1ian
wi7a1ian / ErrorHandler.hpp
Created March 18, 2019 11:01
Generic error handler for C++ API #cpp
class ErrorHandler
{
using Result = FooInterfaces::Result;
using IStatefulLog = FooInterfaces::IStatefulLog;
public:
static Result HandleException(IStatefulLog& log, const uchar* functionName, std::exception_ptr eptr)
{
Result result = Result::SUCCESS;
@wi7a1ian
wi7a1ian / InstantiatingViewModel.xaml
Last active March 18, 2019 11:33
Ways of instantiating viewmodel #csharp #wpf
<!-- This approach requires your view model to have a default (parameter-less) constructor. -->
<UserControl.DataContext>
<my:MyViewModel/>
</UserControl.DataContext>
@wi7a1ian
wi7a1ian / HandlingViewModelLifecycle.cs
Created March 18, 2019 11:31
Handling the view model life cycle #wpf #csharp
public partial MyUserControl()
{
InitializeComponent();
Model.Initialize(DI.Resolve<IMyService>());
Loaded += () => { Model.OnLoaded(); };
Unloaded += () => { Model.OnUnloaded(); };
}
public class MyViewModel
{
@wi7a1ian
wi7a1ian / DependncyPropertyExampleA.xaml
Last active March 18, 2019 12:47
Use dependency properties to pass parameters to a view #wpf #csharp
<MySubView Project="{Binding SelectedProject}" />
@wi7a1ian
wi7a1ian / BindableBase.cs
Last active March 18, 2019 13:02
BindableBase #wpf #csharp
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Product.App.ViewModels
public abstract class BindableBase : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Equals(storage, value)) {
return false;
@wi7a1ian
wi7a1ian / AsyncCommandBase.cs
Created March 18, 2019 13:36
AsyncCommandBase #wpf #csharp
public abstract class AsyncCommandBase : IAsyncCommand
{
public abstract bool CanExecute(object parameter);
public abstract Task ExecuteAsync(object parameter);
public async void Execute(object parameter)
{
await ExecuteAsync(parameter);
}
public event EventHandler CanExecuteChanged
{
@wi7a1ian
wi7a1ian / ValidationinMvvmExample.cs
Last active March 18, 2019 14:02
Implementing INotifyDataErrorInfo (more flexible than IDataErrorInfo) #wpf #csharp
public abstract class DomainObject : INotifyPropertyChanged,
INotifyDataErrorInfo
{
private ErrorsContainer<ValidationResult> errorsContainer =
new ErrorsContainer<ValidationResult>( x => this.RaiseErrorsChanged( x ) );
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public bool HasErrors
{
@wi7a1ian
wi7a1ian / ListCollectionViewExample.cs
Created March 18, 2019 14:04
Using ListCollectionView #wpf #csharp
public class MyViewModel : BindableBase
{
public ICollectionView Customers { get; private set; }
public MyViewModel( ObservableCollection<Customer> customers )
{
// Initialize the CollectionView for the underlying model
// and track the current selection.
Customers = new ListCollectionView( customers );
@wi7a1ian
wi7a1ian / CommonDialogService.cs
Created March 18, 2019 14:05
Define DialogService for ViewModels #wpf #csharp
public interface IDialogService {
MessageBoxResult ShowMessageBox(string messageBoxText, string caption = null, MessageBoxButton buttons = MessageBoxButton.OK,...);
}
public abstract class ViewModelBase : BindableBase {
public IDialogService DialogService => AppContext.Current.DialogService;
}
@wi7a1ian
wi7a1ian / ReturnTextStreamExample.cs
Last active March 19, 2019 08:28
Return text stream in ASP.NET Core Api #asp #csharp #core
[HttpGet]
//public async Task<FileStreamResult> GetTest()
public async Task<IActionResult> GetTest()
{
using (var stream = new MemoryStream(Encoding.ASCII.GetBytes("Hello World")))
{
return new FileStreamResult(stream, new MediaTypeHeaderValue("text/plain"))
{
FileDownloadName = "test.txt"
};