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
  • 移動開發(一):使用.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.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...