框架优化
This commit is contained in:
@@ -10,146 +10,16 @@ namespace MainModule.ViewModels
|
||||
{
|
||||
public class MainViewModel : NavigateViewModelBase
|
||||
{
|
||||
#region 属性
|
||||
|
||||
private ObservableCollection<PostEntity> _posts;
|
||||
public ObservableCollection<PostEntity> 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<IPostService>();
|
||||
_dialogService = containerProvider.Resolve<IDialogService>();
|
||||
InitializeCommands();
|
||||
LoadPosts();
|
||||
}
|
||||
|
||||
private void InitializeCommands()
|
||||
{
|
||||
CreatePostCommand = new DelegateCommand(OnCreatePost);
|
||||
SearchCommand = new AsyncDelegateCommand(OnSearch);
|
||||
CategorySelectCommand = new AsyncDelegateCommand<string>(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<PostEntity>(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<PostEntity>(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<PostEntity>(result.Data);
|
||||
}
|
||||
else
|
||||
{
|
||||
ShowErrorMessageBox("加载分类子失败", null);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowErrorMessageBox($"获取分类帖子异常: {ex.Message}", null);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,138 +0,0 @@
|
||||
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<IPostService>();
|
||||
_dialogService = containerProvider.Resolve<IDialogService>();
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user