简体   繁体   English

如何为多个视图模型实现相同的搜索功能?

[英]How do I implement the same search functionality for multiple view models?

I have a main view in which I have a tab control. 我有一个带有选项卡控件的主视图。 The content of each tab is a treeview which is present in different views. 每个选项卡的内容都是树视图,以不同的视图显示。 This is my main view in which I use 2 other views 这是我使用的另外2个视图的主要视图

In my FirstListView, I have a tree view, a textbox and a button. 在我的FirstListView中,我有一个树状视图,一个文本框和一个按钮。

<TabControl x:Name ="MainTab" SelectionChanged="OnTabSelectionChanged">
    <TabItem Header="First" >
        <view:FirstListView x:Name="FirstView"/>
    </TabItem>
    <TabItem  Header="Second" >
        <view:SecondListView x:Name ="SecondView"/>
    </TabItem>
</TabControl>

Textbox and the button are added to perform a search in the tree. 添加了文本框和按钮以在树中执行搜索。

The view model associated with the FirstListView has a command that is initialized in its contructor. 与FirstListView关联的视图模型具有在其构造函数中初始化的命令。

_searchCommand = new SearchFamilyTreeCommand(this);

SearchFamiltyTreeCommand is a class that is derived from ICommand and the execute method calls a function to perform the search. SearchFamiltyTreeCommand是从ICommand派生的类,execute方法调用一个函数来执行搜索。 This is present in the FirstViewModel. 这存在于FirstViewModel中。

#region SearchCommand

public ICommand SearchCommand
{
    get { return _searchCommand; }
}

private class SearchFamilyTreeCommand : ICommand
{
    readonly FunctionListViewModel _functionTree;

    public SearchFamilyTreeCommand(FunctionListViewModel functionTree)
    {
        _functionTree = functionTree;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    event EventHandler ICommand.CanExecuteChanged
    {
        add { }
        remove { }
    }

    public void Execute(object parameter)
    {
        _functionTree.PerformSearch();
    }
}
#endregion

The search method is not type independent. 搜索方法不是类型无关的。 It depends on the type present in its particular model. 这取决于其特定模型中的类型。 And the data required to perform the search is present in this view model. 并且执行搜索所需的数据存在于此视图模型中。

This is working. 可以了 Now I have to extend this functionality to other views ( SecondListView, ThirdListView and so on) which have their own treeviews(the type of the content is different from the FirstTreeView). 现在,我必须将此功能扩展到具有自己的树视图(内容类型与FirstTreeView不同)的其他视图(SecondListView,ThirdListView等)。 How can I do it? 我该怎么做? Where shall I place the code and the command? 我应该在哪里放置代码和命令?

Don't place business Logic into ViewModels. 不要将业务逻辑放入ViewModels中。 ViewModels should be only for presentation logic. ViewModels仅应用于表示逻辑。

Create a FamilyTreeSearchService and abstract it's functionality to this service, then inject the service into your ViewModels (ie Constructor, Dependency Injection, or ServiceLocator). 创建一个FamilyTreeSearchService并将其功能抽象到该服务,然后将该服务注入到您的ViewModels中(例如,构造函数,依赖项注入或ServiceLocator)。 Call the service from your ViewModels. 从您的ViewModels调用服务。

1) Proper way: 1)正确的方法:

Inherit your ViewModel classes directly from a common abstract base class. 直接从通用抽象基类继承ViewModel类。 Refer this Stackoverflow Answer 请参阅此Stackoverflow答案

2) Simple Way: 2)简单方法:

Have a separate class naming like 'CommonViewModel' and have common code in it. 有一个单独的类命名(如“ CommonViewModel”),并包含通用代码。 Inherit your additional ViewModel classes from the CommonViewModel ; CommonViewModel继承您的其他ViewModel类;

Like below, 像下面一样

public class CommonViewModel
{
....
}

public class FirstViewModel:CommonViewModel
{
....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何实现Facebook功能? - How do i implement facebook like functionality? 如何在MVC中为所有sql表实现搜索功能? - How do I implement search functionality for all my sql tables in MVC? 在一个视图中使用多个模型时,是否必须实现ViewModel? - Do I have to implement a ViewModel when using multiple Models in one View? 如何使用 SQLite 在 .NET MAUI 中实现搜索功能? - How can I implement a search functionality in .NET MAUI using SQLite? 如何在具有多个相同控件的表单上实现接口? - How do I implement an interface on a form with multiple of the same control? 如何在其他视图中多次使用具有相同功能的视图? - How to use View with the same functionality multiple times in other Views? 如果 Model 未实现 INotifyPropertyChanged,视图模型如何从其基础模型传播更改通知? - How do View Models propagate change notification from their underlying Models if the Model do not implement INotifyPropertyChanged? 如何为报表查看器实现文本搜索功能? - how to implement text search functionality for report viewer? 如何实现类似“语音”的搜索 - How do I implement a “phonetical”-like search 如何获得多个视图模型以查看布尔变量的变化? - How do I get multiple view models to see a change in a boolean variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM