Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
初步完成大图页面
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiichiamane committed Apr 21, 2019
1 parent db18d9e commit a483ee3
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 10 deletions.
25 changes: 25 additions & 0 deletions PixivFSUWP/BigImage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Page
x:Class="PixivFSUWP.BigImage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:PixivFSUWP"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" Loaded="Page_Loaded"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid x:Name="layoutRoot">
<ScrollViewer ZoomMode="Enabled"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto"
HorizontalScrollMode="Enabled"
VerticalScrollMode="Enabled">
<Grid>
<Image x:Name="mainImg"/>
<InkCanvas x:Name="mainCanvas"/>
</Grid>
</ScrollViewer>
</Grid>
</Page>
56 changes: 56 additions & 0 deletions PixivFSUWP/BigImage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.Core;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace PixivFSUWP
{
/// <summary>
/// 可用于自身或导航至 Frame 内部的空白页。
/// </summary>
public sealed partial class BigImage : Page
{
Data.BigImageDetail parameter;
public BigImage()
{
this.InitializeComponent();
}

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
parameter = e.Parameter as Data.BigImageDetail;
//CoreApplication.GetCurrentView().TitleBar.ExtendViewIntoTitleBar = true;
var view = ApplicationView.GetForCurrentView();
view.TitleBar.ButtonBackgroundColor = Colors.Transparent;
view.TitleBar.ButtonForegroundColor = Colors.White;
view.TitleBar.ButtonForegroundColor = Colors.Black;
view.TitleBar.ButtonInactiveForegroundColor = Colors.Gray;
view.Title = string.Format("{0} by {1} - {2}x{3}",
parameter.Title, parameter.Author,
parameter.Width, parameter.Height);
mainImg.Source = await Data.OverAll.BytesToImage(parameter.Image, parameter.Width, parameter.Height);
base.OnNavigatedTo(e);
}

private void Page_Loaded(object sender, RoutedEventArgs e)
{
mainImg.MaxHeight = Frame.ActualHeight;
mainImg.MaxWidth = Frame.ActualWidth;
}
}
}
18 changes: 18 additions & 0 deletions PixivFSUWP/Data/BigImageDetail.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Media.Imaging;

namespace PixivFSUWP.Data
{
public class BigImageDetail
{
public string Title { get; set; }
public string Author { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public byte[] Image { get; set; }
}
}
65 changes: 63 additions & 2 deletions PixivFSUWP/Data/OverAll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using PixivFS;
using PixivFSCS;
using Windows.ApplicationModel.Core;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Core;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;

namespace PixivFSUWP.Data
Expand All @@ -27,17 +35,70 @@ public static void RefreshRecommendList()
RecommendList = new RecommendIllustsCollection();
}

public static async Task<BitmapImage> LoadImageAsync(string Uri)
static async Task<MemoryStream> downloadImage(string Uri)
{
var toret = new BitmapImage();
var resStream = await Task.Run(() => new PixivAppAPI(GlobalBaseAPI).csfriendly_no_auth_requests_call_stream("GET",
Uri, new List<Tuple<string, string>>() { ("Referer", "https://app-api.pixiv.net/").ToTuple() })
.ResponseStream);
var memStream = new MemoryStream();
await resStream.CopyToAsync(memStream);
memStream.Position = 0;
return memStream;
}

public static async Task<BitmapImage> LoadImageAsync(string Uri)
{
var toret = new BitmapImage();
var memStream = await downloadImage(Uri);
await toret.SetSourceAsync(memStream.AsRandomAccessStream());
memStream.Dispose();
return toret;
}

public static async Task<WriteableBitmap> LoadImageAsync(string Uri, int Width, int Height)
{
var toret = new WriteableBitmap(Width, Height);
var memStream = await downloadImage(Uri);
await toret.SetSourceAsync(memStream.AsRandomAccessStream());
memStream.Dispose();
return toret;
}

public static async Task<byte[]> ImageToBytes(WriteableBitmap Source)
{
byte[] toret;
using (var stream = Source.PixelBuffer.AsStream())
{
toret = new byte[stream.Length];
await stream.ReadAsync(toret, 0, toret.Length);
}
return toret;
}

public static async Task<WriteableBitmap> BytesToImage(byte[] Source, int Width, int Height)
{
WriteableBitmap toret = new WriteableBitmap(Width, Height);
using (var stream = toret.PixelBuffer.AsStream())
{
await stream.WriteAsync(Source, 0, Source.Length);
}
return toret;
}

//展示一个新的窗口
public static async Task ShowNewWindow(Type Page, object Parameter)
{
CoreApplicationView newView = CoreApplication.CreateNewView();
int newViewId = 0;
await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
Frame frame = new Frame();
frame.Navigate(Page, Parameter);
Window.Current.Content = frame;
Window.Current.Activate();
newViewId = ApplicationView.GetForCurrentView().Id;
});
await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
}
}
}
6 changes: 5 additions & 1 deletion PixivFSUWP/Data/RecommendIllustsCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)

public void StopLoading()
{
if (_busy) _emergencyStop = true;
if (_busy)
{
_emergencyStop = true;
ResumeLoading();
}
}

public void PauseLoading()
Expand Down
15 changes: 12 additions & 3 deletions PixivFSUWP/IllustDetailPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,26 @@
ScrollViewer.HorizontalScrollBarVisibility="Hidden"
ScrollViewer.HorizontalScrollMode="Enabled"
ScrollViewer.VerticalScrollMode="Disabled"
SelectionMode="None" MaxHeight="1"
Margin="0,10,0,0" x:Name="ImageList">
SelectionMode="None" MaxHeight="1" IsItemClickEnabled="True"
Margin="0,10,0,0" x:Name="ImageList"
ItemClick="ImageList_ItemClick">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<controls:ImageSelectorPanel LeftOffset="15" RightOffset="15" ItemMargin="15"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate x:DataType="viewmodels:ImageItemViewModel">
<Grid Margin="-12,0">
<Grid Margin="-12,0" BorderThickness="1.5">
<Grid.BorderBrush>
<RevealBorderBrush Color="Transparent" FallbackColor="LightGray" />
</Grid.BorderBrush>
<Image Source="{Binding ImageSource}"/>
<Grid>
<Grid.Background>
<RevealBackgroundBrush Color="Transparent" FallbackColor="Transparent"/>
</Grid.Background>
</Grid>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
Expand Down
21 changes: 19 additions & 2 deletions PixivFSUWP/IllustDetailPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public sealed partial class IllustDetailPage : Page
int illustID;
Data.IllustDetail illust;

Task<WriteableBitmap> loadingTask = null;

public IllustDetailPage()
{
this.InitializeComponent();
Expand Down Expand Up @@ -62,15 +64,16 @@ private async Task loadContent()
txtBookmarkStatus.Text = illust.TotalBookmarks.ToString();
txtAuthor.Text = illust.Author;
txtAuthorAccount.Text = string.Format("@{0}", illust.AuthorAccount);
txtCaption.Text = illust.Caption.Replace("<br/>", "\n");
txtCaption.Text = illust.Caption.Replace("<br />", "\n");
int counter = 0;
foreach (var i in illust.OriginalUrls)
{
txtLoadingStatus.Text = string.Format("正在加载第 {0} 张,共 {1} 张", ++counter, illust.OriginalUrls.Count);
loadingTask = Data.OverAll.LoadImageAsync(i, 1, 1);
(ImageList.ItemsSource as ObservableCollection<ViewModels.ImageItemViewModel>)
.Add(new ViewModels.ImageItemViewModel()
{
ImageSource = await Data.OverAll.LoadImageAsync(i)
ImageSource = await Data.OverAll.LoadImageAsync(i, 1, 1)
});
}
txtLoadingStatus.Text = string.Format("共 {0} 张作品,点击或触摸查看大图", illust.OriginalUrls.Count);
Expand All @@ -80,5 +83,19 @@ private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
{
ImageList.MaxHeight = Frame.ActualHeight - 245;
}

private async void ImageList_ItemClick(object sender, ItemClickEventArgs e)
{

var Item = e.ClickedItem as ViewModels.ImageItemViewModel;
await Data.OverAll.ShowNewWindow(typeof(BigImage), new Data.BigImageDetail()
{
Title = illust.Title,
Width = Item.ImageSource.PixelWidth,
Height = Item.ImageSource.PixelHeight,
Author = illust.Author,
Image = await Data.OverAll.ImageToBytes(Item.ImageSource)
});
}
}
}
2 changes: 1 addition & 1 deletion PixivFSUWP/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public MainPage()
var view = ApplicationView.GetForCurrentView();
view.TitleBar.ButtonForegroundColor = Colors.Black;
view.TitleBar.ButtonInactiveForegroundColor = Colors.Gray;
view.Title = "Pixiv UWP";
view.Title = "";
}

private async void NavControl_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
Expand Down
8 changes: 8 additions & 0 deletions PixivFSUWP/PixivFSUWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,13 @@
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
</Compile>
<Compile Include="BigImage.xaml.cs">
<DependentUpon>BigImage.xaml</DependentUpon>
</Compile>
<Compile Include="Controls\ImageSelectorPanel.cs" />
<Compile Include="Controls\WaterfallContentPanel.cs" />
<Compile Include="Controls\WaterfallListView.cs" />
<Compile Include="Data\BigImageDetail.cs" />
<Compile Include="Data\IllustDetail.cs" />
<Compile Include="Data\RecommendIllustsCollection.cs" />
<Compile Include="Data\OverAll.cs" />
Expand Down Expand Up @@ -174,6 +178,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Page Include="BigImage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="IllustDetailPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down
2 changes: 1 addition & 1 deletion PixivFSUWP/ViewModels/ImageItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ namespace PixivFSUWP.ViewModels
{
public class ImageItemViewModel
{
public BitmapImage ImageSource { get; set; }
public WriteableBitmap ImageSource { get; set; }
}
}

0 comments on commit a483ee3

Please sign in to comment.