UWP自定義RadioButton實現Tab底部導航

来源:http://www.cnblogs.com/xiaocaidev/archive/2017/06/11/6984375.html
-Advertisement-
Play Games

先看效果: 參照Android的實現方式用RadioButton來實現,但是Uwp的RadioButton並沒有安卓的Selector選擇器 下麵是一個比較簡單的實現,如果有同學有更好的實現,歡迎留言,讓我們共同進步。 1、首先自定義一個RadioImageButton控制項,並定義幾個依賴屬性,代碼 ...


先看效果:

參照Android的實現方式用RadioButton來實現,但是Uwp的RadioButton並沒有安卓的Selector選擇器

下麵是一個比較簡單的實現,如果有同學有更好的實現,歡迎留言,讓我們共同進步。

1、首先自定義一個RadioImageButton控制項,並定義幾個依賴屬性,代碼如下

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using Windows.UI;
 5 using Windows.UI.Xaml;
 6 using Windows.UI.Xaml.Controls;
 7 using Windows.UI.Xaml.Media;
 8 
 9 namespace Demo.UWP.Controls
10 {
11     public class RadioImageButton : RadioButton
12     {
13         //預設圖片
14         public ImageSource Source
15         {
16             get { return (ImageSource)GetValue(SourceProperty); }
17             set { SetValue(SourceProperty, value); }
18         }
19 
20         // Using a DependencyProperty as the backing store for Source.  This enables animation, styling, binding, etc...
21         public static readonly DependencyProperty SourceProperty =
22             DependencyProperty.Register("Source", typeof(ImageSource), typeof(RadioImageButton), null);
23 
24         //選中圖片
25         public ImageSource SourceChecked
26         {
27             get { return (ImageSource)GetValue(SourceCheckedProperty); }
28             set { SetValue(SourceCheckedProperty, value); }
29         }
30 
31         // Using a DependencyProperty as the backing store for SourceChecked.  This enables animation, styling, binding, etc...
32         public static readonly DependencyProperty SourceCheckedProperty =
33             DependencyProperty.Register("SourceChecked", typeof(ImageSource), typeof(RadioImageButton), null);
34 
35         //選中文字顏色
36         public SolidColorBrush ForegroundChecked
37         {
38             get { return (SolidColorBrush)GetValue(ForegroundCheckedProperty); }
39             set { SetValue(ForegroundCheckedProperty, value); }
40         }
41 
42         // Using a DependencyProperty as the backing store for ForegroundChecked.  This enables animation, styling, binding, etc...
43         public static readonly DependencyProperty ForegroundCheckedProperty =
44             DependencyProperty.Register("ForegroundChecked", typeof(SolidColorBrush), typeof(RadioImageButton), new PropertyMetadata(new SolidColorBrush(Colors.Black)));
45 
46         //圖片寬度
47         public double ImageWidth
48         {
49             get { return (double)GetValue(ImageWidthProperty); }
50             set { SetValue(ImageWidthProperty, value); }
51         }
52 
53         // Using a DependencyProperty as the backing store for ImageWidth.  This enables animation, styling, binding, etc...
54         public static readonly DependencyProperty ImageWidthProperty =
55             DependencyProperty.Register("ImageWidth", typeof(double), typeof(RadioImageButton), new PropertyMetadata(50));
56 
57 
58 
59         public double ImageHeight
60         {
61             get { return (double)GetValue(ImageHeightProperty); }
62             set { SetValue(ImageHeightProperty, value); }
63         }
64 
65         // Using a DependencyProperty as the backing store for ImageHeight.  This enables animation, styling, binding, etc...
66         public static readonly DependencyProperty ImageHeightProperty =
67             DependencyProperty.Register("ImageHeight", typeof(double), typeof(RadioImageButton), new PropertyMetadata(50));
68 
69 
70 
71         public Thickness ImageMargin
72         {
73             get { return (Thickness)GetValue(ImageMarginProperty); }
74             set { SetValue(ImageMarginProperty, value); }
75         }
76 
77         // Using a DependencyProperty as the backing store for ImageMargin.  This enables animation, styling, binding, etc...
78         public static readonly DependencyProperty ImageMarginProperty =
79             DependencyProperty.Register("ImageMargin", typeof(Thickness), typeof(RadioImageButton), null);
80 
81 
82     }
83 }
View Code

 

2、使用Blend工具生成RadioButton的模板,並修改其中的Grid,刪除無用代碼,添加一個Image控制項,代碼如下

 1 <ResourceDictionary
 2     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5     xmlns:local="using:Demo.UWP"
 6     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 7     xmlns:mycontrols="using:Demo.UWP.Controls"
 8     mc:Ignorable="d">
 9     <Style x:Key="RadioImageButtonStyle1" TargetType="mycontrols:RadioImageButton">
10         <Setter Property="Background" Value="{ThemeResource RadioButtonBackground}" />
11         <Setter Property="Foreground" Value="{ThemeResource RadioButtonForeground}" />
12         <Setter Property="BorderBrush" Value="{ThemeResource RadioButtonBorderBrush}" />
13         <Setter Property="Padding" Value="0" />
14         <Setter Property="HorizontalAlignment" Value="Left" />
15         <Setter Property="VerticalAlignment" Value="Center" />
16         <Setter Property="HorizontalContentAlignment" Value="Center" />
17         <Setter Property="VerticalContentAlignment" Value="Top" />
18         <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
19         <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
20         <Setter Property="MinWidth" Value="0" />
21         <Setter Property="UseSystemFocusVisuals" Value="True" />
22         <Setter Property="FocusVisualMargin" Value="-7,-3,-7,-3" />
23         <Setter Property="Template">
24             <Setter.Value>
25                 <ControlTemplate TargetType="mycontrols:RadioImageButton">
26                     <Grid
27                         x:Name="RootGrid"
28                         Background="{TemplateBinding Background}"
29                         BorderBrush="{TemplateBinding BorderBrush}"
30                         BorderThickness="{TemplateBinding BorderThickness}">
31                         <Grid.RowDefinitions>
32                             <RowDefinition Height="auto" />
33                             <RowDefinition Height="*" />
34                         </Grid.RowDefinitions>
35                         <Image
36                             x:Name="ImageFront"
37                             Width="{TemplateBinding ImageWidth}"
38                             Height="{TemplateBinding ImageHeight}"
39                             Margin="{TemplateBinding ImageMargin}"
40                             HorizontalAlignment="Center"
41                             VerticalAlignment="Center"
42                             Source="{TemplateBinding Source}"
43                             Stretch="Uniform" />
44                         <ContentPresenter
45                             x:Name="ContentPresenter"
46                             Grid.Row="1"
47                             Margin="{TemplateBinding Padding}"
48                             HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
49                             VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
50                             AutomationProperties.AccessibilityView="Raw"
51                             Content="{TemplateBinding Content}"
52                             ContentTemplate="{TemplateBinding ContentTemplate}"
53                             ContentTransitions="{TemplateBinding ContentTransitions}"
54                             Foreground="{TemplateBinding Foreground}"
55                             TextWrapping="Wrap" />
56                         <VisualStateManager.VisualStateGroups>
57                             <VisualStateGroup x:Name="CheckStates">
58                                 <VisualState x:Name="Checked">
59                                     <VisualState.Setters>
60                                         <!--<Setter Target="ImageBack.Visibility" Value="Visible"/>
61                                         <Setter Target="ImageFront.Visibility" Value="Collapsed"/>-->
62                                         <Setter Target="ImageFront.Source" Value="{Binding SourceChecked, RelativeSource={RelativeSource TemplatedParent}}" />
63                                         <Setter Target="ContentPresenter.Foreground" Value="{Binding ForegroundChecked, RelativeSource={RelativeSource TemplatedParent}}" />
64                                     </VisualState.Setters>
65                                     <!--<Storyboard>
66                                         <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ImageBack" Storyboard.TargetProperty="Visibility">
67                                             <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
68                                         </ObjectAnimationUsingKeyFrames>
69                                         <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ImageFront" Storyboard.TargetProperty="Visibility">
70                                             <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed" />
71                                         </ObjectAnimationUsingKeyFrames>
72                                     </Storyboard>-->
73                                 </VisualState>
74                                 <VisualState x:Name="Unchecked" />
75                                 <VisualState x:Name="Indeterminate" />
76                             </VisualStateGroup>
77                         </VisualStateManager.VisualStateGroups>
78                     </Grid>
79                 </ControlTemplate>
80             </Setter.Value>
81         </Setter>
82     </Style>
83 </ResourceDictionary>
View Code

用VisualStateManager實現選中狀態的實現,56-77行代碼,這裡Setter的Value並不能用TemplateBinding進行綁定,點擊是會報一個Value的異常

 

3、下麵就開始使用了,直接上代碼

 1 <Grid>
 2                     <Grid.ColumnDefinitions>
 3                         <ColumnDefinition Width="auto" />
 4                         <ColumnDefinition Width="auto" />
 5                         <ColumnDefinition Width="auto" />
 6                         <ColumnDefinition Width="auto" />
 7                     </Grid.ColumnDefinitions>
 8                     <mycontrols:RadioImageButton
 9                         Grid.Row="0"
10                         Margin="10"
11                         Checked="RadioImageButton_Checked"
12                         Content="首頁"
13                         FontSize="12"
14                         FontWeight="Normal"
15                         ForegroundChecked="Orange"
16                         ImageHeight="40"
17                         ImageMargin="5"
18                         ImageWidth="40"
19                         Source="ms-appx:///Assets/Main/main_index_home_normal.png"
20                         SourceChecked="ms-appx:///Assets/Main/main_index_home_pressed.png"
21                         Style="{StaticResource RadioImageButtonStyle1}" />
22                     <mycontrols:RadioImageButton
23                         Grid.Column="1"
24                         Margin="10"
25                         Content="品質優惠"
26                         FontSize="12"
27                         FontWeight="Normal"
28                         ForegroundChecked="Orange"
29                         ImageHeight="40"
30                         ImageMargin="5"
31                         ImageWidth="40"
32                         Source="ms-appx:///Assets/Main/main_index_quality_normal.png"
33                         SourceChecked="ms-appx:///Assets/Main/main_index_quality_pressed.png"
34                         Style="{StaticResource RadioImageButtonStyle1}" />
35                     <mycontrols:RadioImageButton
36                         Grid.Column="2"
37                         Margin="10"
38                         Content="發現"
39                         FontSize="12"
40                         FontWeight="Normal"
41                         ForegroundChecked="Orange"
42                         ImageHeight="40"
43                         ImageMargin="5"
44                         ImageWidth="40"
45                         Source="ms-appx:///Assets/Main/main_index_search_normal.png"
46                         SourceChecked="ms-appx:///Assets/Main/main_index_search_pressed.png"
47                         Style="{StaticResource RadioImageButtonStyle1}" />
48                     <mycontrols:RadioImageButton
49                         Grid.Column="3"
50                         Margin="10"
51                         Content="我的"
52                         FontSize="12"
53                         FontWeight="Normal"
54                         ForegroundChecked="Orange"
55                         ImageHeight="40"
56                         ImageMargin="5"
57                         ImageWidth="40"
58                         Source="ms-appx:///Assets/Main/main_index_my_normal.png"
59                         SourceChecked="ms-appx:///Assets/Main/main_index_my_pressed.png"
60                         Style="{StaticResource RadioImageButtonStyle1}" />
61                 </Grid>
View Code
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 哈哈哈 哈哈哈 每個人都是獨立的個體,大白也不例外,仔細觀察大白有一個圓圓的頭加一對黑溜溜的眼睛,沒有腹肌的軀幹和一顆裸露的心臟,圓滾滾的肚子兩邊一對寬厚的臂膀,仔細看還有兩個萌萌噠小手指呢,最後一對粗的像大象一樣的大腿才能支撐這軟軟的肚子嘛,哈哈~ 因為大白是白的,所以我們可以定義一個深顏色的背景 ...
  • 最近在逛某個技術網站的時候,感覺文章關鍵詞上的樣式好酷炫啊,分頁的樣式。來張截圖: 你在首頁的底部也可以看到這樣一個分頁欄;是不是看上去還不錯?下麵就來看看這是如何實現的吧~ 第一種方法:利用border 第一種方法是藉助border屬性 hack 出三角形,然後通過一個矩形拼接兩個三角形最終製造出 ...
  • 因為工作原因,經常關註有關互聯網行業的最新動態。這不,剛送走了高考,又迎來了每年的畢業季,看到好多人都說今年的前端工作不好找,很多童鞋簡歷投了一大堆,也沒有回應,發現連實習的機會都沒有,好不容易去面試了幾次,發現到處都是培訓機構。最後,眼裡都是數不盡的迷茫,甚至都開始懷疑人生了有沒有? 如果你是想要 ...
  • 簡單的省市區三級聯動,適合初學者入門學習的案例 目錄結構如下: 三級聯動.html 跟 JS文件夾是同個級別 效果圖如下: HTML代碼: JS代碼: ...
  • 環境搭建 環境搭建可以參考RN官網,也可以參考中文版本:http://reactnative.cn/docs/0.45/getting-started.html 如果你希望可以看到原版的安裝流程,可以看官方的地址,本文只是我個人的實踐,並且僅限於window平臺。 官方的地址:https://fac ...
  • 一 start命令 ionic start sdscapp --type=ionic1 ...
  • UX瀏覽服務是為了加速瀏覽網頁而開發的瀏覽服務,它解決了WebView的一系列問題,它能夠在網路差的情況下快速的瀏覽,比webview快一倍以上,是webview的優化代替方案。它擁有完善的緩存管理策略,經過優化的載入順序,廣告攔截引擎。 這次更新我們修複大量問題: 1. 緩存加速、DNS加速、弱網 ...
  • 一、在app/src/main/res下有 AndroidManifest.xml打開,打開後如下圖1 二、日誌工具log log.v() log.d() log.i() log.w() log.e() 在下圖中 中存在輸出 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...