diff --git a/LAEPS/App.xaml.cs b/LAEPS/App.xaml.cs index c7bc9e3..b001676 100644 --- a/LAEPS/App.xaml.cs +++ b/LAEPS/App.xaml.cs @@ -1,19 +1,21 @@ -using LAEPS.ViewModels; +using Castle.DynamicProxy; +using Common; +using LAEPS.ViewModels; using LAEPS.ViewModels.Dialogs; using LAEPS.Views; -using LAEPS.Views.Dialogs; -using Castle.DynamicProxy; using LAEPS.Views; +using LAEPS.Views.Dialogs; +using Logger; +using Notifications.Wpf.Core; +using ORM; +using Service.Implement; +using Service.Interface; using System.Configuration; using System.Data; using System.Reflection; using System.Windows; -using static System.Runtime.InteropServices.JavaScript.JSType; using UIShare.PubEvent; -using Notifications.Wpf.Core; -using Logger; -using Common; -using ORM; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace LAEPS { @@ -69,6 +71,8 @@ namespace LAEPS // 注册通知管理器 INotificationManager NotificationManager = new NotificationManager(); containerRegistry.RegisterInstance(NotificationManager); + // 注册服务 + containerRegistry.RegisterSingleton(); } //指定模块加载方式(需要手动将模块生成的dll放入Modules文件夹中) protected override IModuleCatalog CreateModuleCatalog() @@ -77,17 +81,16 @@ namespace LAEPS return new DirectoryModuleCatalog() { ModulePath = @".\Modules" }; } //手动模块加载方式 - //protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog) - //{ - // Type moduleAType = typeof(LoginModule.LoginModule); - // moduleCatalog.AddModule(new ModuleInfo() - // { - // ModuleName = moduleAType.Name, - // ModuleType = moduleAType.AssemblyQualifiedName, - // InitializationMode = InitializationMode.OnDemand - // }); - - // base.ConfigureModuleCatalog(moduleCatalog); - //} - } + protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog) + { + Type moduleAType = typeof(MainModule.MainModule); + moduleCatalog.AddModule(new ModuleInfo() + { + ModuleName = moduleAType.Name, + ModuleType = moduleAType.AssemblyQualifiedName, + InitializationMode = InitializationMode.OnDemand + }); + base.ConfigureModuleCatalog(moduleCatalog); + } + } } diff --git a/LAEPS/ViewModels/ShellViewModel.cs b/LAEPS/ViewModels/ShellViewModel.cs index 553dcb3..1813bf0 100644 --- a/LAEPS/ViewModels/ShellViewModel.cs +++ b/LAEPS/ViewModels/ShellViewModel.cs @@ -41,12 +41,14 @@ namespace LAEPS.ViewModels private IRegionManager _regionManager; private IContainerProvider _containerProvider; private INotificationManager _notificationManager; + private IModuleManager _moduleManager; public ShellViewModel( IContainerProvider containerProvider) { _containerProvider= containerProvider; _eventAggregator = containerProvider.Resolve(); _regionManager = containerProvider.Resolve(); _notificationManager = containerProvider.Resolve(); + _moduleManager = containerProvider.Resolve(); LeftDrawerOpenCommand = new DelegateCommand(LeftDrawerOpen); MinimizeCommand = new DelegateCommand(MinimizeWindow); MaximizeCommand = new DelegateCommand(MaximizeWindow); @@ -65,6 +67,9 @@ namespace LAEPS.ViewModels private void Load() { _notificationManager.ShowAsync(new NotificationContent { Title = "登录成功", Message = "", Type = NotificationType.Success }); + //默认导航到主界面 + Type moduleAType = typeof(MainModule.MainModule); + _moduleManager.LoadModule(moduleAType.Name); } private void Navigate(string content) { diff --git a/MainModule/MainModule.cs b/MainModule/MainModule.cs index 43e3146..ebb7f4a 100644 --- a/MainModule/MainModule.cs +++ b/MainModule/MainModule.cs @@ -14,6 +14,7 @@ namespace MainModule public void RegisterTypes(IContainerRegistry containerRegistry) { containerRegistry.RegisterForNavigation("MainView"); + containerRegistry.RegisterForNavigation("PostDetailView"); } } } diff --git a/MainModule/MainModule.csproj b/MainModule/MainModule.csproj index 71a91c1..9902add 100644 --- a/MainModule/MainModule.csproj +++ b/MainModule/MainModule.csproj @@ -8,6 +8,8 @@ + + diff --git a/MainModule/ViewModels/MainViewModel.cs b/MainModule/ViewModels/MainViewModel.cs index acbd1d8..53e10b7 100644 --- a/MainModule/ViewModels/MainViewModel.cs +++ b/MainModule/ViewModels/MainViewModel.cs @@ -1,32 +1,155 @@ -using UIShare.ViewModelBase; +using Model.Entity; +using Prism.Commands; +using Prism.Ioc; +using Service.Interface; +using System.Collections.ObjectModel; +using System.Windows.Input; +using UIShare.ViewModelBase; namespace MainModule.ViewModels { public class MainViewModel : NavigateViewModelBase { #region 属性 - - - #endregion - #region 命令 - - - #endregion - - public MainViewModel(IContainerProvider containerProvider) : base(containerProvider) + private ObservableCollection _posts; + public ObservableCollection Posts { - + get => _posts; + set => SetProperty(ref _posts, value); } - + private string _searchKeyword; + public string SearchKeyword + { + get => _searchKeyword; + set => SetProperty(ref _searchKeyword, value); + } + + #endregion + + #region 命令 + + public ICommand CreatePostCommand { get; set; } + public ICommand SearchCommand { get; set; } + public ICommand CategorySelectCommand { get; set; } + + #endregion + #region 私有字段 + private IContainerProvider _containerProvider; + private IPostService _postService; + private IDialogService _dialogService; + #endregion + public MainViewModel(IContainerProvider containerProvider ) : base(containerProvider) + { + _containerProvider=containerProvider; + _postService = containerProvider.Resolve(); + _dialogService = containerProvider.Resolve(); + InitializeCommands(); + LoadPosts(); + } + + private void InitializeCommands() + { + CreatePostCommand = new DelegateCommand(OnCreatePost); + SearchCommand = new AsyncDelegateCommand(OnSearch); + CategorySelectCommand = new AsyncDelegateCommand(OnCategorySelect); + } #region 命令处理 + private async void OnCreatePost() + { + // 这里可以导航到发帖页面 + ShowInfoMessageBox("跳转到发帖页面",null); + } + private async Task OnSearch() + { + // 执行搜索逻辑 + if (!string.IsNullOrWhiteSpace(SearchKeyword)) + { + await SearchPostsAsync(SearchKeyword); + } + } + + private async Task OnCategorySelect(string category) + { + // 按分类筛选帖子 + await FilterPostsByCategoryAsync(category); + } #endregion + #region 业务方法 + private async Task LoadPosts() + { + try + { + var result = await _postService.GetRecommendedPostsAsync(20); + if (result.IsSuccess) + { + Posts = new ObservableCollection(result.Data); + } + else + { + ShowErrorMessageBox("帖子加载错误",null); + } + } + catch (Exception ex) + { + ShowErrorMessageBox($"加载帖子异常: {ex.Message}", null); + } + } + + private async Task SearchPostsAsync(string keyword) + { + try + { + var result = await _postService.SearchPostsAsync(keyword); + if (result.IsSuccess) + { + Posts = new ObservableCollection(result.Data); + } + else + { + ShowErrorMessageBox("查询帖子失败", null); + } + } + catch (Exception ex) + { + // 处理异常 + ShowErrorMessageBox($"查询帖子异常: {ex.Message}", null); + } + } + + private async Task FilterPostsByCategoryAsync(string category) + { + if (category == "全部") + { + await LoadPosts(); // 重新加载所有帖子 + return; + } + + try + { + var result = await _postService.GetPostsByCategoryAsync(category, 20); + if (result.IsSuccess) + { + Posts = new ObservableCollection(result.Data); + } + else + { + ShowErrorMessageBox("加载分类子失败", null); + } + } + catch (Exception ex) + { + ShowErrorMessageBox($"获取分类帖子异常: {ex.Message}", null); + } + } + + #endregion } } diff --git a/MainModule/ViewModels/PostDetailViewModel.cs b/MainModule/ViewModels/PostDetailViewModel.cs new file mode 100644 index 0000000..9effc65 --- /dev/null +++ b/MainModule/ViewModels/PostDetailViewModel.cs @@ -0,0 +1,138 @@ +using Model.Entity; +using Prism.Commands; +using Prism.Ioc; +using Service.Interface; +using System.Windows.Input; +using UIShare.ViewModelBase; + +namespace MainModule.ViewModels +{ + public class PostDetailViewModel : NavigateViewModelBase + { + #region 属性 + + private PostEntity _selectedPost; + public PostEntity SelectedPost + { + get => _selectedPost; + set => SetProperty(ref _selectedPost, value); + } + + #endregion + + #region 命令 + + public ICommand LikePostCommand { get; set; } + public ICommand FavoritePostCommand { get; set; } + public ICommand SharePostCommand { get; set; } + + #endregion + + #region 私有字段 + private IContainerProvider _containerProvider; + private IPostService _postService; + private IDialogService _dialogService; + #endregion + + public PostDetailViewModel(IContainerProvider containerProvider) : base(containerProvider) + { + _containerProvider = containerProvider; + _postService = containerProvider.Resolve(); + _dialogService = containerProvider.Resolve(); + InitializeCommands(); + } + + private void InitializeCommands() + { + LikePostCommand = new DelegateCommand(OnLikePost); + FavoritePostCommand = new DelegateCommand(OnFavoritePost); + SharePostCommand = new DelegateCommand(OnSharePost); + } + + #region 命令处理 + + private async void OnLikePost() + { + if (SelectedPost != null) + { + SelectedPost.LikeCount++; + // 在实际应用中,这里应该调用服务更新数据库 + var result = await _postService.UpdatePostAsync(SelectedPost); + if (!result.IsSuccess) + { + var dialogParams = new DialogParameters(); + dialogParams.Add("Title", "错误"); + dialogParams.Add("Message", "点赞失败" + result.Msg); + dialogParams.Add("Icon", "error"); + dialogParams.Add("ShowOk", true); + _dialogService.ShowDialog("MessageBox", dialogParams); + } + } + } + + private async void OnFavoritePost() + { + if (SelectedPost != null) + { + var dialogParams = new DialogParameters(); + dialogParams.Add("Title", "提示"); + dialogParams.Add("Message", "帖子已收藏"); + dialogParams.Add("Icon", "info"); + dialogParams.Add("ShowOk", true); + _dialogService.ShowDialog("MessageBox", dialogParams, result => + { + if (result.Result == ButtonResult.OK) + { + // 用户点击了确定 + } + }); + } + } + + private async void OnSharePost() + { + if (SelectedPost != null) + { + var dialogParams = new DialogParameters(); + dialogParams.Add("Title", "提示"); + dialogParams.Add("Message", "分享功能待实现"); + dialogParams.Add("Icon", "info"); + dialogParams.Add("ShowOk", true); + _dialogService.ShowDialog("MessageBox", dialogParams, result => + { + if (result.Result == ButtonResult.OK) + { + // 用户点击了确定 + } + }); + } + } + + #endregion + + #region 业务方法 + + public async void LoadPostDetails(PostEntity post) + { + if (post != null) + { + SelectedPost = post; + // 增加浏览量 + SelectedPost.ViewCount++; + // 在实际应用中,这里应该调用服务更新数据库 + var result = await _postService.UpdatePostAsync(SelectedPost); + if (!result.IsSuccess) + { + var dialogParams = new DialogParameters(); + dialogParams.Add("Title", "错误"); + dialogParams.Add("Message", "加载帖子详情失败" + result.Msg); + dialogParams.Add("Icon", "error"); + dialogParams.Add("ShowOk", true); + _dialogService.ShowDialog("MessageBox", dialogParams); + } + } + } + + #endregion + } +} \ No newline at end of file diff --git a/MainModule/Views/MainView.xaml b/MainModule/Views/MainView.xaml index 178e897..c275540 100644 --- a/MainModule/Views/MainView.xaml +++ b/MainModule/Views/MainView.xaml @@ -4,12 +4,219 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:i="http://schemas.microsoft.com/xaml/behaviors" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" + xmlns:converters="clr-namespace:UIShare.Converters;assembly=UIShare" mc:Ignorable="d" xmlns:prism="http://prismlibrary.com/" prism:ViewModelLocator.AutoWireViewModel="True" d:DesignHeight="1080" d:DesignWidth="1920"> - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +