Microsoft Technologies Routed Command এবং Custom Command তৈরি করা গাইড ও নোট

243

Routed Commands এবং Custom Commands WPF (Windows Presentation Foundation) এ কমান্ড প্যাটার্ন ব্যবহারের মাধ্যমে ইউজার ইন্টারফেসের বিভিন্ন কার্যাবলী পরিচালনা করতে ব্যবহৃত হয়। Routed Commands আপনাকে বিভিন্ন কন্ট্রোলের মধ্যে কমান্ড প্রেরণ ও গ্রহণ করতে সহায়তা করে, যেখানে Custom Commands আপনার অ্যাপ্লিকেশনের জন্য কাস্টম কমান্ড তৈরি করার সুযোগ দেয়।


Routed Command

Routed Command হল একটি ধরনের কমান্ড যা WPF এর ইভেন্ট সিস্টেমের মাধ্যমে একটি কমান্ডের ক্রিয়াকলাপ অন্য কোন কন্ট্রোল বা উইন্ডোতে প্রেরণ করে। এটি মূলত ICommand ইন্টারফেসের একটি উন্নত ফিচার এবং এর মাধ্যমে আপনি কমান্ডের প্রপাগেশন এবং হ্যান্ডলিং নিয়ন্ত্রণ করতে পারেন।

Routed Command এর মাধ্যমে আপনি কন্ট্রোলের InputBindings এবং CommandBindings ব্যবহার করে কমান্ডগুলি ইউজার ইন্টারফেসের অন্যান্য অংশে পাঠাতে পারেন।

Routed Command এর মৌলিক গঠন

  1. Command: Routed Command তৈরি করতে প্রথমে একটি কমান্ড ডেফাইন করতে হয়, যেমন SaveCommand, OpenCommand ইত্যাদি।
  2. InputBindings: এটি কিবোর্ড শর্টকাট বা মাউসের মাধ্যমে কমান্ড ট্রিগার করার জন্য ব্যবহৃত হয়।
  3. CommandBindings: এটি কমান্ডের জন্য ইভেন্ট হ্যান্ডলার সংযুক্ত করতে ব্যবহৃত হয়।

Routed Command উদাহরণ (Example of Routed Command)

  1. Command Declarations:

    App.xaml এ Routed Command ডেফাইন করুন:

    <Application x:Class="WPFApplication.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="MainWindow.xaml">
        <Application.Resources>
            <RoutedCommand x:Key="SaveCommand"/>
        </Application.Resources>
    </Application>
    
  2. UI (MainWindow.xaml):

    UI তে কমান্ড বাইন্ডিং এবং ইনপুট বাইন্ডিং যোগ করুন।

    <Window x:Class="WPFApplication.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Routed Command Example" Height="350" Width="525">
        <Grid>
            <Button Content="Save" Width="100" Height="50" Command="{Binding SaveCommand}"/>
        </Grid>
    </Window>
    
  3. Command Handler (MainWindow.xaml.cs):

    Routed Command এর জন্য ইভেন্ট হ্যান্ডলার সংযুক্ত করুন।

    public partial class MainWindow : Window
    {
        public RoutedCommand SaveCommand { get; set; }
    
        public MainWindow()
        {
            InitializeComponent();
            SaveCommand = (RoutedCommand)Application.Current.Resources["SaveCommand"];
    
            // CommandBinding
            CommandBinding saveCommandBinding = new CommandBinding(SaveCommand, ExecuteSaveCommand);
            this.CommandBindings.Add(saveCommandBinding);
    
            // InputBinding (Keyboard shortcut)
            InputBinding saveInputBinding = new InputBinding(SaveCommand, new KeyGesture(Key.S, ModifierKeys.Control));
            this.InputBindings.Add(saveInputBinding);
        }
    
        private void ExecuteSaveCommand(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Save command executed!");
        }
    }
    

এখানে, SaveCommand একটি RoutedCommand হিসেবে তৈরি হয়েছে এবং কমান্ডের জন্য একটি ExecutedRoutedEventArgs হ্যান্ডলারের মাধ্যমে "Save command executed!" বার্তা প্রদর্শন করা হয়েছে। কমান্ডের জন্য Ctrl + S কীবোর্ড শর্টকাটও সেট করা হয়েছে।


Custom Command

Custom Command হল এমন একটি কমান্ড যা আপনি নিজে তৈরি করতে পারেন এবং অ্যাপ্লিকেশনের বিশেষ কার্যাবলী বা লজিক প্রয়োগ করার জন্য ব্যবহার করতে পারেন। Custom Command তৈরির জন্য, আপনাকে ICommand ইন্টারফেস ইমপ্লিমেন্ট করতে হবে এবং তার পর সেগুলিকে UI উপাদানগুলির সাথে যুক্ত করতে হবে।

Custom Command এর মৌলিক গঠন

  1. ICommand Interface: Custom Command তৈরি করতে ICommand ইন্টারফেস ইমপ্লিমেন্ট করতে হয়।
  2. Command Handler: ICommand এর Execute এবং CanExecute মেথডের মাধ্যমে আপনি কমান্ডের লজিক পরিচালনা করবেন।

Custom Command উদাহরণ (Example of Custom Command)

  1. Custom Command Class:

    public class CustomCommand : ICommand
    {
        private readonly Action _execute;
        private readonly Func<bool> _canExecute;
    
        public CustomCommand(Action execute, Func<bool> canExecute)
        {
            _execute = execute;
            _canExecute = canExecute;
        }
    
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
    
        public bool CanExecute(object parameter)
        {
            return _canExecute == null || _canExecute();
        }
    
        public void Execute(object parameter)
        {
            _execute();
        }
    }
    
  2. UI (MainWindow.xaml):

    <Window x:Class="WPFApplication.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Custom Command Example" Height="350" Width="525">
        <Grid>
            <Button Content="Execute Custom Command" Width="200" Height="50" Command="{Binding MyCustomCommand}"/>
        </Grid>
    </Window>
    
  3. ViewModel and Command Binding (MainWindow.xaml.cs):

    public partial class MainWindow : Window
    {
        public ICommand MyCustomCommand { get; set; }
    
        public MainWindow()
        {
            InitializeComponent();
            MyCustomCommand = new CustomCommand(ExecuteCustomCommand, CanExecuteCustomCommand);
            this.DataContext = this; // Binding to the view's DataContext
        }
    
        private void ExecuteCustomCommand()
        {
            MessageBox.Show("Custom Command Executed!");
        }
    
        private bool CanExecuteCustomCommand()
        {
            return true; // Always return true, enabling the button
        }
    }
    

এখানে, CustomCommand ক্লাস ICommand ইন্টারফেস ইমপ্লিমেন্ট করেছে এবং বাটনের ক্লিক ইভেন্টে একটি কাস্টম কমান্ড তৈরি করেছে। CanExecuteCustomCommand মেথড দিয়ে আপনি নির্ধারণ করতে পারেন যে কমান্ডটি কখন চালানো যাবে এবং ExecuteCustomCommand মেথডে কমান্ডের কার্যাবলী সংজ্ঞায়িত করা হয়েছে।


Routed Command এবং Custom Command এর মধ্যে পার্থক্য

বৈশিষ্ট্যRouted CommandCustom Command
কমান্ড ব্যবহারের পরিধিUI উপাদানগুলোর মধ্যে প্রপাগেট করতে সক্ষমনির্দিষ্ট কার্যাবলীর জন্য অ্যাপ্লিকেশনের মধ্যে ব্যবহৃত হয়
ইভেন্ট হ্যান্ডলিংCommandBinding এবং InputBinding ব্যবহৃত হয়ICommand ইন্টারফেসের Execute এবং CanExecute মেথডের মাধ্যমে হ্যান্ডলিং
প্রচার (Routing)কমান্ডটি ঐ কন্ট্রোল থেকে অন্য কন্ট্রোলে প্রেরণ করা যেতে পারেশুধুমাত্র নির্দিষ্ট কমান্ডের জন্য কার্যকর

সারাংশ (Summary)

  • Routed Command: এটি একটি WPF কমান্ড যা UI উপাদানগুলোর মধ্যে প্রপাগেট করা যায় এবং InputBindingsCommandBindings এর মাধ্যমে পরিচালনা করা যায়।
  • Custom Command: এটি একটি কাস্টম কমান্ড যা ICommand ইন্টারফেসের মাধ্যমে তৈরি হয় এবং অ্যাপ্লিকেশনের জন্য বিশেষ কার্যাবলী তৈরি করতে ব্যবহৃত হয়।

এই দুটি কমান্ড প্যাটার্ন আপনাকে UI ইন্টারঅ্যাকশন এবং কার্যাবলীর নিয়ন্ত্রণ করতে সহায়তা করে, যেখানে Routed Command সাধারণভাবে UI-তে একাধিক কন্ট্রোলের মধ্যে প্রপাগেট করা যায় এবং Custom Command কাস্টম কার্যাবলীর জন্য ব্যবহৃত হয়।

Content added By
Promotion

Are you sure to start over?

Loading...