Android之ContentProvider數據存儲

来源:http://www.cnblogs.com/zhangmiao14/archive/2016/12/22/6212140.html
-Advertisement-
Play Games

一、ContentProvider保存數據介紹 一個程式可以通過實現一個ContentProvider的抽象介面將自己的數據完全暴露出去,而且ContentProvider是以類似資料庫中表的方式將數據暴露的。那麼外界獲取其提供的數據,也就應該與從資料庫中獲取數據的操作基本一樣,只不過是採用URL來 ...


 一、ContentProvider保存數據介紹

一個程式可以通過實現一個ContentProvider的抽象介面將自己的數據完全暴露出去,而且ContentProvider是以類似資料庫中表的方式將數據暴露的。那麼外界獲取其提供的數據,也就應該與從資料庫中獲取數據的操作基本一樣,只不過是採用URL來表示外界需要訪問的“資料庫”。

ContentProvider提供了一種多應用間數據共用的方式。

ContentProvider是個實現了一組用於提供其他應用程式存取數據的標準方法的類。應用程式可以在ContentProvider中執行如下操作:查詢數據、修改數據、添加數據、刪除數據。

標準的ContentProvider:Android提供了一些已經在系統中實現的標準ContentProvider,比如聯繫人信息,圖片庫等等,可以用這些ContentProvider來訪問設備上存儲的聯繫人信息、圖片等等。

在ContentProvider中使用的查詢字元串有別於標準的SQL查詢,很多諸如select、add、delete、modify等操作都使用一種特殊的URL進行,這種URL由3部分組成,“content://”,代表數據的路徑和一個可選的表示數據的ID。

content://media/internal/images 這個URL將返回設備上存儲的所有圖片

content://contacts/people/ 這個URL將返回設備上的所有聯繫人信息

content://contacts/people/45 這個URL返回單個結果(聯繫人信息中ID為45的聯繫人記錄)

 如果想要存儲位元組型數據,比如點陣圖文件等,那保存該數據的數據列其實是一個表示實際保存保存文件的URL字元串,客戶端通過它來讀取對應的文件數據,處理這種數據類型的ContentProvider需要實現一個名為_data的欄位,_data欄位列出了該文件在Android文件系統上的精確路徑。這個欄位不僅是供客戶端使用,而且也可以供ContentResolver使用。客戶端可以調用ContentResolver.openOutputStream()方法來處理該URL指向的文件資源,如果是ContentResolver本身的話,由於其持有的許可權比客戶端要高,所以它能直接訪問該數據文件。

二、使用方法

大多數ContentProvider使用Android文件系統或者SQLite資料庫來保持數據,但是也可以以任何方式來存儲。本例用SQLite資料庫來保持數據。

1.創建一個介面,定義了一個名為CONTENT_URL,並且是public static final的Uri類型的類變數,必須為其指定一個唯一的字元串值,最好的方案是類的全稱,和數據列的名稱。

public interface IProivderMetaData {

    public static final String AUTHORITY = "com.zhangmiao.datastoragedemo";

    public static final String DB_NAME = "book.db";

    public static final int VERSION = 1;

    public interface BookTableMetaData extends BaseColumns {
        public static final String TABLE_NAME = "book";
        public static final Uri CONTENT_URI = Uri.parse("content://"
                        + AUTHORITY + "/" + TABLE_NAME);

        public static final String BOOK_ID = "_id";
        public static final String BOOK_NAME = "name";
        public static final String BOOK_PUBLISHER = "publisher";

        public static final String SORT_ORDER = "_id desc";
        public static final String CONTENT_LIST = "vnd.android.cursor.dir/vnd.bookprovider.book";
        public static final String CONTENT_ITEM = "vnd.android.cursor.item/vnd.bookprovider.book";
    }
}

2.實現SQLiteOpenHelper

public class ContentProviderDBHelper extends SQLiteOpenHelper implements IProivderMetaData {

    private static final String TAG = "ContentProviderDBHelper";

    public ContentProviderDBHelper(Context context) {
        super(context, DB_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        ...
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        ...
    }
}

3.創建一個繼承了ContentProvider父類的類

public class ContentProviderDBHelper extends SQLiteOpenHelper implements IProivderMetaData {
    public ContentProviderDBHelper(Context context) {
        super(context, DB_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
       ...
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       ...
    }
}

4.在AndroidManifest.xml中使用標簽來設置調用ContentProvider。

<provider
     android:authorities="com.zhangmiao.datastoragedemo"
     android:name=".BookContentProvider"/>

5.增加數據

mContentResolver = getContentResolver();
String[] bookNames = new String[]{"Chinese", "Math", "English", "Sports"};
String[] bookPublishers = new String[]{"XinHua", "GongXin", "DianZi", "YouDian"};
for (int i = 0; i < bookNames.length; i++) {
    ContentValues values = new ContentValues();
    values.put(IProivderMetaData.BookTableMetaData.BOOK_NAME, bookNames[i]);
    values.put(IProivderMetaData.BookTableMetaData.BOOK_PUBLISHER, bookPublishers[i]);
    mContentResolver.insert(IProivderMetaData.BookTableMetaData.CONTENT_URI, values);
}

6.刪除數據

String bookId = "1";
if (!"".equals(bookId)) {
     ContentValues values1 = new ContentValues();
     values1.put(IProivderMetaData.BookTableMetaData.BOOK_ID,bookId);
     mContentResolver.delete(Uri.withAppendedPath(
IProivderMetaData.BookTableMetaData.CONTENT_URI, bookId),
           "_id = ?", new String[]{bookId}
); }
else { mContentResolver.delete( IProivderMetaData.BookTableMetaData.CONTENT_URI, null, null
); }

7.查詢數據

Cursor cursor = mContentResolver.query(IProivderMetaData.BookTableMetaData.CONTENT_URI,
null, null, null, null); String text = ""; if (cursor != null) { while (cursor.moveToNext()) { String bookIdText =
cursor.getString(cursor.getColumnIndex(IProivderMetaData.BookTableMetaData.BOOK_ID)); String bookNameText =
cursor.getString(cursor.getColumnIndex(IProivderMetaData.BookTableMetaData.BOOK_NAME)); String bookPublisherText =

cursor.getString(cursor.getColumnIndex(IProivderMetaData.BookTableMetaData.BOOK_PUBLISHER)); text += "id = " + bookIdText + ",name = " + bookNameText +
",publisher = " + bookPublisherText + "\n"; } cursor.close(); mTableInfo.setText(text); }

8.更新數據

String bookId1 = "2";
String bookName = "Art";
String bookPublisher = "TieDao";
ContentValues values2 = new ContentValues();
values2.put(IProivderMetaData.BookTableMetaData.BOOK_NAME,bookName);
values2.put(IProivderMetaData.BookTableMetaData.BOOK_PUBLISHER,bookPublisher);
if ("".equals(bookId1)) {
      mContentResolver.update(IProivderMetaData.BookTableMetaData.CONTENT_URI,
                            values2, null, null);
} else {
      mContentResolver.update(Uri.withAppendedPath(IProivderMetaData.BookTableMetaData.CONTENT_URI, bookId1),
                            values2, "_id = ? ", new String[]{bookId1}
); }

三、小案例

1.添加strings.xml文件

    <string name="content_provider">ContentProvider</string>
    <string name="add_data">增加數據</string>
    <string name="delete_data">刪除數據</string>
    <string name="update_data">更改數據</string>
    <string name="query_data">查詢數據</string>

2.修改activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.zhangmiao.datastoragedemo.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="@string/content_provider" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/fab_margin"
            android:layout_marginTop="@dimen/fab_margin"
            android:orientation="horizontal">

            <Button
                android:id="@+id/provider_add"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/add_data" />

            <Button
                android:id="@+id/provider_delete"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/delete_data" />

            <Button
                android:id="@+id/provider_update"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/update_data" />

            <Button
                android:id="@+id/provider_query"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/query_data" />

        </LinearLayout>

        <TextView
            android:id="@+id/table_info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/app_name" />
    </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

3.添加IProviderMetaData介面

package com.zhangmiao.datastoragedemo;

import android.net.Uri;
import android.provider.BaseColumns;

/**
 * Created by zhangmiao on 2016/12/20.
 */
public interface IProviderMetaData {

    public static final String AUTHORITY = "com.zhangmiao.datastoragedemo";

    public static final String DB_NAME = "book.db";

    public static final int VERSION = 1;

    public interface BookTableMetaData extends BaseColumns {
        public static final String TABLE_NAME = "book";
        public static final Uri CONTENT_URI = Uri.parse("content://"
                        + AUTHORITY + "/" + TABLE_NAME);

        public static final String BOOK_ID = "_id";
        public static final String BOOK_NAME = "name";
        public static final String BOOK_PUBLISHER = "publisher";

        public static final String SORT_ORDER = "_id desc";
        public static final String CONTENT_LIST = "vnd.android.cursor.dir/vnd.bookprovider.book";
        public static final String CONTENT_ITEM = "vnd.android.cursor.item/vnd.bookprovider.book";
    }
}

4.添加ContentProviderDBHelper類

package com.zhangmiao.datastoragedemo;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created by zhangmiao on 2016/12/20.
 */
public class ContentProviderDBHelper extends SQLiteOpenHelper implements IProviderMetaData {

    private static final String TAG = "ContentProviderDBHelper";

    public ContentProviderDBHelper(Context context) {
        super(context, DB_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String TABLESQL = "CREATE TABLE IF NOT EXISTS "
                + BookTableMetaData.TABLE_NAME + " ("
                + BookTableMetaData.BOOK_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + BookTableMetaData.BOOK_NAME + " VARCHAR,"
                + BookTableMetaData.BOOK_PUBLISHER + " VARCHAR)";
        db.execSQL(TABLESQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + "to"
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + DB_NAME);
        onCreate(db);
    }
}

5.添加BookContentProvider類

package com.zhangmiao.datastoragedemo;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by zhangmiao on 2016/12/21.
 */
public class BookContentProvider extends ContentProvider {

    private static final String TAG = "BookContentProvider";
    private static UriMatcher uriMatcher = null;
    private static final int BOOKS = 1;
    private static final int BOOK = 2;

    private ContentProviderDBHelper dbHelper;
    private SQLiteDatabase db;

    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(IProviderMetaData.AUTHORITY,
                IProviderMetaData.BookTableMetaData.TABLE_NAME, BOOKS);
        uriMatcher.addURI(IProviderMetaData.AUTHORITY,
                IProviderMetaData.BookTableMetaData.TABLE_NAME + "/#",
                BOOK);
    }

    @Override
    public boolean onCreate() {
        dbHelper = new ContentProviderDBHelper(getContext());
        return (dbHelper == null) ? false : true;
    }

    @Nullable
    @Override
    public String getType(Uri uri) {
        switch (uriMatcher.match(uri)) {
            case BOOKS:
                return IProviderMetaData.BookTableMetaData.CONTENT_LIST;
            case BOOK:
                return IProviderMetaData.BookTableMetaData.CONTENT_ITEM;
            default:
                throw new IllegalArgumentException("This is a unKnow Uri"
                        + uri.toString());
        }
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        switch (uriMatcher.match(uri)) {
            case BOOKS:
                db = dbHelper.getWritableDatabase();
                long rowId = db.insert(
                        IProviderMetaData.BookTableMetaData.TABLE_NAME,
                        IProviderMetaData.BookTableMetaData.BOOK_ID,
                        values);
                Uri insertUri = Uri.withAppendedPath(uri, "/" + rowId);
                Log.i(TAG, "insertUri:" + insertUri.toString());
                getContext().getContentResolver().notifyChange(uri, null);
                return insertUri;
            case BOOK:

            default:
                throw new IllegalArgumentException("This is a unKnow Uri"
                        + uri.toString());
        }
    }

    @Nullable
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        db = dbHelper.getReadableDatabase();
        switch (uriMatcher.match(uri)) {
            case BOOKS:
                return db.query(IProviderMetaData.BookTableMetaData.TABLE_NAME,
                        projection, selection, selectionArgs, null, null,
                        sortOrder);
            case BOOK:
                long id = ContentUris.parseId(uri);
                String where = "_id=" + id;
                if (selection != null && !"".equals(selection)) {
                    where = selection + " and " + where;
                }
                return db.query(IProviderMetaData.BookTableMetaData.TABLE_NAME,
                        projection, where, selectionArgs, null, null, sortOrder);
            default:
                throw new IllegalArgumentException("This is a unKnow Uri"
                        + uri.toString());
        }
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        db = dbHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)) {
            case BOOKS:
                return db.delete(IProviderMetaData.BookTableMetaData.TABLE_NAME,
                        selection, selectionArgs);
            case BOOK:
                long id = ContentUris.parseId(uri);
                String where = "_id=" + id;
                if (selection != null && !"".equals(selection)) {
                    where = selection + " and " + where;
                }
                return db.delete(IProviderMetaData.BookTableMetaData.TABLE_NAME,
                        selection, selectionArgs);
            default:
                throw new IllegalArgumentException("This is a unKnow Uri"
                        + uri.toString());
        }
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        db = dbHelper.getWritableDatabase();
        switch (uriMatcher.match(uri)) {
            case BOOKS:
                return db.update(IProviderMetaData.BookTableMetaData.TABLE_NAME,
                        values, null, null);
            case BOOK:
                long id = ContentUris.parseId(uri);
                String where = "_id=" + id;
                if (selection != null && !"".equals(selection)) {
                    where = selection + " and " + where;
                }
                return db.update(IProviderMetaData.BookTableMetaData.TABLE_NAME,
                        values, selection, selectionArgs);
            default:
                throw new IllegalArgumentException("This is a unKnow Uri"
                        + uri.toString());
        }
    }
}

6.修改AndroidManifest.xml文件

<provider
            android:authorities="com.zhangmiao.datastoragedemo"
            android:name=".BookContentProvider"/>

7.修改MainActivity

package com.zhangmiao.datastoragedemo;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.*;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {private ContentResolver mContentResolver;
    private BookContentProvider mBookContentProvider;private TextView mTableInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.v("MainActivity", "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button cpAdd = (Button) findViewById(R.id.provider_add);
        Button cpDelete = (Button) findViewById(R.id.provider_delete);
        Button cpUpdate = (Button) findViewById(R.id.provider_update);
        Button cpQuery = (Button) findViewById(R.id.provider_query);

        mTableInfo = (TextView) findViewById(R.id.table_info);

        cpAdd.setOnClickListener(this);
        cpDelete.setOnClickListener(this);
        cpQuery.setOnClickListener(this);
        cpUpdate.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {case R.id.provider_add:
                mContentResolver = getContentResolver();
                String[] bookNames = new String[]{"Chinese", "Math", "English", "Sports"};
                String[] bookPublishers = new String[]{"XinHua", "GongXin", "DianZi", "YouDian"};
                for (int i = 0; i < bookNames.length; i++) {
                    ContentValues values = new ContentValues();
                    values.put(IProviderMetaData.BookTableMetaData.BOOK_NAME, bookNames[i]);
                    values.put(IProviderMetaData.BookTableMetaData.BOOK_PUBLISHER, bookPublishers[i]);
                    mContentResolver.insert(IProviderMetaData.BookTableMetaData.CONTENT_URI, values);
                }
                break;
            case R.id.provider_delete:
                String bookId = "1";
                if (!"".equals(bookId)) {
                    ContentValues values1 = new ContentValues();
                    values1.put(IProviderMetaData.BookTableMetaData.BOOK_ID,
                            bookId);
                    mContentResolver.delete(
                            Uri.withAppendedPath(
                                    IProviderMetaData.BookTableMetaData.CONTENT_URI,
                                    bookId
                            ), "_id = ?",
                            new String[]{bookId}
                    );
                } else {
                    mContentResolver.delete(
                            IProviderMetaData.BookTableMetaData.CONTENT_URI,
                            null,
                            null
                    );
                }
                break;
            case R.id.provider_query:
                Cursor cursor = mContentResolver.query(IProviderMetaData.BookTableMetaData.CONTENT_URI, null, null, null, null);
                String text = "";
                if (cursor != null) {
                    while (cursor.moveToNext()) {
                        String bookIdText =
                                cursor.getString(cursor.getColumnIndex(IProviderMetaData.BookTableMetaData.BOOK_ID));
                        String bookNameText =
                                cursor.getString(cursor.getColumnIndex(IProviderMetaData.BookTableMetaData.BOOK_NAME));
                        String bookPublisherText =
                                cursor.getString(cursor.getColumnIndex(IProviderMetaData.BookTableMetaData.BOOK_PUBLISHER));
                        text += "id = " + bookIdText + ",name = " + bookNameText + ",publisher = " + bookPublisherText + "\n";
                    }
                    cursor.close();
                    mTableInfo.setText(text);
                }
                break;
            case R.id.provider_update:
                String bookId1 = "2";
                String bookName = "Art";
                String bookPublisher = "TieDao";
                ContentValues values2 = new ContentValues();
                values2.put(IProviderMetaData.BookTableMetaData.BOOK_NAME,
                        bookName);
                values2.put(IProviderMetaData.BookTableMetaData.BOOK_PUBLISHER,
                        bookPublisher);
                if ("".equals(bookId1)) {
                    mContentResolver.update(
                            IProviderMetaData.BookTableMetaData.CONTENT_URI,
                            values2, null, null);
                } else {
                    mContentResolver.update(
                            Uri.withAppendedPath(
                                    IProviderMetaData.BookTableMetaData.CONTENT_URI, bookId1),
                            values2, "_id = ? ", new String[]{bookId1});
                }
                break;default:
                Log.v("MainActivity", "default");
                break;
        }
    }
}

代碼下載地址:https://github.com/ZhangMiao147/DataStorageDemo

參考文章:https://liuzhichao.com/p/562.html


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

-Advertisement-
Play Games
更多相關文章
  • 現實中:電臺要發佈消息,通過廣播把消息廣播出去,使用收音機,就可以收聽廣播,得知這條消息。Android中:系統在運行過程中,會產生許多事件,那麼某些事件產生時,比如:電量改變、收發簡訊、撥打電話、屏幕解鎖、開機,系統會發送廣播。 只要應用程式接收到這條廣播,就知道系統發生了相應的事件,從而執行相應 ...
  • 一、網路保存數據介紹 可以使用網路來保存數據,在需要的時候從網路上獲取數據,進而顯示在App中。 用網路保存數據的方法有很多種,對於不同的網路數據採用不同的上傳與獲取方法。 本文利用LeanCloud來進行網路數據的存儲。 LeanCloud是一種簡單高效的數據和文件存儲服務。感興趣的可以查看網址: ...
  • NSURLConnection在iOS9被宣佈棄用,NSURLSession從13年發展到現在,終於迎來了它獨步江湖的時代.NSURLSession是蘋果在iOS7後為HTTP數據傳輸提供的一系列介面,比NSURLConnection強大,坑少,好用.今天從使用的角度介紹下. ...
  • 大家在網上購物時都有這樣一個體驗,在確認訂單選擇收貨人以及地址時,會跳轉頁面到我們存入網站內的所有收貨信息(包含收貨地址,收貨人)的界面供我們選擇,一旦我們點擊其中某一條信息,則會自動跳轉到訂單提交界面,此時的收貨信息已經變為我們之前選擇的收件信息、 為了實現這個功能,Android提供了一個機制, ...
  • 前言斷點續傳概述斷點續傳就是從文件賞賜中斷的地方重新開始下載或者上傳數據,而不是從頭文件開始。當下載大文件的 ...
  • Ocelot的中間代碼是仿照國外編譯器相關圖書Modern Compiler Implementation 中所使用的名為Tree 的中間代碼設計的。顧名思義,Tree 是一種樹形結構,其特征是簡單,而且方便轉換為機器語言。 例如以下代碼: 會被轉換成如下的中間代碼: 組成中間代碼的類如表11.1 ...
  • 關於“靜態類型檢查”,想必使用C 或Java 的各位應該非常熟悉了。在此過程中將檢查表達式的類型,發現類型不正確的操作時就會報錯。例如結構體之間無法用+ 進行加法運算,指針和數值之間無法用* 進行乘法運算,將數組傳遞給參數類型為int 型的函數會出現莫名其妙的結果。在編譯過程中檢查是否符合這樣的限制 ...
  • 微信小程式可以通過生成帶參數的二維碼,那麼這個參數是可以通過APP的頁面進行監控的 這樣就可以統計每個二維碼的推廣效果。 今天由好推二維碼推出的小程式統計工具HotApp小程式統計也推出了帶參數二維碼統計功能 幫助統計每個渠道的二維碼的推廣效果,用戶使用情況,留存,付費等。 網址是: www.wei ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...