添加主界面
This commit is contained in:
138
MainModule/ViewModels/PostDetailViewModel.cs
Normal file
138
MainModule/ViewModels/PostDetailViewModel.cs
Normal file
@@ -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<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