Xamarin+Prism開發詳解七:Plugin開發與打包測試

来源:http://www.cnblogs.com/lixiaobin/archive/2017/01/09/XamarinPlugin.html
-Advertisement-
Play Games

如何快速開發Xamarin.Forms的Plugin?自己開發的Plugin如何使用Nuget打包?本地Package如何參照引用?本文通過TextToSpeech實例為你講解。 ...


有了上章【Xamarin+Prism開發詳解六:DependencyService與IPlatformInitializer的關係】的基礎,現在來理解Plugin開發就簡單了。

本文實例代碼地址:https://github.com/NewBLife/XamarinDemo/tree/master/Speecher

 

簡介

Plugin其實就是各類相對獨立的功能提取出來的Package,一般都不引用Xamarin相關類庫,比如上章的Text To Speech功能(Github庫地址https://github.com/jamesmontemagno/TextToSpeechPlugin):

image

xamarin的Plugin有一個專門的統計庫【xamarin/XamarinComponents】,目前統計結果如下:

NameDescriptionNuGetDocs & SourceCreator
Battery Status Gather battery level, charging status, and type. NuGet GitHub @JamesMontemagno
Barcode Scanner Scan and create barcodes with ZXing.NET.Mobile. NuGet GitHub @Redth
Bluetooth LE Scan and connect to Bluetooth devices. NuGet GitHub @allanritchie911
Calendar Query and modify device calendars NuGet GitHub Caleb Clarke
Compass Access device compass heading. NuGet GitHub @cbartonnh & @JamesMontemagno
Connectivity Get network connectivity info such as type and if connection is available. NuGet GitHub @JamesMontemagno
Cryptography PCL Crypto provides a consistent, portable set of crypto APIs. NuGet GitHub @aarnott
Device Info Properties about device such as OS, Model, and Id. NuGet GitHub @JamesMontemagno
Device Motion Provides access to Accelerometer, Gyroscope, Magnetometer, and Compass. NuGet GitHub @rdelrosario
Embedded Resource Unpack embedded resource cross-platform. NuGet GitHub @JosephHill
External Maps Launch external maps from Lat/Long or Address. NuGet GitHub @JamesMontemagno
File Storage/File System PCL Storage offers cross platform storage APIs. NuGet GitHub @dsplaisted
File Picker Pick and save files. NuGet GitHub @studyxnet
Fingerprint Access Fingerprint sensor on iOS, Android, and Windows. NuGet GitHub @smstuebe
FFImageLoading Image loading with caching, placeholders, transformations and more NuGet GitHub @molinch, @daniel-luberda
Geofencing Monitor regions when user enters/exits. NuGet GitHub @allanritchie911
Geolocator Easily detect GPS location of device. NuGet GitHub @JamesMontemagno
iBeacon & Estimote Range and monitor Bluetooth beacons. NuGet GitHub @allanritchie911
Lamp Access to LED NuGet GitHub @kphillpotts
Local Notifications Show local notifications NuGet GitHub @EdSnider, @JamesMontemagno
Manage Sleep Manage auto sleep/auto lock. NuGet GitHub @molinch0
Media Take or pick photos and videos. NuGet GitHub @JamesMontemagno
Media Manager Playback for Audio. NuGet GitHub @mhvdijk
Messaging Make phone call, send sms, and send e-mail NuGet GitHub @cjlotz
Microsoft Band Connect and communicate with the Microsoft Band from shared code! NuGet GitHub @mattleibow
Mono.Data.Sqlite Add Mono.Data.Sqlite to any Xamarin or Windows .NET app. NuGet GitHub @mattleibow
Permissions Easily check and request runtime permissions. NuGet GitHub @JamesMontemagno
Persistent key-value store Akavache is an asynchronous, persistent (i.e. writes to disk) key-value store. NuGet GitHub @paulcbetts
Portable Razor Lightweight implemenation of ASP.NET MVC APIs for mobile. NuGet GitHub @JosephHill
Push Notifications Cross platform iOS and Android Push Notifications. NuGet GitHub @rdelrosario
Secure Storage Provides secure storage for key value pairs Data NuGet GitHub @sameerIOTApps
Settings Simple & Consistent cross platform settings API. NuGet GitHub @JamesMontemagno
Share Easily share text, links, or open a browser. NuGet GitHub @JamesMontemagno & @Jakob Gürtl
Sockets TCP & UDP Listeners and Clients + UDP multicast. NuGet GitHub @rdavis_au
Speech Recognition Speech to Text. NuGet GitHub @allanritchie911
Text To Speech Talk back text from shared code. NuGet GitHub @JamesMontemagno
Toast A simple way of showing toast/pop-up notifications. NuGet GitHub @AdamPed & @EgorBo
User Dialogs Message box style dialogs. NuGet GitHub @allanritchie911
Version Tracking Track which versions of your app a user has previously installed. NuGet GitHub @ColbyLWilliams
Vibrate Vibrate any device. NuGet GitHub @JamesMontemagno
 

開發實踐

Xamarin為Plugin開發提供了一個Visual Studio模板,通過它你可以快速開發各類Plugin。

1、安裝Plugin for Xamarin Templates

image

2、從Plugin for Xamarin模板新建項目

image

整體項目結構:

image

通過模板創建的項目包含三類工程文件:

  • Plugin.功能名(PCL):懶漢式實例創建文件,生成Plugin.功能名.dll。(共用類文件)
  • Plugin.功能名.Abstractions(PCL):介面和Enums的定義,生成Plugin.功能名.Abstractions.dll。
  • Plugin.功能名.平臺名(n個):介面實現,生成Plugin.功能名.dll。

image

3、添加介面

namespace Plugin.Speecher.Abstractions
{
    /// <summary>
    /// Interface for Speecher
    /// </summary>
    public interface ISpeecher
    {
        void Speak(string text);
    }
}

4、各個平臺介面實現

iOS平臺

using AVFoundation;
using Plugin.Speecher.Abstractions;


namespace Plugin.Speecher
{
    /// <summary>
    /// Implementation for Speecher
    /// </summary>
    public class SpeecherImplementation : ISpeecher
    {
        public void Speak(string text)
        {
            var speechSynthesizer = new AVSpeechSynthesizer();

            var speechUtterance = new AVSpeechUtterance(text)
            {
                Rate = AVSpeechUtterance.MaximumSpeechRate / 4,
                Voice = AVSpeechSynthesisVoice.FromLanguage("en-US"),
                Volume = 0.5f,
                PitchMultiplier = 1.0f
            };

            speechSynthesizer.SpeakUtterance(speechUtterance);
        }
    }
}

Android平臺(由於沒有Xamarin.Forms,所以這裡使用Application.Context)

using Android.App;
using Android.Runtime;
using Android.Speech.Tts;
using Plugin.Speecher.Abstractions;
using System.Collections.Generic;

namespace Plugin.Speecher
{
    /// <summary>
    /// Implementation for Feature
    /// </summary>
    public class SpeecherImplementation : Java.Lang.Object, ISpeecher, TextToSpeech.IOnInitListener
    {
        TextToSpeech speaker;
        string toSpeak;
        public SpeecherImplementation() { }

        public void Speak(string text)
        {
            var ctx = Application.Context;
            toSpeak = text;
            if (speaker == null)
            {
                speaker = new TextToSpeech(ctx, this);
            }
            else
            {
                var p = new Dictionary<string, string>();
                speaker.Speak(toSpeak, QueueMode.Flush, p);
            }
        }

        public void OnInit([GeneratedEnum] OperationResult status)
        {
            if (status.Equals(OperationResult.Success))
            {
                var p = new Dictionary<string, string>();
                speaker.Speak(toSpeak, QueueMode.Flush, p);
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("was quiet");
            }
        }
    }
}

UWP平臺

using Plugin.Speecher.Abstractions;
using System;
using Windows.Media.SpeechSynthesis;
using Windows.UI.Xaml.Controls;

namespace Plugin.Speecher
{
    /// <summary>
    /// Implementation for Feature
    /// </summary>
    public class SpeecherImplementation : ISpeecher
    {
        public async void Speak(string text)
        {
            MediaElement mediaElement = new MediaElement();

            var synth = new SpeechSynthesizer();
            var stream = await synth.SynthesizeTextToStreamAsync(text);
            mediaElement.SetSource(stream, stream.ContentType);
            mediaElement.Play();
        }
    }
}

備註

如果確定一直使用Prism做開發的話,只需要定義介面與介面的實現就可以了。然後在實際項目各個平臺的RegisterTypes方法中註冊介面到IOC容器,

public void RegisterTypes(IUnityContainer container)
{
    container.RegisterType<ISpeecher, SpeecherImplementation>();
}

使用IOC容器管理對象生存周期,比懶漢式載入更加自由方便。比如在Prism.Forms中只需構造函數添加介面作為參數就可以自動創建對象。

 

打包並測試

在安裝Plugin for Xamarin Templates的時候其實已經安裝了另外一個打包用的模板【Plugin for Xamarin NuSpec】,通過它可以快速打包。

1、添加Package配置文件【Speecher.Plugin.nuspec】

image

預設生成的文件如下,包含所有平臺:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
    <metadata minClientVersion="2.8.3">
        <id>Plugin.Speecher</id>
        <version>1.0.0</version>
        <title>Speecher Plugin for Xamarin and Windows</title>
        <authors>Your Name</authors>
        <owners>Your Name</owners>
        <licenseUrl/>
        <projectUrl/>
        <!--Default Icon, a template can be found:  https://raw.githubusercontent.com/jamesmontemagno/Xamarin-Templates/master/Plugins-Templates/icons/plugin_icon.png-->  
        <iconUrl>https://raw.githubusercontent.com/jamesmontemagno/Xamarin-Templates/master/Plugins-Templates/icons/plugin_icon_nuget.png</iconUrl>

        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <description>
         Long description for your plugin.
        </description>
        <summary>Short description for your plugin.</summary>
        <tags>xamarin, pcl, xam.pcl, plugin, plugin for xamarin, windows phone, winphone, wp8, winrt, android, xamarin.forms, ios</tags>
        <dependencies>
            <group targetFramework="net">
            </group>
            <group targetFramework="win">
            </group>
            <group targetFramework="wp">
            </group>
            <group targetFramework="wpa">
            </group>
            <group targetFramework="MonoAndroid">
            </group>
            <group targetFramework="Xamarin.iOS10">
            </group>
            <group targetFramework="Xamarin.Mac20">
            </group>
            <group targetFramework="portable-net45+win+wpa81+wp80">
            </group>
            <group targetFramework="uap">
            </group>
            <group targetFramework="dotnet">
            </group>
        </dependencies>
    </metadata>
    <files>
        <!--Core-->
        <file src="Speecher\Plugin.Speecher\bin\Release\Plugin.Speecher.dll" target="lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\Plugin.Speecher.dll" />
        <file src="Speecher\Plugin.Speecher\bin\Release\Plugin.Speecher.xml" target="lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\Plugin.Speecher.xml" />
        <file src="Speecher\Plugin.Speecher\bin\Release\Plugin.Speecher.pdb" target="lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\Plugin.Speecher.pdb" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.dll" target="lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\Plugin.Speecher.Abstractions.dll" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.xml" target="lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\Plugin.Speecher.Abstractions.xml" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.pdb" target="lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\Plugin.Speecher.Abstractions.pdb" />        
        
        <!--dotnet-->
        <file src="Speecher\Plugin.Speecher\bin\Release\Plugin.Speecher.dll" target="lib\dotnet\Plugin.Speecher.dll" />
        <file src="Speecher\Plugin.Speecher\bin\Release\Plugin.Speecher.xml" target="lib\dotnet\Plugin.Speecher.xml" />
        <file src="Speecher\Plugin.Speecher\bin\Release\Plugin.Speecher.pdb" target="lib\dotnet\Plugin.Speecher.pdb" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.dll" target="lib\dotnet\Plugin.Speecher.Abstractions.dll" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.xml" target="lib\dotnet\Plugin.Speecher.Abstractions.xml" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.pdb" target="lib\dotnet\Plugin.Speecher.Abstractions.pdb" />
        
        <!--Win Phone Silverlight-->
        <file src="Speecher\Plugin.Speecher.WindowsPhone8\bin\Release\Plugin.Speecher.dll" target="lib\wp8\Plugin.Speecher.dll" />
        <file src="Speecher\Plugin.Speecher.WindowsPhone8\bin\Release\Plugin.Speecher.xml" target="lib\wp8\Plugin.Speecher.xml" />
        <file src="Speecher\Plugin.Speecher.WindowsPhone8\bin\Release\Plugin.Speecher.pdb" target="lib\wp8\Plugin.Speecher.pdb" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.dll" target="lib\wp8\Plugin.Speecher.Abstractions.dll" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.xml" target="lib\wp8\Plugin.Speecher.Abstractions.xml" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.pdb" target="lib\wp8\Plugin.Speecher.Abstractions.pdb" />
        
        <!--Win Phone 81-->
        <file src="Speecher\Plugin.Speecher.WindowsPhone81\bin\Release\Plugin.Speecher.dll" target="lib\wpa81\Plugin.Speecher.dll" />
        <file src="Speecher\Plugin.Speecher.WindowsPhone81\bin\Release\Plugin.Speecher.xml" target="lib\wpa81\Plugin.Speecher.xml" />
        <file src="Speecher\Plugin.Speecher.WindowsPhone81\bin\Release\Plugin.Speecher.pdb" target="lib\wpa81\Plugin.Speecher.pdb" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.dll" target="lib\wpa81\Plugin.Speecher.Abstractions.dll" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.xml" target="lib\wpa81\Plugin.Speecher.Abstractions.xml" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.pdb" target="lib\wpa81\Plugin.Speecher.Abstractions.pdb" />
        
        <!--WinStore-->
        <file src="Speecher\Plugin.Speecher.WindowsStore\bin\Release\Plugin.Speecher.dll" target="lib\win8\Plugin.Speecher.dll" />
        <file src="Speecher\Plugin.Speecher.WindowsStore\bin\Release\Plugin.Speecher.xml" target="lib\win8\Plugin.Speecher.xml" />
        <file src="Speecher\Plugin.Speecher.WindowsStore\bin\Release\Plugin.Speecher.pdb" target="lib\win8\Plugin.Speecher.pdb" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.dll" target="lib\win8\Plugin.Speecher.Abstractions.dll" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.xml" target="lib\win8\Plugin.Speecher.Abstractions.xml" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.pdb" target="lib\win8\Plugin.Speecher.Abstractions.pdb" />

        <!--Xamarin.Android-->
        <file src="Speecher\Plugin.Speecher.Android\bin\Release\Plugin.Speecher.dll" target="lib\MonoAndroid10\Plugin.Speecher.dll" />
        <file src="Speecher\Plugin.Speecher.Android\bin\Release\Plugin.Speecher.xml" target="lib\MonoAndroid10\Plugin.Speecher.xml" />
        <file src="Speecher\Plugin.Speecher.Android\bin\Release\Plugin.Speecher.pdb" target="lib\MonoAndroid10\Plugin.Speecher.pdb" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.dll" target="lib\MonoAndroid10\Plugin.Speecher.Abstractions.dll" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.xml" target="lib\MonoAndroid10\Plugin.Speecher.Abstractions.xml" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.pdb" target="lib\MonoAndroid10\Plugin.Speecher.Abstractions.pdb" />

        <!--Xamarin.iOS-->
        <file src="Speecher\Plugin.Speecher.iOS\bin\iPhone\Release\Plugin.Speecher.dll" target="lib\Xamarin.iOS10\Plugin.Speecher.dll" />
        <file src="Speecher\Plugin.Speecher.iOS\bin\iPhone\Release\Plugin.Speecher.xml" target="lib\Xamarin.iOS10\Plugin.Speecher.xml" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.dll" target="lib\Xamarin.iOS10\Plugin.Speecher.Abstractions.dll" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.xml" target="lib\Xamarin.iOS10\Plugin.Speecher.Abstractions.xml" />

        <!--uap-->
        <file src="Speecher\Plugin.Speecher.UWP\bin\Release\Plugin.Speecher.dll" target="lib\UAP10\Plugin.Speecher.dll" />
        <file src="Speecher\Plugin.Speecher.UWP\bin\Release\Plugin.Speecher.xml" target="lib\UAP10\Plugin.Speecher.xml" />
        <file src="Speecher\Plugin.Speecher.UWP\bin\Release\Plugin.Speecher.pdb" target="lib\UAP10\Plugin.Speecher.pdb" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.dll" target="lib\UAP10\Plugin.Speecher.Abstractions.dll" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.xml" target="lib\UAP10\Plugin.Speecher.Abstractions.xml" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.pdb" target="lib\UAP10\Plugin.Speecher.Abstractions.pdb" />
        
        <!--Xamarin.Mac
        <file src="Speecher\Plugin.Speecher.Mac\bin\iPhone\Release\Plugin.Speecher.dll" target="lib\Xamarin.Mac20\Plugin.Speecher.dll" />
        <file src="Speecher\Plugin.Speecher.Mac\bin\iPhone\Release\Plugin.Speecher.xml" target="lib\Xamarin.Mac20\Plugin.Speecher.xml" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.dll" target="lib\Xamarin.Mac20\Plugin.Speecher.Abstractions.dll" />
        <file src="Speecher\Plugin.Speecher.Abstractions\bin\Release\Plugin.Speecher.Abstractions.xml" target="lib\Xamarin.Mac20\Plugin.Speecher.Abstractions.xml" />
        -->
    </files>
</package>
  • 由於只正對iOS,Android,UWP三個平臺,其他的配置都刪除掉
  • 固定文件名用*代替

修改後的文件如下:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata minClientVersion="2.8.3">
    <id>Plugin.Speecher</id>
    <version>1.0.0</version>
    <title>Speecher Plugin for Xamarin and Windows</title>
    <authors>NewBlifes</authors>
    <owners>NewBlifes</owners>
    <iconUrl>https://github.com/NewBLife/NewBlife.Core/blob/master/logo.png</iconUrl>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>
      Text to speech plugin.
    </description>
    <summary>Text to speech plugin.</summary>
    <tags>xamarin, pcl, xam.pcl, plugin, plugin for xamarin, windows phone, UWP, android, xamarin.forms, ios</tags>
    <dependencies>
      <group targetFramework="MonoAndroid">
      </group>
      <group targetFramework="Xamarin.iOS10">
      </group>
      <group targetFramework="portable-net45+win+wpa81+wp80">
      </group>
      <group targetFramework="uap">
      </group>
    </dependencies>
  </metadata>
  <files>
    <!--Core-->
    <file src="Speecher\Plugin.Speecher\bin\Release\Plugin.Speecher.*" target="lib\portable-net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10\" />

    <!--Xamarin.Android-->
    <file src="Speecher\Plugin.Speecher.Android\bin\Release\Plugin.Speecher.*" target="lib\MonoAndroid10\" />

    <!--Xamarin.iOS-->
    <file src="Speecher\Plugin.Speecher.iOS\bin\iPhone\Release\Plugin.Speecher.*" target="lib\Xamarin.iOS10\" />

    <!--uap-->
    <file src="Speecher\Plugin.Speecher.UWP\bin\Release\Plugin.Speecher.*" target="lib\UAP10\" />

  </files>
</package>

image

2、編譯解決方案的Release版本,使用【nuget pack】命令打包

image

image

如果沒有Nuget的命令行工具記得安裝。

3、測試

添加本地包路徑

image

創建測試項目,添加Package引用

image

測試代碼

using Plugin.Speecher;
using Prism.Commands;
using Prism.Mvvm;

namespace SpeecherTest.ViewModels
{
    public class MainPageViewModel : BindableBase
    {
        private string _speakText;
        public string SpeakText
        {
            get { return _speakText; }
            set
            {
                SetProperty(ref _speakText, value);
            }
        }

        public MainPageViewModel()
        {
        }

        public DelegateCommand SpeakCommand => new DelegateCommand(
            () =>
            {
                CrossSpeecher.Current.Speak(SpeakText);
            },
            () => !string.IsNullOrEmpty(SpeakText)).ObservesProperty(() => this.SpeakText);
    }
}

pluginTextToSpeech1

總結

通過使用Plugin For xamarin模塊來開發Plugin簡單又快速,你只需要關註真正的邏輯就可以。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 前言 最初打算熟悉下微信開發打算用node.js開發,結果底氣不足先用C#開發,先踩了踩坑。 準備工作 微信公眾平臺開發者文檔。 這個先多看幾遍。 測試公眾號,申請開通後會看到微信號,appID,appsecret。開通後可以看到掃描下方的二維碼關註該測試公眾號 功能變數名稱。功能變數名稱和伺服器的話大家可以買阿裡 ...
  • 靜態、非靜態 先來看一段代碼來區分靜態與非靜態: 可以看出靜態與非靜態的區別在於 關鍵字 : static 靜態和非靜態的區別: 1)、在非靜態類中,既可以有實例成員,也可以有靜態成員。 2)、在調用實例成員的時候,需要使用對象名.實例成員; 在調用靜態成員的時候,需要使用類名.靜態成員名; 3)、 ...
  • 一.方法: ContainerFromIndex:返回 ItemCollection 中指定索引處的項的容器。 ContainerFromItem:返回與制定的項對應的容器(ComboxItem等條目控制項)。 Equals(Object):確定製定的Object是否等於當前的Object。 Fina ...
  • Linq之Expression進階 Lambda表達式 "Lambda表達式"是一個匿名函數,是一種高效的類似於函數式編程的表達式,Lambda簡化了開發中需要編寫的代碼量。它可以包含表達式和語句,並且可用於創建委托或表達式目錄樹類型,支持帶有可綁定到委托或表達式樹的輸入參數的內聯表達式。所有Lam ...
  • 委托是一個類,它定義了方法的類型,使得可以將方法當作另一個方法的參數來進行傳遞。事件是一種特殊的委托。 1.委托的聲明 (1). delegate delegate我們常用到的一種聲明 Delegate至少0個參數,至多32個參數,可以無返回值,也可以指定返回值類型。 例:public delega ...
  • APS.NET MVC中(以下簡稱“MVC”)的每一個請求,都會分配給相應的控制器和對應的行為方法去處理,而在這些處理的前前後後如果想再加一些額外的邏輯處理。這時候就用到了過濾器。 MVC支持的過濾器類型有四種,分別是:Authorization(授權),Action(行為),Result(結果)和 ...
  • 本篇博文,給大家講解一下裝飾模式,還是老樣子,有一個簡單的例子逐步演繹 一、舉例 用一個簡單的控制台實現 一個人穿各種各樣衣服 的功能 然後我們會很自然的寫出一下代碼: 先寫一個Person類 然後客戶端調用這個Person類 這樣就寫完了。 二、演繹 ①現在,我各種裝扮都寫到了Person類中,有 ...
  • 1 OAuth2解決什麼問題的? 舉個慄子先。小明在QQ空間積攢了多年的照片,想挑選一些照片來列印出來。然後小明在找到一家提供線上列印並且包郵的網站(我們叫它PP吧(Print Photo縮寫 😂))。 那麼現在問題來了,小明有兩個方案來得到列印的服務。 針對方案(1):小明要去下載這些照片,然後 ...
一周排行
    -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.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...