學習
《台北》Xamarin.Forms 跨平台行動開發一日實戰營(派工 App)
相關連結:
本次課程包含 1.5 小時的線上課程與 1 天實體課程,將會帶領大家從無到有,開發出一個派工與回報的後台 Web API 專案與跨平台(Android / iOS)行動應用 App。
ZXing.Net.Mobile
這個關鍵字,搜尋出這個套件。請選擇 ZXing.Net.Mobile.Forms
這個套件,要來安裝到 .NET Standard 專案內。<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="XFQRCode.Views.MainPage"
Title="ZXing.Net.Mobile 條碼掃描">
<StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
<Label Text="Welcome to Xamarin Forms and Prism!" />
<Label
Text="條碼類型"/>
<Label
Text="{Binding BarcodeFormatType}"/>
<Label
Text="{Binding BarcodeResult}"/>
<Button
Text="Scan..."
Command="{Binding ScanCommand}"/>
</StackLayout>
</ContentPage>
MainPageViewModel.cs
檔案,修正 ViewModel 的商業邏輯程式碼。parameters.ContainsKey("Result")
表示式,確認該事件可以讀取掃描結果內容。namespace XFQRCode.ViewModels
{
public class MainPageViewModel : INotifyPropertyChanged, INavigationAware
{
public event PropertyChangedEventHandler PropertyChanged;
public string BarcodeFormatType { get; set; }
public string BarcodeResult { get; set; }
public DelegateCommand ScanCommand { get; set; }
private readonly INavigationService _navigationService;
public MainPageViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
ScanCommand = new DelegateCommand(() =>
{
_navigationService.NavigateAsync("ScanPage");
});
}
public void OnNavigatedFrom(NavigationParameters parameters)
{
}
public void OnNavigatingTo(NavigationParameters parameters)
{
if(parameters.ContainsKey("Result"))
{
var fooReslut = parameters["Result"] as Result;
BarcodeFormatType = fooReslut.BarcodeFormat.ToString();
BarcodeResult = fooReslut.Text;
}
}
public void OnNavigatedTo(NavigationParameters parameters)
{
}
}
}
xmlns:Zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
,這樣,我們就可以使用這個命名空間前置詞參考到這個 ZXingScannerView 控制項。<?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"
xmlns:Zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="XFQRCode.Views.ScanPage"
Title="進行條碼掃描中..."
>
<Grid
>
<Zxing:ZXingScannerView
HorizontalOptions="Fill"
IsAnalyzing="{Binding IsAnalyzing}"
IsScanning="{Binding IsScanning}"
Result="{Binding ScanResult, Mode=TwoWay}"
ScanResultCommand="{Binding ScanResultCommand}"
/>
<Zxing:ZXingDefaultOverlay Opacity="0.9" ShowFlashButton="False" />
</Grid>
</ContentPage>
ScanPageViewModel.cs
檔案,修正 ViewModel 的商業邏輯程式碼。Device.BeginInvokeOnMainThread
方法,將您要執行的程式碼,全部都指定在 UI (Main) Thread 上運行。namespace XFQRCode.ViewModels
{
public class ScanPageViewModel : INotifyPropertyChanged, INavigationAware
{
public event PropertyChangedEventHandler PropertyChanged;
public bool IsAnalyzing { get; set; } = true;
public bool IsScanning { get; set; } = true;
public Result ScanResult { get; set; }
private readonly INavigationService _navigationService;
public DelegateCommand ScanResultCommand { get; set; }
public ScanPageViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
ScanResultCommand = new DelegateCommand(async () =>
{
Device.BeginInvokeOnMainThread(async () =>
{
IsAnalyzing = false;
IsScanning = false;
var fooPara = new NavigationParameters();
fooPara.Add("Result", ScanResult);
// 回到上頁,並且把掃描結果帶回去
await _navigationService.GoBackAsync(fooPara);
});
});
}
public void OnNavigatedFrom(NavigationParameters parameters)
{
}
public void OnNavigatingTo(NavigationParameters parameters)
{
}
public void OnNavigatedTo(NavigationParameters parameters)
{
}
}
}
PCLStorage
這個關鍵字,搜尋出這個套件。這個 PCLStorage 套件圖示為一個綠色瓶子樣子。<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="NETStdPCLStorage.Views.MainPage"
Title="{Binding Title}">
<StackLayout HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand">
<Label Text="Welcome to Xamarin Forms and Prism!" />
<Entry Text="{Binding MyEntry}"/>
<Button Text="Save" Command="{Binding SaveCommand}"/>
<Button Text="Clean" Command="{Binding CleanCommand}"/>
<Button Text="Load" Command="{Binding LoadCommand}"/>
</StackLayout>
</ContentPage>
MainPageViewModel.cs
檔案,修正 ViewModel 的商業邏輯程式碼。namespace NETStdPCLStorage.ViewModels
{
public class MainPageViewModel : ViewModelBase
{
#region MyEntry
private string _MyEntry = "";
/// <summary>
/// MyEntry
/// </summary>
public string MyEntry
{
get { return this._MyEntry; }
set { this.SetProperty(ref this._MyEntry, value); }
}
#endregion
public DelegateCommand SaveCommand { get; set; }
public DelegateCommand CleanCommand { get; set; }
public DelegateCommand LoadCommand { get; set; }
public MainPageViewModel(INavigationService navigationService)
: base(navigationService)
{
Title = "Main Page";
SaveCommand = new DelegateCommand(async () =>
{
IFolder rootFolder = FileSystem.Current.LocalStorage;
IFolder folder = await rootFolder.CreateFolderAsync("MySubFolder",
CreationCollisionOption.OpenIfExists);
IFile file = await folder.CreateFileAsync("answer.txt",
CreationCollisionOption.ReplaceExisting);
await file.WriteAllTextAsync(MyEntry);
});
CleanCommand = new DelegateCommand(() =>
{
MyEntry = "";
});
LoadCommand = new DelegateCommand(async () =>
{
IFolder rootFolder = FileSystem.Current.LocalStorage;
IFolder folder = await rootFolder.CreateFolderAsync("MySubFolder",
CreationCollisionOption.OpenIfExists);
IFile file = await folder.GetFileAsync("answer.txt");
MyEntry = await file.ReadAllTextAsync();
});
}
}
}