基于ArcGIS Pro SDK的MVVM架构

示例结果展示

文件夹创建

相对于原始C#,少了Command文件夹里的类。该文件中的RelayCommand使用 ArcGIS.Desktop.Framework

Properties属性配置,主要用于设置执行程序路径(自带文件夹)

DarkImages用于存放深色图片(自带文件夹)

Images用于存放浅色图片(自带文件夹)

DataHelper存放测试数据,或者是从数据库读取到数据

Model用于存放类数据Student类等

View存放前端界面

ViewModel存放View和Model之间处理的事件及方法,属性。

代码

DataHelper

StudentDataHelper

using ProAppModuleMVVM.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProAppModuleMVVM.DataHelper
{
    internal class StudentDataHelper
    {
        public static ObservableCollection<Student> GetStudent()
        {
            ObservableCollection<Student> sampleStudent = new ObservableCollection<Student>();
            sampleStudent.Add(new Student() { Id = 0, TeacherId = 0, Name = "学生0", Age = 33 });
            sampleStudent.Add(new Student() { Id = 1, TeacherId = 0, Name = "学生1", Age = 22 });
            sampleStudent.Add(new Student() { Id = 2, TeacherId = 1, Name = "学生2", Age = 35 });
            sampleStudent.Add(new Student() { Id = 3, TeacherId = 0, Name = "学生3", Age = 27 });
            return sampleStudent;
        }
    }
}

TeacherDataHelper

using ArcGIS.Core.Data.UtilityNetwork.Trace;
using ProAppModuleMVVM.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProAppModuleMVVM.DataHelper
{
    internal class TeacherDataHelper
    {
        public static ObservableCollection<Teacher> GetTeacher()
        {
            ObservableCollection<Teacher> sampleTeacher = new ObservableCollection<Teacher>();
            sampleTeacher.Add(new Teacher() { Id = 0, Name = "老师0", Age = 33 });
            sampleTeacher.Add(new Teacher() { Id = 1, Name = "老师1", Age = 22 });
            sampleTeacher.Add(new Teacher() { Id = 2, Name = "老师2", Age = 35 });
            sampleTeacher.Add(new Teacher() { Id = 3, Name = "老师3", Age = 27 });
            return sampleTeacher;
        }
    }
}

Model

Student

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

namespace ProAppModuleMVVM.Model
{
    internal class Student
    {
        public long Id { get; set; }
        public long TeacherId { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

Teacher

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

namespace ProAppModuleMVVM.Model
{
    internal class Teacher
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public List<Student> Students { get; set; }
    }
}

View

文件夹分布

右键添加-》新建项目

ProWindow1.xaml

<controls:ProWindow x:Class="ProAppModuleMVVM.View.ProWindow1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:controls="clr-namespace:ArcGIS.Desktop.Framework.Controls;assembly=ArcGIS.Desktop.Framework"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:extensions="clr-namespace:ArcGIS.Desktop.Extensions;assembly=ArcGIS.Desktop.Extensions"
        xmlns:viewmodel="clr-namespace:ProAppModuleMVVM.ViewModel"
        xmlns:model="clr-namespace:ProAppModuleMVVM.Model"
        mc:Ignorable="d"
        Title="ProWindow1" Height="300" Width="300"
        WindowStartupLocation="CenterOwner"
    >
    <controls:ProWindow.DataContext>
        <viewmodel:ProWindow1ViewModel />
    </controls:ProWindow.DataContext>
    <controls:ProWindow.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <extensions:DesignOnlyResourceDictionary Source="pack://application:,,,/ArcGIS.Desktop.Framework;component\Themes\Default.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </controls:ProWindow.Resources>
    <Grid>
        <TreeView
                BorderThickness="0"
                ItemsSource="{Binding ResultList, Mode=TwoWay}"
                VirtualizingPanel.IsVirtualizing="True">
            <TreeView.Resources>
                <DataTemplate DataType="{x:Type model:Student}">
                    <StackPanel 
                            Orientation="Horizontal" 
                            Margin="2">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto"/>
                                <ColumnDefinition Width="auto"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Column="0" Text="{Binding Name, StringFormat=学生姓名:\{0\}}" Margin="5,1" />
                            <TextBlock Grid.Column="1" Text="{Binding Age, StringFormat=学生年龄:\{0\}}" Margin="5,1" />
                        </Grid>
                    </StackPanel>
                </DataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type model:Teacher}" ItemsSource="{Binding Students, Mode=OneWay}">
                    <StackPanel
                            Margin="2"
                            Orientation="Horizontal">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="auto" />
                                <ColumnDefinition Width="auto" />
                            </Grid.ColumnDefinitions>
                            <TextBlock
                                    Grid.Column="0"
                                    Margin="5,1"
                                    Text="{Binding Name, StringFormat=老师姓名:\{0\}}" />
                            <TextBlock
                                    Grid.Column="1"
                                    Margin="10,0"
                                    Text="{Binding Age, StringFormat=老师年龄:\{0\}}" />
                        </Grid>
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.Resources>
        </TreeView>
        <Button Content="删除老师0" Grid.Row="1" Height="20" Command="{Binding DeleteCommand}" Style="{DynamicResource Esri_SimpleButton}"></Button>
    </Grid>
</controls:ProWindow>

ShowProWindow1.cs该类是自动生成的,主要用于显示窗口,配合Config.daml使用

ViewModel

ProWindow1ViewModel

using ArcGIS.Desktop.Framework;
using ProAppModuleMVVM.DataHelper;
using ProAppModuleMVVM.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace ProAppModuleMVVM.ViewModel
{
    internal class ProWindow1ViewModel : INotifyPropertyChanged
    {
        #region Fields
        private string _searchText;
        private ObservableCollection<Teacher> _resultList;
        #endregion 

        #region Properties

        public ObservableCollection<Student> StudentList { get; private set; }
        public ObservableCollection<Teacher> TeacherList { get; private set; }

        // 查询关键字
        public string SearchText
        {
            get { return _searchText; }
            set
            {
                _searchText = value;
                RaisePropertyChanged("SearchText");
            }
        }

        // 查询结果
        public ObservableCollection<Teacher> ResultList
        {
            get { return _resultList; }
            set
            {
                _resultList = value;
                RaisePropertyChanged("ResultList");
            }
        }

        public ICommand DeleteCommand
        {
            get { return new RelayCommand(Deleting, CanDeleting); }
        }

        #endregion 

        #region Construction
        public ProWindow1ViewModel()
        {
            StudentList = StudentDataHelper.GetStudent();
            TeacherList = TeacherDataHelper.GetTeacher();
            foreach (Teacher teacher in TeacherList)
            {
                teacher.Students = new List<Student>();
                foreach (Student student in StudentList)
                {
                    if (student.TeacherId == teacher.Id)
                    {
                        teacher.Students.Add(student);
                    }
                }
            }
            _resultList = TeacherList;
        }

        #endregion

        #region Command Handler
        public void Deleting()
        {
            foreach (Teacher teacher in ResultList.ToList())
            {
                if (teacher.Id == 0)
                {
                    ResultList.Remove(teacher);
                }
            }
        }

        public bool CanDeleting()
        {
            return true;
        }

        #endregion 

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Methods
        private void RaisePropertyChanged(string propertyName)
        {
            // take a copy to prevent thread issues
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion 
    }
}

结果

参考文献

【C#】MVVM架构-CSDN博客

相关推荐

  1. Android MVVM+Clean架构简介

    2024-02-19 05:56:03       44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-19 05:56:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-19 05:56:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-19 05:56:03       82 阅读
  4. Python语言-面向对象

    2024-02-19 05:56:03       91 阅读

热门阅读

  1. 001:自动驾驶概述

    2024-02-19 05:56:03       35 阅读
  2. 【Ubuntu 22.04】解决Ubuntu 22.04终端行距过大的问题

    2024-02-19 05:56:03       56 阅读
  3. 图像处理与计算机视觉算法

    2024-02-19 05:56:03       42 阅读
  4. wsl内置Ubuntu使用 Dinky 与 Flink 集成

    2024-02-19 05:56:03       47 阅读
  5. [R] First Section Revision

    2024-02-19 05:56:03       43 阅读
  6. go内置库函数实现client与server数据的发送接收

    2024-02-19 05:56:03       53 阅读
  7. 【状态估计】深度传感器与深度估计算法(1/3)

    2024-02-19 05:56:03       48 阅读
  8. 关于怎么监督机器学习训练的进度

    2024-02-19 05:56:03       53 阅读
  9. 学习数据接构和算法的第10天

    2024-02-19 05:56:03       52 阅读