Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7b946d6

Browse files
author
Aether
committed
增加首页图片更换功能
- 增强ResetBannerImageCommand的错误处理和日志记录- 添加完整路径检查和备用恢复方案- 优化文件删除和默认图片恢复逻辑
1 parentd14de17 commit7b946d6

File tree

2 files changed

+138
-3
lines changed

2 files changed

+138
-3
lines changed

‎BetterGenshinImpact/View/Pages/HomePage.xaml‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Pagex:Class="BetterGenshinImpact.View.Pages.HomePage"
1+
<Pagex:Class="BetterGenshinImpact.View.Pages.HomePage"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:b="http://schemas.microsoft.com/xaml/behaviors"
@@ -26,10 +26,16 @@
2626
<StackPanelMargin="42,16,42,12">
2727

2828
<BorderHeight="200"CornerRadius="8">
29+
<Border.ContextMenu>
30+
<ContextMenu>
31+
<ui:MenuItemHeader="更换背景图片"Command="{Binding ChangeBannerImageCommand}"Icon="{ui:SymbolIcon Image24}" />
32+
<ui:MenuItemHeader="恢复默认图片"Command="{Binding ResetBannerImageCommand}"Icon="{ui:SymbolIcon ArrowCounterclockwise24}" />
33+
</ContextMenu>
34+
</Border.ContextMenu>
2935
<Grid>
3036
<BorderClipToBounds="True"CornerRadius="8">
3137
<Border.Background>
32-
<ImageBrushImageSource="pack://application:,,,/Resources/Images/banner.jpg"
38+
<ImageBrushImageSource="{Binding BannerImageSource}"
3339
RenderOptions.BitmapScalingMode="HighQuality"
3440
Stretch="UniformToFill" />
3541
</Border.Background>

‎BetterGenshinImpact/ViewModel/Pages/HomePageViewModel.cs‎

Lines changed: 130 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
usingBetterGenshinImpact.Core.Config;
1+
usingBetterGenshinImpact.Core.Config;
22
usingBetterGenshinImpact.Core.Monitor;
33
usingBetterGenshinImpact.Core.Recognition.ONNX;
44
usingBetterGenshinImpact.Core.Script;
@@ -20,6 +20,7 @@
2020
usingCommunityToolkit.Mvvm.Messaging.Messages;
2121
usingFischless.GameCapture;
2222
usingMicrosoft.Extensions.Logging;
23+
usingMicrosoft.Win32;
2324
usingSystem;
2425
usingSystem.Collections.Frozen;
2526
usingSystem.Collections.Generic;
@@ -35,8 +36,10 @@
3536
usingSystem.Windows.Controls;
3637
usingSystem.Windows.Interop;
3738
usingSystem.Windows.Media;
39+
usingSystem.Windows.Media.Imaging;
3840
usingWindows.System;
3941
usingWpf.Ui.Controls;
42+
usingWpf.Ui.Violeta.Controls;
4043

4144
namespaceBetterGenshinImpact.ViewModel.Pages;
4245

@@ -73,11 +76,18 @@ public partial class HomePageViewModel : ViewModel
7376
[ObservableProperty]
7477
privateInferenceDeviceType[]_inferenceDeviceTypes=Enum.GetValues<InferenceDeviceType>();
7578

79+
[ObservableProperty]
80+
privateImageSource_bannerImageSource;
81+
82+
privateconststringDefaultBannerImagePath="pack://application:,,,/Resources/Images/banner.jpg";
83+
privateconststringCustomBannerImagePath="UserData/Images/custom_banner.jpg";
84+
7685
publicHomePageViewModel(IConfigServiceconfigService,TaskTriggerDispatchertaskTriggerDispatcher)
7786
{
7887
_taskDispatcher=taskTriggerDispatcher;
7988
Config=configService.Get();
8089
ReadGameInstallPath();
90+
InitializeBannerImage();
8191

8292

8393
// WindowsGraphicsCapture 只支持 Win10 18362 及以上的版本 (Windows 10 version 1903 or later)
@@ -509,4 +519,123 @@ public void OnOpenHardwareAccelerationSettings()
509519
};
510520
varresult=dialogWindow.ShowDialog();
511521
}
522+
523+
#region 背景图片管理
524+
525+
privatevoidInitializeBannerImage()
526+
{
527+
try
528+
{
529+
// 检查是否存在自定义图片
530+
if(File.Exists(CustomBannerImagePath))
531+
{
532+
BannerImageSource=newBitmapImage(newUri(Path.GetFullPath(CustomBannerImagePath)));
533+
_logger.LogInformation("已加载自定义背景图片");
534+
}
535+
else
536+
{
537+
// 使用默认图片
538+
BannerImageSource=newBitmapImage(newUri(DefaultBannerImagePath,UriKind.Absolute));
539+
_logger.LogInformation("已加载默认背景图片");
540+
}
541+
}
542+
catch(Exceptionex)
543+
{
544+
_logger.LogError(ex,"初始化背景图片失败,使用默认图片");
545+
BannerImageSource=newBitmapImage(newUri(DefaultBannerImagePath,UriKind.Absolute));
546+
}
547+
}
548+
549+
[RelayCommand]
550+
privatevoidChangeBannerImage()
551+
{
552+
try
553+
{
554+
varopenFileDialog=newOpenFileDialog
555+
{
556+
Title="选择背景图片",
557+
Filter="图片文件|*.jpg;*.jpeg;*.png;*.bmp;*.gif|所有文件|*.*",
558+
Multiselect=false
559+
};
560+
561+
if(openFileDialog.ShowDialog()==true)
562+
{
563+
varselectedFile=openFileDialog.FileName;
564+
_logger.LogInformation("用户选择了图片: {ImagePath}",selectedFile);
565+
566+
// 确保目标目录存在
567+
vardirectory=Path.GetDirectoryName(CustomBannerImagePath);
568+
if(!string.IsNullOrEmpty(directory)&&!Directory.Exists(directory))
569+
{
570+
Directory.CreateDirectory(directory);
571+
_logger.LogInformation("创建了自定义图片目录: {Directory}",directory);
572+
}
573+
574+
// 复制图片到自定义路径
575+
File.Copy(selectedFile,CustomBannerImagePath,true);
576+
_logger.LogInformation("图片已复制到: {CustomPath}",CustomBannerImagePath);
577+
578+
// 更新UI
579+
BannerImageSource=newBitmapImage(newUri(Path.GetFullPath(CustomBannerImagePath)));
580+
Toast.Success("背景图片更换成功!");
581+
_logger.LogInformation("背景图片更换成功");
582+
}
583+
}
584+
catch(Exceptionex)
585+
{
586+
_logger.LogError(ex,"更换背景图片失败");
587+
Toast.Error($"更换背景图片失败:{ex.Message}");
588+
}
589+
}
590+
591+
[RelayCommand]
592+
privatevoidResetBannerImage()
593+
{
594+
try
595+
{
596+
// 获取自定义图片的完整路径
597+
varcustomImageFullPath=Path.GetFullPath(CustomBannerImagePath);
598+
_logger.LogInformation("尝试恢复默认背景图片,自定义图片路径: {CustomPath}",customImageFullPath);
599+
600+
if(File.Exists(customImageFullPath))
601+
{
602+
File.Delete(customImageFullPath);
603+
_logger.LogInformation("已删除自定义背景图片: {CustomPath}",customImageFullPath);
604+
}
605+
else
606+
{
607+
_logger.LogInformation("自定义背景图片不存在: {CustomPath}",customImageFullPath);
608+
}
609+
610+
// 恢复为默认图片
611+
_logger.LogInformation("正在恢复为默认背景图片,默认路径: {DefaultPath}",DefaultBannerImagePath);
612+
BannerImageSource=newBitmapImage(newUri(DefaultBannerImagePath,UriKind.Absolute));
613+
Toast.Success("已恢复为默认背景图片!");
614+
_logger.LogInformation("背景图片已恢复为默认");
615+
}
616+
catch(Exceptionex)
617+
{
618+
_logger.LogError(ex,"恢复默认背景图片失败,尝试使用备用方案");
619+
620+
// 备用方案:尝试重新创建默认图片的BitmapImage
621+
try
622+
{
623+
varbitmap=newBitmapImage();
624+
bitmap.BeginInit();
625+
bitmap.UriSource=newUri(DefaultBannerImagePath,UriKind.Absolute);
626+
bitmap.CacheOption=BitmapCacheOption.OnLoad;
627+
bitmap.EndInit();
628+
BannerImageSource=bitmap;
629+
Toast.Success("已恢复为默认背景图片!");
630+
_logger.LogInformation("使用备用方案恢复默认背景图片成功");
631+
}
632+
catch(ExceptionfallbackEx)
633+
{
634+
_logger.LogError(fallbackEx,"备用方案也失败");
635+
Toast.Error($"恢复默认背景图片失败:{ex.Message}");
636+
}
637+
}
638+
}
639+
640+
#endregion
512641
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp