zoukankan      html  css  js  c++  java
  • 简单的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>
  • 相关阅读:
    【bzoj题解】2186 莎拉公主的困惑
    【算法学习】整体二分
    【算法学习】【洛谷】cdq分治 & P3810 三维偏序
    【比赛游记】NOIP2017游记
    【0】如何在电脑中使用多个python版本【python虚拟环境配置】
    Mysql 安装服务无法启动解决方案与使用的一般使用指令
    4-urllib库添加代理,添加请求头格式 模板
    3-urllib的post请求方式
    02-urllib库的get请求方式
    01-urllib库添加headers的一般方法
  • 原文地址:https://www.cnblogs.com/hushzhang/p/5899358.html
Copyright © 2011-2022 走看看