Posts

How Dependency Injection Containers Work in C#?

Dependency Injection (DI) containers, such as Unity or DryIoc, help manage the creation and lifetime of object dependencies in C#. They facilitate the Inversion of Control (IoC) principle, allowing you to focus on writing clean, maintainable code without worrying about the complexities of instantiating dependencies manually. How DI Containers Work? Registration:  You define which concrete classes should be used to fulfill specific interface contracts. This allows the DI container to know what to instantiate when a class requests a particular dependency. Resolution:  When an instance of a class is requested, the DI container looks at the registered services, resolves the dependencies, and creates the object with the required dependencies injected. Lifetime Management:  The container manages the lifecycle of the dependencies. You can specify whether instances should be singleton (one instance for the entire application), transient (a new instance each time), or scoped (one ...

Easy way of navigate using Prism in WPF

Image
1. First.xaml <UserControl x:Class="PrismMain.Views.First"              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"              xmlns:local="clr-namespace:PrismMain.Views"              mc:Ignorable="d"              xmlns:prism="http://prismlibrary.com/"         prism:ViewModelLocator.AutoWireViewModel="True">     <StackPanel>           <Button Content="Navigate To Second" Margin="5"  Command="{Bin...

How to Create TabControl using Prism Region

Image
MainWindow <metro:MetroWindow x:Class="PrismDemo.Views.MainWindow"         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:PrismDemo.Views"         mc:Ignorable="d" xmlns:prism="http://prismlibrary.com/"         Title="MetroPrismDemo"         xmlns:metro="http://metro.mahapps.com/winfx/xaml/controls">     <Grid>         <TabControl Margin="5" prism:RegionManager.RegionName =" TabRegion "                   VerticalAlignment="Stretch" HorizontalAlignment="Left" Bord...

How to create TextBox in wpf that does not accept number using MVVM?

Image
Method 1: Using Model Property Inside View <TextBox Width="200" Text="{Binding MainWindowModel.NonNumeric,UpdateSourceTrigger=PropertyChanged}"> Inside Model  private string _nonNumeric;         public string NonNumeric         {             get { return _nonNumeric; }             set             {                 int intvalue;                 if (!int.TryParse(value[value.Length - 1].ToString(), out intvalue))                     SetProperty(ref _nonNumeric, value);                 else                     value = _nonNumeric;             }         } M...

How to create Own Semaphore?

 class CustomSemaphore     {         private Mutex[] arrMutex;         // place holder to store thread to mutex mapping         private Thread[] arrthread;         // number of threads allowed to access the resources         private int threadLimit;         // contructor creates the mutexes         public CustomSemaphore(int threadLimit)         {             this.threadLimit = threadLimit;             arrMutex = new Mutex[this.threadLimit];             arrthread = new Thread[this.threadLimit];             for (int i = 0; i < this.threadLimit; i++)             {                 arrMutex[i] = new Mutex()...

Write a method for division of two number without using division (‘/’) operator in C#.

    void DivideWithoutDivisionOperator(int divident,int divisior)         {             int quotient = 0;             bool isNegative = false;             if (divident < 0)//if negative             {                 divident = -divident;//make positive                 if (divisior < 0)//if negative                     divisior = -divisior;//make positive                 else                     isNegative = true;             }             else if (divisior < 0)//if negative             {      ...