toast控件实现

This commit is contained in:
rmbgame
2021-03-08 10:31:39 +08:00
parent 3235f1d980
commit f42ca101c8
18 changed files with 143 additions and 151 deletions
@@ -90,5 +90,6 @@ namespace Microsoft.Extensions.DependencyInjection
services.AddSingleton<ISteamworksWebApiService, SteamworksWebApiServiceImpl>();
return services;
}
}
}
@@ -30,12 +30,10 @@ namespace System.Application.Services.Implementation
readonly IDesktopPlatformService platformService;
readonly string? mSteamDirPath;
readonly string? mSteamProgramPath;
readonly IToast toast;
readonly string[] steamProcess = new[] { "steam", "steamservice", "steamwebhelper" };
public SteamServiceImpl(IDesktopPlatformService platformService, IToast toast)
public SteamServiceImpl(IDesktopPlatformService platformService)
{
this.toast = toast;
this.platformService = platformService;
mSteamDirPath = platformService.GetSteamDirPath();
mSteamProgramPath = platformService.GetSteamProgramPath();
@@ -92,10 +90,7 @@ namespace System.Application.Services.Implementation
{
Process.Start(SteamProgramPath, arguments);
}
else
{
toast.Show(SR.SteamInstallPathNotFound);
} }
}
public string GetLastLoginUserName() => platformService.GetLastSteamLoginUserName();
@@ -212,7 +207,7 @@ namespace System.Application.Services.Implementation
{
if (string.IsNullOrWhiteSpace(UserVdfPath) || string.IsNullOrWhiteSpace(SteamDirPath))
{
toast.Show(SR.SteamInstallPathNotFound);
return;
}
else
{
@@ -229,7 +224,7 @@ namespace System.Application.Services.Implementation
{
if (string.IsNullOrWhiteSpace(UserVdfPath))
{
toast.Show(SR.SteamInstallPathNotFound);
return;
}
else
{
@@ -4,69 +4,86 @@ using System.Reactive.Subjects;
namespace System.Application.Services
{
/// <summary>
/// 提供对显示在主窗口底部的状态栏的访问。
/// </summary>
public class ToastService : ReactiveObject
{
#region static members
public static ToastService Current { get; } = new ToastService();
#endregion
/// <summary>
/// 提供对显示在主窗口底部的状态栏的访问。
/// </summary>
public class ToastService : ReactiveObject
{
#region static members
public static ToastService Current { get; } = new ToastService();
#endregion
private readonly Subject<string> notifier;
private string persisitentMessage = "";
private string notificationMessage;
private readonly Subject<string> notifier;
private string persisitentMessage = "";
private string notificationMessage = "";
#region Message 変更通知
#region Message 変更通知
/// <summary>
/// 获取指示当前状态的字符串。
/// </summary>
public string Message
{
get { return this.notificationMessage ?? this.persisitentMessage; }
set
{
if (this.persisitentMessage != value)
{
this.persisitentMessage = value;
this.RaisePropertyChanged();
}
}
}
/// <summary>
/// 获取指示当前状态的字符串。
/// </summary>
public string Message
{
get { return this.notificationMessage ?? this.persisitentMessage; }
set
{
this.notificationMessage = value;
this.persisitentMessage = value;
this.RaisePropertyChanged();
}
}
#endregion
#endregion
private ToastService()
{
this.notifier = new Subject<string>();
this.notifier
.Do(x =>
{
this.notificationMessage = x;
this.RaiseMessagePropertyChanged();
})
.Throttle(TimeSpan.FromMilliseconds(5000))
.Subscribe(_ =>
{
this.notificationMessage = null;
this.RaiseMessagePropertyChanged();
});
}
/// <summary>
/// 显示状态
/// </summary>
private bool _IsVisible;
public bool IsVisible
{
get => _IsVisible;
set => this.RaiseAndSetIfChanged(ref _IsVisible, value);
}
public void Set(string message)
{
this.Message = message;
}
private ToastService()
{
this.notifier = new Subject<string>();
this.notifier
.Do(x =>
{
this.notificationMessage = x;
this.RaiseMessagePropertyChanged();
})
.Throttle(TimeSpan.FromMilliseconds(5000))
.Subscribe(_ =>
{
this.notificationMessage = string.Empty;
this.RaiseMessagePropertyChanged();
});
public void Notify(string message)
{
this.notifier.OnNext(message);
}
this.WhenAnyValue(x => x.Message)
.Subscribe(x => IsVisible = !string.IsNullOrEmpty(x));
}
private void RaiseMessagePropertyChanged()
{
this.RaisePropertyChanged(nameof(this.Message));
}
}
public void Set(string message)
{
this.Message = message;
}
public void Notify(string message)
{
this.notifier.OnNext(message);
}
private void RaiseMessagePropertyChanged()
{
this.RaisePropertyChanged(nameof(this.Message));
}
public void CloseBtn_Click()
{
Set("");
IsVisible = false;
}
}
}
@@ -71,19 +71,14 @@ namespace System.Application.UI.Resx
/// <param name="cultureName"></param>
public static void ChangeLanguage(string cultureName)
{
//void ChangeLanguage()
//{
if (IsMatch(CultureInfo.CurrentUICulture, cultureName)) return;
// CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(cultureName);
if (string.IsNullOrWhiteSpace(cultureName))
AppResources.Culture = null;
AppResources.Culture = DefaultCurrentUICulture;
else
AppResources.Culture = CultureInfo.GetCultureInfo(Languages.SingleOrDefault(x => x.Key == cultureName).Key);
//AppResources.Culture = CultureInfo.GetCultureInfo(cultureName);
Current.Res = new AppResources();
Current.RaisePropertyChanged(nameof(Res));
//}
//MainThreadDesktop.BeginInvokeOnMainThread(ChangeLanguage);
}
public static string GetCurrentCultureSteamLanguageName()
@@ -11,13 +11,6 @@ namespace System.Application.UI.ViewModels
set => this.RaiseAndSetIfChanged(ref title, value);
}
ViewModelBase? _Toast;
public ViewModelBase? Toast
{
get => _Toast;
set => this.RaiseAndSetIfChanged(ref _Toast, value);
}
public bool IsInitialized { get; set; }
protected void InvokeOnUIDispatcher(Action action)
@@ -1,42 +0,0 @@
using ReactiveUI;
using System.Application.UI.Resx;
namespace System.Application.UI.ViewModels
{
public class ToastMessageViewModel : ViewModelBase
{
/// <summary>
/// 吐司消息标题
/// </summary>
private string? _Title;
public string? Title
{
get => _Title;
set => this.RaiseAndSetIfChanged(ref _Title, value);
}
/// <summary>
/// 消息
/// </summary>
private string? _Message;
public string? Message
{
get => _Message;
set => this.RaiseAndSetIfChanged(ref _Message, value);
}
/// <summary>
/// 显示状态
/// </summary>
private bool _IsVisible;
public bool IsVisible
{
get => _IsVisible;
set => this.RaiseAndSetIfChanged(ref _IsVisible, value);
}
}
}
@@ -1,4 +1,5 @@
using ReactiveUI;
using System.Application.Models;
using System.Application.Models.Settings;
using System.Application.UI.Resx;
using System.Collections.Generic;
@@ -78,6 +79,7 @@ namespace System.Application.UI.ViewModels
public async void Initialize()
{
//AppHelper.Current.Theme = (AppTheme)UISettings.Theme.Value;
if (!this.IsInitialized)
{
foreach (var item in this.TabItems)
@@ -27,7 +27,9 @@ namespace System.Application.UI.ViewModels
public void DebugButton_Click()
{
DebugString += "Test Command" + Environment.NewLine;
Services.ToastService.Current.Notify("Test CommandTest CommandTest CommandTest CommandTest CommandTest Command");
DebugString += Services.ToastService.Current.Message + Environment.NewLine;
DebugString += Services.ToastService.Current.IsVisible + Environment.NewLine;
}
}
}
@@ -1,14 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace System.Application.UI.ViewModels.Windows
{
public class MessageWindow : WindowViewModel
{
public MessageWindow()
{
Title = "";
}
}
}
@@ -0,0 +1,20 @@
using ReactiveUI;
namespace System.Application.UI.ViewModels.Windows
{
public class MessageWindowViewModel : WindowViewModel
{
public MessageWindowViewModel()
{
}
private string _Content;
public string Content
{
get => _Content;
set => this.RaiseAndSetIfChanged(ref _Content, value);
}
}
}