From e872682d37464e239a4afb3f6eed448bd5efea45 Mon Sep 17 00:00:00 2001 From: iorebuild Date: Sun, 13 Oct 2024 23:47:03 +0800 Subject: [PATCH] day 2 --- 山海奇闻录/山海奇闻录/App.axaml | 1 + 山海奇闻录/山海奇闻录/Assets/Themes.axaml | 2 +- .../山海奇闻录/ViewModels/MainViewModel.cs | 141 +++++++++++++----- 山海奇闻录/山海奇闻录/Views/MainView.axaml | 107 ++++++++++--- 山海奇闻录/山海奇闻录/Views/MainView.axaml.cs | 123 ++++++++++++++- 5 files changed, 312 insertions(+), 62 deletions(-) diff --git a/山海奇闻录/山海奇闻录/App.axaml b/山海奇闻录/山海奇闻录/App.axaml index 9c6c1b0..4529bba 100644 --- a/山海奇闻录/山海奇闻录/App.axaml +++ b/山海奇闻录/山海奇闻录/App.axaml @@ -21,4 +21,5 @@ + \ No newline at end of file diff --git a/山海奇闻录/山海奇闻录/Assets/Themes.axaml b/山海奇闻录/山海奇闻录/Assets/Themes.axaml index 4787e42..ba46635 100644 --- a/山海奇闻录/山海奇闻录/Assets/Themes.axaml +++ b/山海奇闻录/山海奇闻录/Assets/Themes.axaml @@ -14,7 +14,7 @@ - + diff --git a/山海奇闻录/山海奇闻录/ViewModels/MainViewModel.cs b/山海奇闻录/山海奇闻录/ViewModels/MainViewModel.cs index 9d925d9..2e2b3e4 100644 --- a/山海奇闻录/山海奇闻录/ViewModels/MainViewModel.cs +++ b/山海奇闻录/山海奇闻录/ViewModels/MainViewModel.cs @@ -1,62 +1,125 @@ -using System.ComponentModel; -using System.Net.Http; -using System.Runtime.CompilerServices; -using System.Threading.Tasks; +using System; +using System.Net; +using System.IO; +using Avalonia; +using Avalonia.Animation; +using Avalonia.Controls; +using Avalonia.Media; using Avalonia.Media.Imaging; -using Avalonia.Threading; using CommunityToolkit.Mvvm.ComponentModel; namespace 山海奇闻录.ViewModels; public partial class MainViewModel : ViewModelBase { - //界面文字 [ObservableProperty] private string _AppTitle = "山海奇闻录"; - [ObservableProperty] private string _MyValue = "我的 / 上传"; + [ObservableProperty] private string _UpLoadTitle = "上传内容库"; + [ObservableProperty] private string _MyValue = "内容上传"; + [ObservableProperty] private string _CloseMyValue = "返回主页"; [ObservableProperty] private string _Friendly = "社交"; [ObservableProperty] private string _TextBook = "文案"; [ObservableProperty] private string _TalkPhotos = "图片"; -} -//加载logo -public partial class MainViewModel : INotifyPropertyChanged -{ - private Bitmap _LogoUrl; - public Bitmap LogoUrl - { - get => _LogoUrl; - set - { - _LogoUrl = value; - OnPropertyChanged(); - } - } + [ObservableProperty] private string _DebugText = "调试输出"; + + [ObservableProperty] private Bitmap? _LogoUrl; + [ObservableProperty] private Bitmap? _UpLoadUrl; + + [ObservableProperty] private Bitmap? _MainImage1; + [ObservableProperty] private Bitmap? _MainImage2; + [ObservableProperty] private Bitmap? _MainImage3; + + private readonly string _LogoUrlAddr = "https://shanhai.linuxacme.com/logo.png"; + private readonly string _UpLoadUrlAddr = "https://shanhai.linuxacme.com/logo.png"; + private readonly string _MainImage1Addr = "https://shanhai.linuxacme.com/image/2021102818050.jpg"; + private readonly string _MainImage2Addr = "https://shanhai.linuxacme.com/image/2021091109343.jpg"; + private readonly string _MainImage3Addr = "https://shanhai.linuxacme.com/image/210903/210903.jpg"; + // private static readonly HttpClient client = new HttpClient(); + public MainViewModel() { - LoadImageAsync(); + //加载icon + LoadIcon(_LogoUrlAddr); + LoadUpLoad(_UpLoadUrlAddr); + LoadMainImage1(_MainImage1Addr); + LoadMainImage2(_MainImage2Addr); + LoadMainImage3(_MainImage3Addr); } - - private async Task LoadImageAsync() + + private void LoadIcon(string url) { - var url = "https://shanhai.linuxacme.com/logo.png"; - using (var httpClient = new HttpClient()) + using (var webClient = new WebClient()) { - var imageData = await httpClient.GetByteArrayAsync(url); - var bitmap = new Bitmap(new System.IO.MemoryStream(imageData)); - - // 确保在 UI 线程上更新属性 - Dispatcher.UIThread.InvokeAsync(() => + byte[] imageBytes = webClient.DownloadData(url); + using (var memoryStream = new MemoryStream(imageBytes)) { - LogoUrl = bitmap; - }); + LogoUrl = new Bitmap(memoryStream); + } } } - - public event PropertyChangedEventHandler PropertyChanged; - - protected void OnPropertyChanged([CallerMemberName] string propertyName = null) + + private void LoadUpLoad(string url) { - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + using (var webClient = new WebClient()) + { + byte[] imageBytes = webClient.DownloadData(url); + using (var memoryStream = new MemoryStream(imageBytes)) + { + UpLoadUrl = new Bitmap(memoryStream); + } + } } -} \ No newline at end of file + + private void LoadMainImage1(string url) + { + using (var webClient = new WebClient()) + { + byte[] imageBytes = webClient.DownloadData(url); + using (var memoryStream = new MemoryStream(imageBytes)) + { + MainImage1 = new Bitmap(memoryStream); + } + } + } + + private void LoadMainImage2(string url) + { + using (var webClient = new WebClient()) + { + byte[] imageBytes = webClient.DownloadData(url); + using (var memoryStream = new MemoryStream(imageBytes)) + { + MainImage2 = new Bitmap(memoryStream); + } + } + } + + private void LoadMainImage3(string url) + { + using (var webClient = new WebClient()) + { + byte[] imageBytes = webClient.DownloadData(url); + using (var memoryStream = new MemoryStream(imageBytes)) + { + MainImage3 = new Bitmap(memoryStream); + } + } + } + + // private async Task Post(string data) + // { + // using (var client = new HttpClient()) + // { + // // 将字符串数据转化为 StringContent + // var content = new StringContent(data, Encoding.UTF8, "application/x-www-form-urlencoded"); + // + // // 发送请求 + // var response = await client.PostAsync("https://shanhai.linuxacme.com/Php/PushText.php", content); + // + // // 获取数据 + // var responseString = await response.Content.ReadAsStringAsync(); + // DebugText = responseString; + // } + // } +} diff --git a/山海奇闻录/山海奇闻录/Views/MainView.axaml b/山海奇闻录/山海奇闻录/Views/MainView.axaml index 47e60c7..dd30056 100644 --- a/山海奇闻录/山海奇闻录/Views/MainView.axaml +++ b/山海奇闻录/山海奇闻录/Views/MainView.axaml @@ -3,52 +3,84 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="clr-namespace:山海奇闻录.ViewModels" - mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" + mc:Ignorable="d" x:Class="山海奇闻录.Views.MainView" x:DataType="vm:MainViewModel"> - - + + + + + + + + + - + + Margin="10 0 0 0"/> - + Margin="0 0 15 0"/> - - + + + + + + + + + + + + + + + + + + + + + - + - - - - - - - + + + + + + +