Understanding Class Design, Object Orientation in C#

Asked 2 years ago, Updated 2 years ago, 37 views

C# is developing a WebBrowser for tabs, but I don't really understand class design and object orientation.
Create controls that inherit the current TabControl, TabPage, and WebBrowser controls and
On TabControl, I have a list type variable and manage drag movement, addition/deletion, alignment, etc. of tabs.
I have WebBrowse variables on TabPage, receive url on the constructor, receive browser back forward, pass them to webBrowser, receive the title of the currently displayed web page at WebBrowser event, and set it to Text on TabPage label
We only configure cookies on WebBrowser.
If I want to add a new function to restore the previously closed tab at the next boot, which class should I write if I am aware of object orientation and MVVM?
Also, if you have any design points, please let me know.

c#

2022-09-30 13:48

2 Answers

In MVVM, first define the class that represents the tab.

public sold class TabViewModel
{
    // Current URL, etc.
}

Then create a class to manage the tab list.This class is used to generate one instance per TabControl or Form to substitute a field or BindingContext.

public sold class MainViewModel
{
    private ObservableCollection <TabViewModel >_Tabs;

    publicObservableCollection<TabViewModel>Tabs
    {
        get
        {
            if(_Tabs==null)
            {
                _Tabs=new ObservableCollection<TabViewModel>();

                // TODO: Add tabs from last boot (asynchronous)
            }

            return_Tabs;
        }
    }
}

The implementation described above allows you to start loading tabs that are stored on your first access to Tabs.The ObservableCollection<T> used here also provides change notification events, and controls monitor CollectionChanged events to detect tab changes on the view model.

// Run on constructor, BindingContextChanged event, etc.
// MainViewModel vm;
vm.Tabs.CollectionChanged+=Tabs_CollectionChanged;

The CollectionChanged event tells you where to change the collection, so change the TabPage in the same way.The code below implements only resetting, but it actually works with all four other patterns.

private static void Tabs_CollectionChanged (object sender, NotifyCollectionChangedEventArgse)
{
    switch(e.Action)
    {
        caseNotifyCollectionChangedAction.Add:
            // TODO:handling when adding
            break;

        case NotifyCollectionChangedAction.Remove:
            // TODO:remove action
            break;

        caseNotifyCollectionChangedAction.Move:
            // TODO:handling during Move
            break;

        caseNotifyCollectionChangedAction.Replace:
            // TODO:processing during replacement
            break;
    }

    ResetTabs();
}

private static void ResetTabs()
{
    // TabControl tabControl;
    // MainViewModel vm;

    tabControl.TabPages.Clear();
    foreach (vart in vm.Tabs)
    {
        startp = new TabPage();

        // TODO:TabPage initialization

        tabControl.TabPages.Add(tp);
    }
}

All you have to do is increase the properties of TabViewModel/MainViewModel while adding changes on the view side, or manipulate the view model with the view side event handler.


2022-09-30 13:48

It's sensuous, but if I were to do it...
Create classes that deal with user settings and environments.The class should be referenced from the startup point and notified to TabCon.


2022-09-30 13:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.