简单的MVVM的实例

在Model层中建一个Person类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestPractice.Model
{
    public class Person
    {
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string age;
        public string Age
        {
            get { return age; }
            set { age = value; }
        }
        public Person(string name, string age)
        {
            this.Name = name;
            this.Age = age;
        }
    }
}

在Service层中建一个IPersonInformation,IQueryService接口和PersonInformation,QueryService类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestPractice.Model;

namespace TestPractice.Service
{
    interface IPersonInformation
    {
        List<Person> GetPersonInformation();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestPractice.Model;

namespace TestPractice.Service
{
    public class PersonInformation:IPersonInformation
    {
        public List<Person> GetPersonInformation()
        {
            List<Person> personList = new List<Person>();
            personList.Add(new Person("张学健", "19"));
            personList.Add(new Person("李四", "20"));
            personList.Add(new Person("王五", "21"));
            personList.Add(new Person("孙红雷", "22"));
            return personList;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestPractice.Model;

namespace TestPractice.Service
{
    interface IQueryService
    {
        Person Searching(string up);
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestPractice.Model;

namespace TestPractice.Service
{
    class QueryService : IQueryService
    {
        public Person Searching(string SearchText)
        {
            PersonInformation per = new PersonInformation();
            List<Person> Members = per.GetPersonInformation();

            Person p= new Person("","");

            if (!string.IsNullOrEmpty(SearchText))
            {
                foreach (var i in Members)
                {
                    if (i.Name.Contains(SearchText))
                    {
                        return i;
                    }
                }

            }
            return p;
        }
    }
}

在ViewModel中创建一个MainWindowViewModel类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestPractice.Model;

namespace TestPractice.Service
{
    class QueryService : IQueryService
    {
        public Person Searching(string SearchText)
        {
            PersonInformation per = new PersonInformation();
            List<Person> Members = per.GetPersonInformation();

            Person p= new Person("","");

            if (!string.IsNullOrEmpty(SearchText))
            {
                foreach (var i in Members)
                {
                    if (i.Name.Contains(SearchText))
                    {
                        return i;
                    }
                }

            }
            return p;
        }
    }
}

在MainWindow中:

<Window x:Class="TestPractice.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="公司人员查询系统" Height="350" Width="525"
        WindowStartupLocation="CenterScreen">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Border BorderBrush="Black" BorderThickness="1" CornerRadius="6" Padding="2" Grid.Row="0">
        <StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="公司人员查询系统" FontSize="20"  FontFamily="LiShu"  TextAlignment="Center" Width="517"/>
            </StackPanel>
        </StackPanel>
        </Border>
        <Border BorderBrush="Black" BorderThickness="1" CornerRadius="6" Padding="6" Grid.Row="1">
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="姓名:" FontSize="15"  FontFamily="LiShu" Height="20" />
                    <TextBox FontSize="15"  FontFamily="LiShu" Width="155" Text="{Binding SearchText,Mode=TwoWay}"/>
                </StackPanel>
            </StackPanel>
        </Border>
        <Border BorderBrush="Black" BorderThickness="1" CornerRadius="6" Padding="6" Grid.Row="2">
            <Button Content="查询" FontSize="15" Margin="255,0,167,242" Command="{Binding SeacheringCommand}"/>
        </Border>
        <Border BorderBrush="Black" BorderThickness="1" CornerRadius="6" Padding="6" Grid.Row="3">
            <StackPanel>
                <StackPanel Orientation="Horizontal" Height="270">
                    <TextBlock Text="姓名:" FontSize="15"  FontFamily="LiShu" Margin="10,89,0,0" Width="56" Height="31" VerticalAlignment="Top" />
                    <Label Content="{Binding P.Name,Mode=TwoWay}" FontSize="15" Width="150" Margin="0,77,0,161" RenderTransformOrigin="0.577,0.743"></Label>
                </StackPanel>
            </StackPanel>
        </Border>
        <Border BorderBrush="Black" BorderThickness="1" CornerRadius="6" Padding="6" Grid.Row="4">
            <StackPanel>
                <StackPanel Orientation="Horizontal" Height="270">
                    <TextBlock Text="年龄:" FontSize="15"  FontFamily="LiShu" Margin="10,164,0,75" Width="56" Height="31" RenderTransformOrigin="2.091,0.55" />
                    <Label Content="{Binding P.Age,Mode=TwoWay}" FontSize="15" Width="150" Margin="0,153,0,75" RenderTransformOrigin="0.577,0.743"></Label>
                </StackPanel>
            </StackPanel>
        </Border>
    </Grid>
</Window>
时间: 2024-12-22 11:14:58

简单的MVVM的实例的相关文章

JSP简单练习-数组应用实例

<%@ page contentType="text/html; charset=gb2312" %> <html> <body> <% int i,j,k; // 声明一个5行6列的二维数组 int intArray[][]=new int[5][6]; k=0; // 数组赋初始值 for(i=0;i<5;i++) { for(j=0;j<6;j++) { intArray[i][j]=k; k++; } } // 输出数组中的

简单的Ajax应用实例

从网页前端输入提示范围内的字符,然后显示从后台返回的结果 <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <script type="text/javascript"> function showHint(str) { var xmlhttp; if (str.length==0) {

230行实现一个简单的MVVM(转载)

https://zhuanlan.zhihu.com/p/24475845 作者:mirone链接:https://zhuanlan.zhihu.com/p/24475845来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 题图的PixivID为:pixiv-ID: 14402942,画师为RAHWIA 本文始发于我的博客,转载请注明作者. MVVM这两年在前端届掀起了一股热潮,火热的Vue和Angular带给了开发者无数的便利,本文将实现一个简单的MVVM,用20

230行实现一个简单的MVVM

作者:mirone链接:https://zhuanlan.zhihu.com/p/24451202来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. MVVM这两年在前端届掀起了一股热潮,火热的Vue和Angular带给了开发者无数的便利,本文将实现一个简单的MVVM,用200多行代码探索MVVM的秘密.您可以先点击本文的JS Bin查看效果,代码使用ES6,所以你可能需要转码. 什么是MVVM? MVVM是一种程序架构设计.把它拆开来看应该是Model-View-V

C#简单注册表操作实例

1.简介操作 //设置注册值 private void Button_Click(object sender, RoutedEventArgs e) { //路径及间隔符号要正确 //1.如果指定路径不存在,则创建 //2.如果指定键存在,则覆盖值 string path = "HKEY_CURRENT_USER\\myRegOne"; Registry.SetValue(path, "Expend", "hellow world!"); Mes

Java多线程之简单的线程同步实例

数据类: package Thread.MyCommon; public class Data { public int num = 0; public synchronized int getEven() { ++num; Thread.yield();//让另外线程先执行,加大测试效果几率 ++num; return num; } } 线程类: package Thread.MyCommon; public class myThread implements Runnable { priva

Linux下简单的socket通信实例

Linux下简单的socket通信实例 If you spend too much time thinking about a thing, you’ll never get it done. —Bruce Lee       学习网络编程也一段时间了,刚开始看<UNIX网络编程>的时候,觉得这本厚厚的书好难啊!看到后来,发现并没有想象中的那么难.如果你是新手,建议你看到第二部分结束后,开始着手写代码.不写代码肯定是不行的.看100遍也没有敲一遍实现一遍来的清楚.敲完以后,带着问题去看书,你会

一个简单的MVVM雏形

这是@尚春实现的MVVM,使用定时器轮询,只支持{{}}与input.value的修改. 这只能算是一个玩具,真正的MVVM需要有更复杂的扫描机制,JS解析器,双向绑定链什么的. <!DOCTYPE html> <html> <head> <script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script> <

C# 最简单的三层架构实例 ——转载自网易博客

代码        /// <summary>        /// 初始化登录名称.登录密码(Model类)        /// </summary>        private string adminUser = string.Empty; //设置用户名称为空值        private string adminPwd = string.Empty; //设置用户密码为空值        public string AdminUser        {