最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

WPF實(shí)現(xiàn)類似ChatGPT逐字打印效果的示例代碼

 更新時(shí)間:2023年08月11日 09:54:03   作者:czwy  
前一段時(shí)間ChatGPT類的應(yīng)用十分火爆,這類應(yīng)用在回答用戶的問題時(shí)逐字打印輸出,像極了真人打字回復(fù)消息,本文就來利用WPF模擬一下這種逐字打印的效果吧

背景

前一段時(shí)間ChatGPT類的應(yīng)用十分火爆,這類應(yīng)用在回答用戶的問題時(shí)逐字打印輸出,像極了真人打字回復(fù)消息。出于對(duì)這個(gè)效果的興趣,決定用WPF模擬這個(gè)效果。

真實(shí)的ChatGPT逐字輸出效果涉及其語言生成模型原理以及服務(wù)端與前端通信機(jī)制,本文不做過多闡述,重點(diǎn)是如何用WPF模擬這個(gè)效果。

技術(shù)要點(diǎn)與實(shí)現(xiàn)

對(duì)于這個(gè)逐字輸出的效果,我想到了兩種實(shí)現(xiàn)方法:

方法一:根據(jù)字符串長度n,添加n個(gè)關(guān)鍵幀DiscreteStringKeyFrame,第一幀的Value為字符串的第一個(gè)字符,緊接著的關(guān)鍵幀都比上一幀的Value多一個(gè)字符,直到最后一幀的Value是完整的目標(biāo)字符串。實(shí)現(xiàn)效果如下所示:

方法二:首先把TextBlock的字體顏色設(shè)置為透明,然后通過TextEffectPositionStartPositionCount屬性控制應(yīng)用動(dòng)畫效果的子字符串的起始位置以及長度,同時(shí)使用ColorAnimation設(shè)置TextEffectForeground屬性由透明變?yōu)槟繕?biāo)顏色(假定是黑色)。實(shí)現(xiàn)效果如下所示:

由于方案二的思路與WPF實(shí)現(xiàn)跳動(dòng)的字符效果中的效果實(shí)現(xiàn)思路非常類似,具體實(shí)現(xiàn)不再詳述。接下來我們看一下方案一通過關(guān)鍵幀動(dòng)畫拼接字符串的具體實(shí)現(xiàn)。

public class TypingCharAnimationBehavior : Behavior<TextBlock>
{
    private Storyboard _storyboard;
    protected override void OnAttached()
    {
        base.OnAttached();
        this.AssociatedObject.Loaded += AssociatedObject_Loaded; ;
        this.AssociatedObject.Unloaded += AssociatedObject_Unloaded;
        BindingOperations.SetBinding(this, TypingCharAnimationBehavior.InternalTextProperty, new Binding("Tag") { Source = this.AssociatedObject });
    }
    private void AssociatedObject_Unloaded(object sender, RoutedEventArgs e)
    {
        StopEffect();
    }
    private void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
    {
        if (IsEnabled)
            BeginEffect(InternalText);
    }
    protected override void OnDetaching()
    {
        base.OnDetaching();
        this.AssociatedObject.Loaded -= AssociatedObject_Loaded;
        this.AssociatedObject.Unloaded -= AssociatedObject_Unloaded;
        this.ClearValue(TypingCharAnimationBehavior.InternalTextProperty);
        if (_storyboard != null)
        {
            _storyboard.Remove(this.AssociatedObject);
            _storyboard.Children.Clear();
        }
    }
    private string InternalText
    {
        get { return (string)GetValue(InternalTextProperty); }
        set { SetValue(InternalTextProperty, value); }
    }
    private static readonly DependencyProperty InternalTextProperty =
    DependencyProperty.Register("InternalText", typeof(string), typeof(TypingCharAnimationBehavior),
    new PropertyMetadata(OnInternalTextChanged));
    private static void OnInternalTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var source = d as TypingCharAnimationBehavior;
        if (source._storyboard != null)
        {
            source._storyboard.Stop(source.AssociatedObject);
            source._storyboard.Children.Clear();
        }
        source.SetEffect(e.NewValue == null ? string.Empty : e.NewValue.ToString());
    }
    public bool IsEnabled
    {
        get { return (bool)GetValue(IsEnabledProperty); }
        set { SetValue(IsEnabledProperty, value); }
    }
    public static readonly DependencyProperty IsEnabledProperty =
        DependencyProperty.Register("IsEnabled", typeof(bool), typeof(TypingCharAnimationBehavior), new PropertyMetadata(true, (d, e) =>
        {
            bool b = (bool)e.NewValue;
            var source = d as TypingCharAnimationBehavior;
            source.SetEffect(source.InternalText);
        }));
    private void SetEffect(string text)
    {
        if (string.IsNullOrEmpty(text) || this.AssociatedObject.IsLoaded == false)
        {
            StopEffect();
            return;
        }
        BeginEffect(text);
    }
    private void StopEffect()
    {
        if (_storyboard != null)
        {
            _storyboard.Stop(this.AssociatedObject);
        }
    }
    private void BeginEffect(string text)
    {
        StopEffect();
        int textLength = text.Length;
        if (textLength < 1  || IsEnabled == false) return;
        if (_storyboard == null)
            _storyboard = new Storyboard();
        double duration = 0.15d;
        StringAnimationUsingKeyFrames frames = new StringAnimationUsingKeyFrames();
        Storyboard.SetTargetProperty(frames, new PropertyPath(TextBlock.TextProperty));
        frames.Duration = TimeSpan.FromSeconds(textLength * duration);
        for(int i=0;i<textLength;i++)
        {
            frames.KeyFrames.Add(new DiscreteStringKeyFrame()
            {
                Value = text.Substring(0,i+1),
                KeyTime = TimeSpan.FromSeconds(i * duration),
            });
        }
        _storyboard.Children.Add(frames);
        _storyboard.Begin(this.AssociatedObject, true);
    }
}

由于每一幀都在修改TextBlockText屬性的值,如果TypingCharAnimationBehavior直接綁定TextBlockText屬性,當(dāng)Text屬性的數(shù)據(jù)源發(fā)生變化時(shí),無法判斷是關(guān)鍵幀動(dòng)畫修改的,還是外部數(shù)據(jù)源變化導(dǎo)致Text的值被修改。因此這里用TextBlockTag屬性暫存要顯示的字符串內(nèi)容。調(diào)用的時(shí)候只需要把需要顯示的字符串變量綁定到Tag,并在TextBlock添加Behavior即可,代碼如下:

<TextBlock x:Name="source"
            IsEnabled="True"
            Tag="{Binding TypingText, ElementName=self}"
            TextWrapping="Wrap">
    <i:Interaction.Behaviors>
        <local:TypingCharAnimationBehavior IsEnabled="True" />
    </i:Interaction.Behaviors>
</TextBlock>

小結(jié)

兩種方案各有利弊:

關(guān)鍵幀動(dòng)畫拼接字符串這個(gè)方法的優(yōu)點(diǎn)是最大程度還原了逐字輸出的過程,缺點(diǎn)是需要額外的屬性來輔助,另外遇到英文單詞換行時(shí),會(huì)出現(xiàn)單詞從上一行行尾跳到下一行行首的問題;

通過TextEffect設(shè)置字體顏色這個(gè)方法則相反,不需要額外的屬性輔助,并且不會(huì)出現(xiàn)單詞在輸入過程中從行尾跳到下一行行首的問題,開篇中兩種實(shí)現(xiàn)方法效果圖中能看出這一細(xì)微差異。但是一開始就把文字都渲染到界面上,只是通過透明的字體顏色騙過用戶的眼睛,逐字改變字體顏色模擬逐字打印的效果。

到此這篇關(guān)于WPF實(shí)現(xiàn)類似ChatGPT逐字打印效果的示例代碼的文章就介紹到這了,更多相關(guān)WPF逐字打印效果內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

玉田县| 新蔡县| 盐津县| 嘉荫县| 东乡县| 如皋市| 肥乡县| 利辛县| 扎兰屯市| 秭归县| 柳林县| 泰兴市| 克什克腾旗| 无棣县| 六盘水市| 苏州市| 弥渡县| 郯城县| 东明县| 北票市| 永仁县| 靖江市| 隆安县| 南城县| 砚山县| 黔西| 郴州市| 阳新县| 崇仁县| 华蓥市| 昌江| 虎林市| 聂荣县| 刚察县| 孟津县| 高邑县| 西青区| 曲阳县| 青岛市| 杭州市| 江阴市|