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

Commit

Permalink
修改实现,保证无其它字符混入PixivID
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiichiamane committed Oct 31, 2019
1 parent 394cb4b commit c431bbf
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
42 changes: 40 additions & 2 deletions PixivFSUWP/SearchPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,44 @@
Storyboard.TargetProperty="Opacity"
From="0.0" To="1.0" Duration="0:0:0.2"/>
</Storyboard>
<Style x:Key="DigitOnlyAutoSuggestBox" TargetType="AutoSuggestBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="AutoSuggestBox">
<Grid x:Name="LayoutRoot">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="Orientation">
<VisualState x:Name="Landscape"/>
<VisualState x:Name="Portrait"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<TextBox x:Name="TextBox" ScrollViewer.BringIntoViewOnFocusChange="False" Description="{TemplateBinding Description}"
DesiredCandidateWindowAlignment="BottomEdge" Header="{TemplateBinding Header}" Margin="0"
PlaceholderText="{TemplateBinding PlaceholderText}" Style="{TemplateBinding TextBoxStyle}" InputScope="Digits"
UseSystemFocusVisuals="{TemplateBinding UseSystemFocusVisuals}" Width="{TemplateBinding Width}" Canvas.ZIndex="0"
TextChanging="style_TextBox_TextChanging" BeforeTextChanging="style_TextBox_BeforeTextChanging"/>
<Popup x:Name="SuggestionsPopup">
<Border x:Name="SuggestionsContainer">
<ListView x:Name="SuggestionsList" Background="{ThemeResource AutoSuggestBoxSuggestionsListBackground}"
BorderThickness="{ThemeResource AutoSuggestListBorderThemeThickness}"
BorderBrush="{ThemeResource AutoSuggestBoxSuggestionsListBorderBrush}"
DisplayMemberPath="{TemplateBinding DisplayMemberPath}" ItemContainerStyle="{TemplateBinding ItemContainerStyle}"
ItemTemplate="{TemplateBinding ItemTemplate}" IsItemClickEnabled="True"
ItemTemplateSelector="{TemplateBinding ItemTemplateSelector}" MaxHeight="{ThemeResource AutoSuggestListMaxHeight}"
Margin="{ThemeResource AutoSuggestListMargin}" Padding="{ThemeResource AutoSuggestListPadding}"/>
</Border>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<Grid>
<Frame x:Name="resultFrame"/>
Expand Down Expand Up @@ -80,8 +118,8 @@
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" HorizontalAlignment="Stretch">
<TextBlock x:Uid="GoToPixivID"/>
<AutoSuggestBox x:Name="asbGTPID" Margin="0 5 0 0" x:Uid="GoPixivID" QuerySubmitted="GoPixivID_QuerySubmitted"
HorizontalAlignment="Stretch" QueryIcon="Forward" TextChanged="asbGTPID_TextChanged"/>
<AutoSuggestBox Style="{StaticResource DigitOnlyAutoSuggestBox}" x:Name="asbGTPID" Margin="0 5 0 0" x:Uid="GoPixivID" QuerySubmitted="GoPixivID_QuerySubmitted"
HorizontalAlignment="Stretch" QueryIcon="Forward"/>
</StackPanel>
<Button Grid.Row="1" Margin="0,5,0,0" x:Name="btnSauceNAO" x:Uid="BtnSauceNAO" HorizontalAlignment="Stretch" Click="btnSauceNAO_Click" Visibility="Collapsed"/>
</Grid>
Expand Down
22 changes: 13 additions & 9 deletions PixivFSUWP/SearchPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private void BtnTag_Click(object sender, RoutedEventArgs e)

private async void btnSauceNAO_Click(object sender, RoutedEventArgs e)
{
const string sauceNAOAPI=null;
const string sauceNAOAPI = null;
const string imgurAPI = null;
string SAUCENAO_API_KEY, IMGUR_API_KEY;
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Expand All @@ -219,7 +219,7 @@ private async void btnSauceNAO_Click(object sender, RoutedEventArgs e)
{
Frame.Navigate(typeof(SettingsPage));
IMGUR_API_KEY = imgurAPI;
return;
return;
}
SAUCENAO_API_KEY = localSettings.Values["SauceNAOAPI"] as string;
IMGUR_API_KEY = localSettings.Values["ImgurAPI"] as string;
Expand Down Expand Up @@ -247,16 +247,20 @@ private void GoPixivID_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuery
{
Frame.Navigate(typeof(IllustDetailPage), Convert.ToInt32(asbGTPID.Text));
}
// 使Pixiv ID文本输入框始终保持纯数字
// 这里作用:出现非数字则删除最右侧一个字符
private void asbGTPID_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)

private void style_TextBox_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(asbGTPID.Text, "^\\d*\\.?\\d*$") && asbGTPID.Text != "")
{
asbGTPID.Text = asbGTPID.Text.Substring(0, asbGTPID.Text.Length - 1);
}
//IME输入不能触发BeforeTextChanging,我估计是个Bug
//只能在此确保绝对没有不是数字的东西混进来
sender.Text = new string(sender.Text.Where(char.IsDigit).ToArray());
}

private void style_TextBox_BeforeTextChanging(TextBox sender, TextBoxBeforeTextChangingEventArgs args)
{
args.Cancel = args.NewText.Any(c => !char.IsDigit(c));
}
}

static class StorageFileExt
{
/// <summary>
Expand Down

0 comments on commit c431bbf

Please sign in to comment.