UWP控制項與DataBind

来源:http://www.cnblogs.com/ms-uap/archive/2016/06/14/5520872.html
-Advertisement-
Play Games

在uwp開發中必不可少的一個環節就是各種通用的控制項的開發,所以在閑暇時間彙總了一下在uwp開發中控制項的幾種常用寫法,以及屬性的幾種綁定方式,有可能不全面,請大家多多包涵 :) 1、先從win10新增的{x:Bind}綁定方式說起,相對於{Binding},{x:Bind}在時間複雜度和空間複雜度上都 ...


  在uwp開發中必不可少的一個環節就是各種通用的控制項的開發,所以在閑暇時間彙總了一下在uwp開發中控制項的幾種常用寫法,以及屬性的幾種綁定方式,有可能不全面,請大家多多包涵 :)

1、先從win10新增的{x:Bind}綁定方式說起,相對於{Binding},{x:Bind}在時間複雜度和空間複雜度上都要降低不少。但並不是說{x:Bind}能夠完全取代{Binding},因為{x:Bind} 比 {Binding} 少了許多功能,例如 Source、UpdateSourceTrigger等,並且不支持後臺C#代碼編寫,所以使用者還是要根據自己的需求來選擇用哪種方式,下麵是Control1的簡單實現

Control1.xaml

<UserControl
    x:Class="Controls.Control1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <Grid>
        <TextBlock Text="{x:Bind Text}"></TextBlock>
    </Grid>
</UserControl>

Control1.xaml.cs

using Windows.UI.Xaml.Controls;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace Controls
{
    public sealed partial class Control1 : UserControl
    {
        public Control1()
        {
            this.InitializeComponent();
        }

        public string Text { set; get; }
    }
}

使用方式

<Page
    x:Class="Controls.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:controls="using:Controls">

    <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Margin="40">
        <controls:Control1 Text="這是控制項1"></controls:Control1>
    </StackPanel>
</Page>

  值得一提是{x:Bind}在DataTemplate中綁定時是需要指定類型的(x:DataType),並且Mode預設是OneTime,所以使用者如果有需要千萬不要忘了改成Mode=OneWay或者Mode=TwoWay

<DataTemplate x:DataType="model:Student">
        <TextBlock Text="{x:Bind Name}"></TextBlock>
        <TextBlock Text="{x:Bind Age}"></TextBlock>
</DataTemplate>

 

2、{Binding}綁定方式,大家應該比較熟悉了,它提供了豐富的綁定功能,綁定方式也比較靈活,閑話不多說啦,下麵的Control2Control3的實現

TextVisibilityConverter.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;

namespace Controls.Common
{
    public class TextVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if(value is string)
            {
                var text = value as string;
                
                if(string.IsNullOrEmpty(text))
                {
                    return Visibility.Collapsed;
                }
                else
                {
                    return Visibility.Visible;
                }
            }

            return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
}
View Code

Control2.xaml

<UserControl
    x:Class="Controls.Control2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Controls"
    xmlns:converter="using:Controls.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">
    <UserControl.Resources>
        <converter:TextVisibilityConverter x:Name="TextVisibilityConverter"></converter:TextVisibilityConverter>
    </UserControl.Resources>
    <Grid>
        <TextBlock Text="{Binding Text}" Visibility="{Binding Text,Converter={StaticResource TextVisibilityConverter}}"></TextBlock>
    </Grid>
</UserControl>

Control2.xaml.cs

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace Controls
{
    public sealed partial class Control2 : UserControl
    {
        public Control2()
        {
            this.InitializeComponent();

            this.DataContext = this;
        }

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(Control2), new PropertyMetadata(""));

    }
}

Control3.xaml

<UserControl
    x:Class="Controls.Control3"
    Name="uc"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <Grid>
        <TextBlock Text="{Binding ElementName=uc,Path=Text}"></TextBlock>
    </Grid>
</UserControl>

Control3.xaml.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace Controls
{
    public sealed partial class Control3 : UserControl
    {
        public Control3()
        {
            this.InitializeComponent();
        }

        public string Text { set; get; }
    }
}

  

大家可以看出Control2Control3是有些微差別的:

Control2是通過 this.DataContext = this,然後將依賴屬性(至於為什麼是依賴屬性,下麵會有詳細的介紹)綁到xaml頁面的控制項屬性上

Control3的特點也不難發現,充分利用了{Binding}強大功能的一個小小角落;個人感覺應該提一下的是,如果Control3有一個叫做Control1屬性,類型是Control1,我們可以把控制項1綁到控制項3上面去,這樣我們就可以在控制項3里訪問控制項1啦,這個只是{Binding}靈活運用的一個例子

<controls:Control1 x:Name="ctr1" Text="這是控制項1"></controls:Control1>
<controls:Control3 Control1="{Binding ElementName=ctr1}"></controls:Control3>

 

3、通過依賴屬性的PropertyChangedCallback來實現對控制項屬性賦值,請看示例Control5

Control5.xaml

<UserControl
    x:Class="Controls.Control5"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <Grid>
        <TextBlock Name="txt"></TextBlock>
    </Grid>
</UserControl>

Control5.xaml.cs

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace Controls
{
    public sealed partial class Control5 : UserControl
    {
        public Control5()
        {
            this.InitializeComponent();
        }

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(Control5), new PropertyMetadata("", OnTextChanged));

        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var me = d as Control5;
            me.OnTextChanged();
        }

        private void OnTextChanged()
        {
            var text = txt.Text = this.Text;

            if (string.IsNullOrEmpty(text))
            {
                txt.Visibility = Visibility.Collapsed;
            }
            else
            {
                txt.Visibility = Visibility.Visible;
            }
        }
    }
}

  

  不用通過任何綁定,就可以實現數據賦值,好處在於更加靈活,實現了與Control2同樣的功能,您會不會覺得與使用Converter相比,這樣寫更加直觀和舒服呢,而且很多複雜的功能都可以在OnTextChanged裡面處理。當然,並不是說Converter是多餘的,如果僅限於“值”的轉換,Converter還是很方便的,而且還可以重用。

  如果我們增加一個屬性TextMaxLength,用來表示最多可顯示的字元數,這樣我們把Control5做一下改裝

Control5.xaml

<UserControl
    x:Class="Controls.Control5"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <StackPanel>
        <TextBlock Name="txt"></TextBlock>
        <TextBlock><Run Text="最多可顯示"></Run><Run x:Name="run1" Foreground="Red"></Run><Run Text="個字元"></Run></TextBlock>
        <TextBlock><Run Text="還有"></Run><Run x:Name="run2" Foreground="Blue"></Run><Run Text="個字元可以顯示"></Run></TextBlock>
    </StackPanel>
</UserControl>

Control5.xaml.cs

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace Controls
{
    public sealed partial class Control5 : UserControl
    {
        public Control5()
        {
            this.InitializeComponent();
        }

        public int TextMaxLength
        {
            get { return (int)GetValue(TextMaxLengthProperty); }
            set { SetValue(TextMaxLengthProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TextMaxLength.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextMaxLengthProperty =
            DependencyProperty.Register("TextMaxLength", typeof(int), typeof(Control5),
                new PropertyMetadata(int.MaxValue, OnTextChanged));

        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(Control5),
                new PropertyMetadata("", OnTextChanged));

        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var me = d as Control5;
            me.OnTextChanged();
        }

        private void OnTextChanged()
        {
            run1.Text = TextMaxLength.ToString();

            if (string.IsNullOrEmpty(this.Text))
            {
                txt.Visibility = Visibility.Collapsed;
            }
            else
            {
                txt.Visibility = Visibility.Visible;
                var len = this.Text.Length;
                if (len <= TextMaxLength)
                {
                    txt.Text = this.Text;
                    run2.Text = (TextMaxLength - len).ToString();
                }
                else
                {
                    txt.Text = this.Text.Remove(TextMaxLength);
                    run2.Text = "0";
                }
            }
        }
    }
}

使用方式

<Page
    x:Class="Controls.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:controls="using:Controls">

    <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" HorizontalAlignment="Center" Margin="40">
        <controls:Control5 x:Name="control5" TextMaxLength="10" Text="這是控制項5"></controls:Control5>
    </StackPanel>
</Page>

運行結果

  需求好無釐頭啊,不過確實體現出了通過PropertyChangedCallback來處理實現兩種或兩種以上屬性間“聯動”(我給起的名字,具體意思就是多個屬性聯合在一起來實現某個功能,意會吧)情況的方便之處,在這裡提醒一下大家,請儘量使用同一個PropertyChangedCallback來處理屬性“聯動”問題,否則可能會因為屬性賦值先後問題,而導致出現各種“值”不一致的bug

 

4、{TemplateBinding}綁定方式實現自定義控制項

  用UserControl來製作自定義控制項是一個很方便的做法,但是用來製作一些簡單或者功能單一的那些最基本的自定義控制項時,就顯得有點大材小用了,同時UserControl也帶來了許多多餘的開銷,這個時候就可以用另外一種方式來編寫這樣的控制項了,我們可以通過看一下Control4的實現方式,來瞭解一下

Generic.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="using:Controls">

    <Style TargetType="controls:Control4">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="controls:Control4">
                    <Grid>
                        <TextBlock x:Name="txt" Text="{TemplateBinding Text}"></TextBlock>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

Control4.cs

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Controls
{
    public class Control4 : Control
    {
        TextBlock txt;

        public Control4()
        {
            DefaultStyleKey = typeof(Control4);
        }

        //public string Text { set; get; }


        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(Control4), new PropertyMetadata(""));


        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            txt = GetTemplateChild("txt") as TextBlock;
        }
    }
}

這種實現方式有幾個特點:

a)Generic.xaml文件要放在主項目的根目錄下的一個叫做“Themes”的文件夾下,如果沒有“Themes”文件夾,可以自己創建一個

b)構造函數里不能缺少DefaultStyleKey = typeof(Control4)

c)您需要對控制項的生命周期有一定的瞭解,因為在不同的時期txt有可能為null

d)所有的綁定方式都是TemplateBinding,當然你也可以用txt.Text=Text來賦值,但是在這之前最好能確定txt不為空

一般在重寫控制項時使用的比較多例如重寫Button、ListView等,您可以到系統的“C:\Program Files (x86)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\{版本號比如 10.0.10586.0}\Generic\generic.xaml”里找到這些控制項的樣式,可以根據視覺需求對控制項樣式做一些修改,也可以增加一些自定義的功能

 

5、比較一下

把這5個控制項放到一起比較一下

MainPage.xaml

<Page
    x:Class="Controls.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:controls="using:Controls">

    <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" HorizontalAlignment="Center" Margin="40">
        <controls:Control1 x:Name="control1" Text="這是控制項1"></controls:Control1>
        <controls:Control2 x:Name="control2" Text="這是控制項2"></controls:Control2>
        <controls:Control3 x:Name="control3" Text="這是控制項3"></controls:Control3>
        <controls:Control4 x:Name="control4" Text="這是控制項4"></controls:Control4>
        <controls:Control5 x:Name="control5" Text="這是控制項5"></controls:Control5>
        <TextBox Name="txt"></TextBox>
        <Button Click="Button_Click">update</Button>
    </StackPanel>
</Page>

MainPage.xaml.cs

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace Controls
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            control1.Text = txt.Text;
            control2.Text = txt.Text;
            control3.Text = txt.Text;
            control4.Text = txt.Text;
            control5.Text = txt.Text;
        }
    }
}

運行結果

看上去這些控制項都沒有問題,但是如果我們在TextBox中輸入內容,然後update一下,再看一下結果

  我們發現Control1和Control3的值沒有更新,問題到底出在哪呢?仔細檢查一下會發現這倆個控制項的Text屬性是普通屬性(public string Text { set; set; }),依賴屬性是有通知屬性變更的能力的,而普通屬性是不具備這個能力的,所以我們需要控制項繼承INotifyPropertyChanged介面,於是我們將Control1.xaml.cs作如下變更,Control3也如Control1一樣

using System.ComponentModel;
using System.Runtime.CompilerServices;
using Windows.UI.Xaml.Controls;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace Controls
{
    public sealed partial class Control1 : UserControl, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged([CallerMemberName]string propertyName = null)
        {
            var handler = PropertyChanged;

            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }

        public Control1()
        {
            this.InitializeComponent();
        }

        private string text;

        public string Text
        {
            get
            {
                return text;
            }
            set
            {
                text = value;
                RaisePropertyChanged();
            }
        }
    }
}

現在我們再來看一下運行結果

Control3是可以了,可是為什麼Control1還是不能更新呢,why?讓我們來重新看一下Control1的code,原來問題出現在這裡

前面我們說過{x:Bind}的預設Mode是OneTime,所以我們需要把它改成OneWay

<UserControl
    x:Class="Controls.Control1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">

    <Grid>
        <TextBlock Text="{x:Bind Text,Mode=OneWay}"></TextBlock>
    </Grid>
</UserControl>

再來不厭其煩地看一下結果

Great!用螺絲釘們經常說的一句話叫“大功告成”。:-D

 

題外話,給大家出個謎語,猜一猜下麵的程式運行結果是多少?

for (var i = 0; i < 10; i++)
{
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
        {
            Debug.WriteLine(i);
        });
    });
}

 


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

-Advertisement-
Play Games
更多相關文章
  • 之所以寫這個,是因為本來想寫一個Youtube刷評論的工具,把登錄做出來了,後面就沒繼續做下去。 涉及到基本的HttpWatch的應用以及Fiddler的應用(Fd主要用來排查問題,通過對比 瀏覽器和vs代碼 找出問題所在!以瀏覽器為標準)。 通過HttpWatch抓包分析,我把登錄Youtube一 ...
  • 在.Net中,對文件(File)和文件夾(Folder)的操作可以使用File類和Directory類,也可以使用FileInfo類和DirectoryInfo類,本文將詳細介紹,需要的朋友可以參考。 在.Net中,對文件(File)和文件夾(Folder)的操作可以使用File類和Director... ...
  • 以下兩種方法均為全站出錯處理 方法一: 1、在Web.config配置文件中<system.web></system.web>中添加<customErrors mode="On" defaultRedirect="404.htm" ></customErrors>節點 2、添加錯誤處理頁面:Appl ...
  • 下麵我們就動手來創建一個線程,使用Thread類創建線程時,只需提供線程入口即可。(線程入口使程式知道該讓這個線程乾什麼事) 在C#中,線程入口是通過ThreadStart代理(delegate)來提供的,你可以把ThreadStart理解為一個函數指針,指向線程要執行的函數,當調用Thread.S ...
  • 前幾個月做了一個旅游網站,有PC站和手機站,涉及支付寶支付功能. 要求:PC站下的單,用戶用手機登錄也能支付;同理,手機站下的單,PC端登錄也能支付. 附支付寶開放平臺網址:即時到賬 ,手機網站支付. 當然啦,最基本的就是要申請賬號,簽約產品.完事之後,把官網的DEMO跑起來. PC端DEMO:cr ...
  • 單點登陸設計SSO英文全稱Single Sign On,單點登錄。SSO是在多個應用系統中,用戶只需要登錄一次就可以訪問所有相互信任的應用系統。它包括可以將這次主要的登錄映射到其他應用中用於同一個用戶的登錄的機制。它是目前比較流行的企業業務整合的解決方案之一 現在很多企業級應用都基本會去實現單點登陸 ...
  • Code First 遷移可用於從 Visual Studio 內部更新資料庫,但也可通過命令行工具 migrate.exe 來執行。本頁簡單介紹如何使用 migrate.exe 對資料庫執行遷移。 複製 migrate.exe 在使用 NuGet 安裝實體框架時,migrate.exe 位於下載包 ...
  • WCF Data Service with OData 是一個優秀的Restful Web Service在ASP.NET下的實現,但是在使用中,我遇到了一個問題,即當我單獨部署WDS服務的時候,Ajax訪問就需要跨域。 在一般的WCF服務中,我們可以用JSONP解決。所以我發起了下麵這個請求: 你 ...
一周排行
    -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# ...