WIn_RustTools/RustTools/Views/HomePage.xaml.cs
2024-08-15 09:40:33 +08:00

156 lines
4.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Newtonsoft.Json;
using RustTools.DataList;
using RustTools.ViewModels;
using RustTools.WindowUI;
namespace RustTools.Views;
public sealed partial class HomePage : Page
{
public HomePageViewModel ViewModel
{
get;
}
public ObservableCollection<HomeBanner.Data> Pictures = new();
public HomePage()
{
ViewModel = App.GetService<HomePageViewModel>();
//轮播图
var lunbo = JsonConvert.DeserializeObject<HomeBanner>(ViewModel.lunbo);
if (lunbo != null)
{
foreach (var item in lunbo.data)
{
Pictures.Add(item);
}
}
InitializeComponent();
ListA.ItemClick += ListAClick;
ListB.ItemClick += ListAClick;
//为了节省流量测试的时候不加载这些
if (false)
{
ViewModel.random();
ViewModel.Latest();
}
}
private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
{
//var height = DisplayArea.Primary.WorkArea.Height;
//double a = height / 5;
//Debug.WriteLine(a);
//// 设置FlipView的高度
//Banner.Height = a;
}
public static void ListAClick(object sender, ItemClickEventArgs e)
{
if (e.ClickedItem is not ModListResponse.ModData item) { return; }
// 查找已存在的 ModulePage如果存在则激活它
var existingWindow = ModuleInfoWin.ModulePageList.FirstOrDefault(window => window.Mod.Id == item.Id);
if (existingWindow != null)
{
// 如果窗口已经存在,则激活它
existingWindow.Activate();
}
else
{
// 如果窗口不存在,则创建新窗口并添加到列表中
var newWindow = new ModuleInfoWin(item.Id);
//newWindow.Activated += (s, args) =>
//{
// // 当窗口被激活时,确保它在列表中
// if (!ModulePage.ModulePageList.Contains(newWindow))
// {
// ModulePage.ModulePageList.Add(newWindow);
// }
//};
newWindow.Activate();
ModuleInfoWin.ModulePageList.Add(newWindow);
}
}
private void RefreshRandom(object sender, RoutedEventArgs e)
{
ViewModel.random();
}
private void RefreshLatest(object sender, RoutedEventArgs e)
{
ViewModel.Latest();
}
private void Image_PointerPressed(object sender, Microsoft.UI.Xaml.Input.PointerRoutedEventArgs e)
{
// 获取点击的 Image 控件
var image = sender as Image;
if (image != null)
{
// 获取 Image 控件的绑定数据上下文
var item = image.DataContext as HomeBanner.Data;
var link1 = item?.link;
if (link1 != null)
{
// 使用正则表达式匹配大括号内的内容
var match = MyRegex().Match(link1);
if (match.Success)
{
var link = match.Groups[1].Value;
Process.Start(new ProcessStartInfo
{
FileName = link,
UseShellExecute = true,
Verb = "open"
});
}
else
{
Debug.WriteLine("No match found.");
}
}
}
}
[GeneratedRegex("\\{(.+?)\\}")]
private static partial Regex MyRegex();
private void Image_Tapped(object sender, Microsoft.UI.Xaml.Input.TappedRoutedEventArgs e)
{
// 获取点击的 Image 控件
var image = sender as Image;
if (image != null)
{
// 获取 Image 控件的绑定数据上下文
var item = image.DataContext as HomeBanner.Data;
var link1 = item?.link;
if (link1 != null)
{
// 使用正则表达式匹配大括号内的内容
var match = MyRegex().Match(link1);
if (match.Success)
{
var link = match.Groups[1].Value;
Process.Start(new ProcessStartInfo
{
FileName = link,
UseShellExecute = true,
Verb = "open"
});
}
else
{
Debug.WriteLine("No match found.");
}
}
}
}
}