目的
學習重點
專案原始碼
專案使用到的圖片
無
實作方法
建立專案
-
-
-
-
-
-
-
-
-
-
開發前準備工作
建立資料夾
- 請在核心PCL 專案內,建立一個
UserControls
資料夾
複製專案使用的圖片檔案
無
專案開發
建立 CustDialogUserControl 使用者控制項 (User COntrol)
-
-
-
-
CustDialogUserControl.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CustomDiag.UserControls.CustDialogUserControl">
<Grid
HorizontalOptions="Fill" VerticalOptions="Fill"
>
<BoxView
BackgroundColor="#BB000000"
/>
<BoxView
BackgroundColor="#FFFFFFFF"
Margin="20,70"
/>
<StackLayout
Margin="40,80"
HorizontalOptions="Fill" VerticalOptions="Start"
>
<Label
Text="{Binding 客製化使用者對話窗ViewModel.對話窗主題}"
TextColor="#000000"
FontSize="30"
HorizontalOptions="Center"
Margin="0,40,0,20"
/>
<Label
Text="{Binding 客製化使用者對話窗ViewModel.對話窗訊息}"
TextColor="#000000"
Margin="20,20,20,30"
FontSize="18"
HorizontalTextAlignment="Center"
HorizontalOptions="Center"
/>
<Label Text="帳號" />
<Entry
HorizontalOptions="Fill"
Text="{Binding 客製化使用者對話窗ViewModel.對話窗使用者帳號}"
/>
<Label Text="密碼" />
<Entry
HorizontalOptions="Fill"
Text="{Binding 客製化使用者對話窗ViewModel.對話窗使使用者密碼}"
IsPassword="True"
/>
<StackLayout
Orientation="Horizontal"
HorizontalOptions="Center"
>
<Button
Text="確定"
Command="{Binding 客製化對話窗確定Command}"
/>
<Button
Text="取消"
Command="{Binding 客製化對話窗取消Command}"
/>
</StackLayout>
</StackLayout>
</Grid>
</ContentView>
-
-
CustDialogUserControl.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace CustomDiag.UserControls
{
public partial class CustDialogUserControl : ContentView
{
public CustDialogUserControl()
{
InitializeComponent();
}
}
}
建立 ProcessingUserControl 使用者控制項 (User COntrol)
-
-
-
-
ProcessingUserControl.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="CustomDiag.UserControls.ProcessingUserControl">
<Grid
HorizontalOptions="Fill" VerticalOptions="Fill"
>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="150" />
</Grid.RowDefinitions>
<BoxView
Grid.Row="0" Grid.RowSpan="2"
BackgroundColor="#BB000000"
>
<BoxView.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding 點選遮罩Command}"
NumberOfTapsRequired="1" />
</BoxView.GestureRecognizers>
</BoxView>
<StackLayout
Grid.Row="0"
HorizontalOptions="Center" VerticalOptions="Center"
Margin="20,0"
>
<Label
Text="{Binding 處理中ViewModel.處理中訊息}"
TextColor="#FFFFFF"
FontSize="30"
HorizontalOptions="Center"
/>
<Label
Text="{Binding 處理中ViewModel.處理中狀態文字}"
TextColor="#FFFFFF"
Margin="0,30,0,0"
FontSize="20"
HorizontalOptions="Center"
/>
</StackLayout>
<ActivityIndicator
Grid.Row="1"
Color="#53ff1a"
WidthRequest="50" HeightRequest="50"
HorizontalOptions="Center" VerticalOptions="Center"
IsRunning="{Binding 處理中ViewModel.忙碌中控制項使用中}"
/>
</Grid>
</ContentView>
-
-
ProcessingUserControl.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace CustomDiag.UserControls
{
public partial class ProcessingUserControl : ContentView
{
public ProcessingUserControl()
{
InitializeComponent();
}
}
}
修改 MainPage 檢視 (View)
-
-
MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
xmlns:UserControls="clr-namespace:CustomDiag.UserControls"
x:Class="CustomDiag.Views.MainPage"
BackgroundColor="#ffaa80"
Title="MainPage">
<Grid>
<StackLayout
Spacing="30"
HorizontalOptions="Center" VerticalOptions="Center">
<Button Text="客製化對話窗"
Command="{Binding 客製化對話窗Command}"
/>
<Label Text="{Binding 使用者輸入內容}"/>
<Button Text="處理中遮罩"
Command="{Binding 處理中遮罩Command}"/>
</StackLayout>
<UserControls:ProcessingUserControl
IsVisible="{Binding 處理中ViewModel.顯示處理中遮罩}"
/>
<UserControls:CustDialogUserControl
IsVisible="{Binding 客製化使用者對話窗ViewModel.顯示客製化使用者對話窗}"
/>
</Grid>
</ContentPage>
建立 CustDialogUserControlViewModel 檢視模型 (ViewModel)
-
-
-
-
CustDialogUserControlViewModel.cs
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
namespace CustomDiag.ViewModels
{
public class CustDialogUserControlViewModel : BindableBase
{
#region ViewModel
#region 顯示客製化使用者對話窗
private bool _顯示客製化使用者對話窗 = false;
public bool 顯示客製化使用者對話窗
{
get { return _顯示客製化使用者對話窗; }
set { SetProperty(ref _顯示客製化使用者對話窗, value); }
}
#endregion
#region 對話窗主題
private string _對話窗主題 = "";
public string 對話窗主題
{
get { return _對話窗主題; }
set { SetProperty(ref _對話窗主題, value); }
}
#endregion
#region 對話窗訊息
private string _對話窗訊息 = "";
public string 對話窗訊息
{
get { return _對話窗訊息; }
set { SetProperty(ref _對話窗訊息, value); }
}
#endregion
#region 對話窗使用者帳號
private string _對話窗使用者帳號 = "";
public string 對話窗使用者帳號
{
get { return _對話窗使用者帳號; }
set { SetProperty(ref _對話窗使用者帳號, value); }
}
#endregion
#region 對話窗使使用者密碼
private string _對話窗使使用者密碼 = "";
public string 對話窗使使用者密碼
{
get { return _對話窗使使用者密碼; }
set { SetProperty(ref _對話窗使使用者密碼, value); }
}
#endregion
#endregion
public CustDialogUserControlViewModel()
{
}
public void 顯示客製化使用者對話窗控制項(string p對話窗主題, string p對話窗訊息)
{
對話窗主題 = p對話窗主題;
對話窗訊息 = p對話窗訊息;
對話窗使用者帳號 = "";
對話窗使使用者密碼 = "";
顯示客製化使用者對話窗 = true;
}
public void 關閉客製化使用者對話窗控制項()
{
顯示客製化使用者對話窗 = false;
}
}
}
建立 ProcessingUserControlViewModel 檢視模型 (ViewModel)
-
-
-
-
ProcessingUserControlViewModel.cs
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
namespace CustomDiag.ViewModels
{
public class ProcessingUserControlViewModel : BindableBase
{
#region ViewModel
#region 顯示處理中遮罩
private bool _顯示處理中遮罩 = false;
public bool 顯示處理中遮罩
{
get { return _顯示處理中遮罩; }
set { SetProperty(ref _顯示處理中遮罩, value); }
}
#endregion
#region 忙碌中控制項使用中
private bool _忙碌中控制項使用中 = true;
public bool 忙碌中控制項使用中
{
get { return _忙碌中控制項使用中; }
set { SetProperty(ref _忙碌中控制項使用中, value); }
}
#endregion
#region 處理中訊息
private string _處理中訊息 = "";
public string 處理中訊息
{
get { return _處理中訊息; }
set { SetProperty(ref _處理中訊息, value); }
}
#endregion
#region 處理中狀態文字
private string _處理中狀態文字 = "";
public string 處理中狀態文字
{
get { return _處理中狀態文字; }
set { SetProperty(ref _處理中狀態文字, value); }
}
#endregion
#endregion
public ProcessingUserControlViewModel()
{
}
public void 顯示忙碌中使用者控制項(string p處理中訊息, string p處理中狀態文字)
{
處理中訊息 = p處理中訊息;
處理中狀態文字 = p處理中狀態文字;
顯示處理中遮罩 = true;
忙碌中控制項使用中 = true;
}
public void 關閉忙碌中使用者控制項()
{
處理中訊息 = "";
處理中狀態文字 = "";
顯示處理中遮罩 = false;
忙碌中控制項使用中 = false;
}
}
}
修改 MainPageViewModel 檢視模型 (ViewModel)
-
-
MainPageViewModel.cs
using Prism.Commands;
using Prism.Mvvm;
using Prism.Navigation;
using Prism.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CustomDiag.ViewModels
{
public class MainPageViewModel : BindableBase, INavigationAware
{
#region DI
INavigationService _navigationService;
IPageDialogService _dialogService;
#endregion
#region ICommand
public DelegateCommand 客製化對話窗Command { get; set; }
public DelegateCommand 處理中遮罩Command { get; set; }
public DelegateCommand 點選遮罩Command { get; set; }
public DelegateCommand 客製化對話窗確定Command { get; set; }
public DelegateCommand 客製化對話窗取消Command { get; set; }
#endregion
#region ViewModel
#region 顯示客製化對話窗
private bool _顯示客製化對話窗 = false;
public bool 顯示客製化對話窗
{
get { return _顯示客製化對話窗; }
set { SetProperty(ref _顯示客製化對話窗, value); }
}
#endregion
#region 對話窗主題
private string _對話窗主題 = "";
public string 對話窗主題
{
get { return _對話窗主題; }
set { SetProperty(ref _對話窗主題, value); }
}
#endregion
#region 對話窗內容
private string _對話窗內容 = "";
public string 對話窗內容
{
get { return _對話窗內容; }
set { SetProperty(ref _對話窗內容, value); }
}
#endregion
#region 使用者輸入內容
private string _使用者輸入內容 = "";
public string 使用者輸入內容
{
get { return _使用者輸入內容; }
set { SetProperty(ref _使用者輸入內容, value); }
}
#endregion
public ProcessingUserControlViewModel 處理中ViewModel { get; set; } = new ProcessingUserControlViewModel();
public CustDialogUserControlViewModel 客製化使用者對話窗ViewModel { get; set; } = new CustDialogUserControlViewModel();
#endregion
public MainPageViewModel(INavigationService navigationService, IPageDialogService dialogService)
{
_navigationService = navigationService;
_dialogService = dialogService;
客製化對話窗Command = new DelegateCommand(客製化對話窗);
處理中遮罩Command = new DelegateCommand(處理中遮罩);
點選遮罩Command = new DelegateCommand(點選遮罩);
客製化對話窗確定Command = new DelegateCommand(客製化對話窗確定);
客製化對話窗取消Command = new DelegateCommand(客製化對話窗取消);
}
private void 客製化對話窗取消()
{
使用者輸入內容 = "";
客製化使用者對話窗ViewModel.關閉客製化使用者對話窗控制項();
}
private async void 客製化對話窗確定()
{
if (string.IsNullOrEmpty(客製化使用者對話窗ViewModel.對話窗使用者帳號) || 客製化使用者對話窗ViewModel.對話窗使用者帳號.Length < 6)
{
await _dialogService.DisplayAlertAsync("警告", "帳號不可為空值或者小於6個字元", "確定");
}
else if (string.IsNullOrEmpty(客製化使用者對話窗ViewModel.對話窗使使用者密碼) || 客製化使用者對話窗ViewModel.對話窗使使用者密碼.Length < 8)
{
await _dialogService.DisplayAlertAsync("警告", "密碼不可為空值或者小於8個字元", "確定");
}
else
{
使用者輸入內容 = $"帳號:{客製化使用者對話窗ViewModel.對話窗使用者帳號} / 密碼: {客製化使用者對話窗ViewModel.對話窗使使用者密碼}";
客製化使用者對話窗ViewModel.關閉客製化使用者對話窗控制項();
}
}
private void 點選遮罩()
{
處理中ViewModel.關閉忙碌中使用者控制項();
}
private async void 處理中遮罩()
{
處理中ViewModel.顯示忙碌中使用者控制項("正在更新資料", "正在更新資料,需要2秒鐘");
await Task.Delay(2000);
處理中ViewModel.關閉忙碌中使用者控制項();
}
private void 客製化對話窗()
{
客製化使用者對話窗ViewModel.顯示客製化使用者對話窗控制項("請進行身分驗證", "請輸入您的帳號與密碼,並且點選確定按鈕");
}
public void OnNavigatedFrom(NavigationParameters parameters)
{
}
public void OnNavigatedTo(NavigationParameters parameters)
{
}
}
}
Grid.Row
這樣用法的附加屬性;這裡,將會新建一個附加屬性類別,當這個附加屬性設定在Entry
控制項上的時候,就會自動設定該Entry
控制項上的其他相關屬性。