WPF中的Marquee Textblock问题,C#? [英] Problem with Marquee Textblock in WPF,C#?

查看:62
本文介绍了WPF中的Marquee Textblock问题,C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF中为我的应用创建了Marquee Textblock控件,但它没有显示整个文本。我试图解决问题所在,但我无法解决。

创建的Marquee文本块控件xaml代码是

UserControl x:Class =Andhrajyo.MarqueeTextBlock

xmlns =http:// schemas。 microsoft.com/winfx/2006/xaml/presentation

xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml

xmlns :d =http://schemas.microsoft.com/expression/blend/2008

xmlns:mc =http://schemas.openxmlformats.org/markup-compatibility/2006

mc:Ignorable =d

FontFamily ={StaticResource PhoneFontFamilyNormal}

FontSize ={StaticResource PhoneFontSizeNormal}

Foreground ={StaticResource PhoneForegroundBrush}

d:DesignHeight =480d:DesignWidth =480



网格x:Name =LayoutRoot

< Canvas x:Name =canvasBackground =TransparentWidth ={Binding Width}Height ={Binding Height}>

< Canvas.Clip>

RectangleGeometry RadiusX =5RadiusY =5Rect =400,400,400,400

< /Canvas.Clip>





TextBlock x:Name =textBlock1

Height ={Binding Height}

Width ={Binding Width}

TextWrapping =NoWrap

Foreground ={Binding Foreground}

Text ={Binding Text}

FontSize = {Binding MFontSize}

< / Canvas>

/ Grid

/ UserControl





Marquee Textblock.xaml.cs是





使用系统;

使用System.Collections.Generic;

使用System.Linq;

使用System.Net;

使用System.Windows;

使用System.Windows.Controls;

使用System.Windows.Documents;

使用System.Windows.Input;

使用System.W indows.Media;

使用System.Windows.Media.Animation;

使用System.Windows.Shapes;



名称空间Andhrajyo

{

公共部分类MarqueeTextBlock:UserControl

{

// storyboard
故事板故事板;



//当textBlock1很长时偏移,因为用户希望看到平滑滚动

私有双偏移= 0;



//默认偏移

私有双defaultOffset = 100;



//默认速度

int velocity = 0;



//控件是否已加载

//因为它是加载控件的时候我们开始选择

bool isLoaded = false;



public MarqueeTextBlock()

{

InitializeComponent();



加载+ = MarqueeTextBlock_Loaded;

卸载+ = MarqueeTextBlock_Unloaded;

}



//卸载事件

void MarqueeTextBlock_Unloaded(对象发送者,RoutedEventArgs e)

{

//当控件卸载时停止选取框

StopMarquee() ;

}



//在已加载中设置数据上下文

void MarqueeTextBlock_Loaded(对象发送者,RoutedEventArgs e)

{

if(!isLoaded)

{

//加载控件

isLoaded = true;



LayoutRoot.DataContext = this;



//调整剪裁大小

Rect rect = new Rect(0,0,canvas.ActualWidth,canvas.ActualHeight);

RectangleGeometry reo = n ew RectangleGeometry();

reo.Rect = rect;

this.canvas.Clip = reo;



//始终垂直对齐

ChangeOffsetTop();



//这对于选框开始是可以的

StartMarquee();

}

else

{

//只需运行它,用户来之后这个页面再次

StartMarquee();

}

}





public void StartMarquee()

{

//首先停止所有选框

StopMarquee();



if(ShouldStartMarquee())

{

//更改偏移量

ChangeOffsetLeft(真);



DoAnima tionFirst();

}

else

{

//更改偏移量

ChangeOffsetLeft(false);

}

}



public void StopMarquee()

{

if(storyboard!= null)

{

storyboard.Stop();

}

}



bool ShouldStartMarquee()

{

返回textBlock1。 ActualWidth> LayoutRoot.ActualWidth;

}





//

void storyboard_Completed (对象发送者,EventArgs e)

{

DoAnimationAfter();



//记得取消订阅

storyboard.Completed + = storyboard_Completed;

}





/// ////////////////////////////////////////////////// ///////////////////////

//计算偏差

void ChangeOffsetLeft(bool isLong)

{

if(isLong)

{

offset = defaultOffset;

Canvas .SetLeft(textBlock1,offset);

}

else

{

offset =(canvas.ActualWidth - textBlock1.ActualWidth)/ 2;

Canvas.SetLeft(t extBlock1,offset);

}

}



//中心垂直

void ChangeOffsetTop()

{

double topOffset =(canvas.ActualHeight - textBlock1.ActualHeight)/ 2;

Canvas.SetTop(textBlock1 ,topOffset);

}



int CalculateDurationFirst()

{

int duration = 45000;







返回时间;

} < br $>


int CalculateDurationAfter()

{

int duration = 40000;







返回时间;

}





无效DoAnimationFirst()

{

storyboard = new Storyboard();

TranslateTransform trans = new Tr anslateTransform(){X = 5.0,Y = 1.0};

textBlock1.RenderTransformOrigin = new Point(0.5,0.5);

textBlock1.RenderTransform = trans;



//我们必须计算持续时间

DoubleAnimation moveAnim = new DoubleAnimation();

int durationFirst = CalculateDurationFirst() ;

moveAnim.Duration = TimeSpan.FromMilliseconds(durationFirst);



moveAnim.From = 0;

moveAnim.To = -textBlock1.ActualWidth - offset;



Storyboard.SetTarget(moveAnim,textBlock1);

Storyboard.SetTargetProperty(moveAnim,新的PropertyPath((UIElement.RenderTransform)。(TranslateTransform.X)));



//订阅已完成的事件

storyboard .Completed + = new EventHandler(storyboard_Completed);



storyboard.Children.Add(moveAnim);

storyboard.FillBehavior = FillBehavior.HoldEnd;



storyboard.Begin();

}







无效DoAnimationAfter()

{

storyboard = new Storyboard();

TranslateTransform trans = new TranslateTransform(){X = 5.0,Y = 1.0};

textBlock1 .RenderTransformOrigin = new Point(0.5,0.5);

textBlock1.RenderTransform = trans;



//我们必须计算持续时间

DoubleAnimation moveAnim = new DoubleAnimation();

int durationAfter = CalculateDurationAfter();

moveAnim.Duration = TimeSpan.FromMilliseconds(durationAfter);



moveAnim.From = canvas.ActualWidth;

moveAnim.To = -textBlock1.ActualWidth - 偏移;



Storyboard.SetTarget(moveAnim,textBlock1);

Storyboard.SetTargetProperty(moveAnim,new PropertyPath((UIElement.RenderTransform) )。(TranslateTransform.X)));



storyboard.Children.Add(moveAnim);

storyboard.RepeatBehavior = RepeatBehavior。永远;

storyboard.FillBehavior = FillBehavior.HoldEnd;



storyboard.Begin();

} < br $>




/////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// //////////////

//依赖属性

public static readonly DependencyProperty TextProperty =

DependencyProperty。注册(Text,typeof(string),typeof(MarqueeTextBlock),null);



公共字符串文本

{

get {return(string)this.GetValue(TextProperty); }

set

{

this.SetValue(TextProperty,value);



//这不是第一次的选框

//因为textBlock1.ActualWidth是0 !!

if(isLoaded)

{

StartMarquee();

}



}

}



public static readonly DependencyProperty MFontSizeProperty = DependencyProperty.Register(MFontSize,

typeof(int),typeof(MarqueeTextBlock),null);



public int MFontSize

{

get {return(int)this.GetValue(MFontSizeProperty); }

set

{

this.SetValue(MFontSizeProperty,value);

}

}



public static readonly DependencyProperty MTextTrimProperty = DependencyProperty.Register(MTextTrim,

typeof(String),typeof(MarqueeTextBlock) ),null);



public String MTextTrim

{

get {return(String)this.GetValue (MFontSizeProperty); }

set

{

this.SetValue(MTextTrimProperty,value);

}

}

}

}







我使用字幕文本块控件的应用程序代码是



controls2:marqueetextblock x:name =txtTxmlns:x =#unknownxmlns: controls2 =#unknown>



MFontSize =25

宽度=1000

高度=80保证金=20,-10,0,0

/>

./controls2:marqueetextblock





文本属性设置在代码后面



执行选框文本块后没有显示整个文本最后一个文本是miss ing ...

文本长度超过500个字符

请帮帮我。

解决方案

< blockquote class =FQ>

SureshMarepalli写道:



你能提供你的做法吗?

当然。你可以从这里开始: http://msdn.microsoft.com/en -us / library / ms752312%28v = vs.110%29.aspx [ ^ ]。



这些CodeProject文章也可能有用:

初学者的WPF动画教程 [ ^ ],

使用WPF中的故事板动画 [ ^ ]。



但我想要你:这样的元素可能非常烦人;我宁愿避免它。在向用户提供之前,最好先对自己和同事进行测试。



-SA


I created Marquee Textblock control in WPF for my app but it was not showing the entire text. I tried to solve where the problem is but i am unable to solve.
Created Marquee textblock control xaml code is
UserControl x:Class="Andhrajyo.MarqueeTextBlock"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480"

Grid x:Name="LayoutRoot"
<Canvas x:Name="canvas" Background="Transparent" Width="{Binding Width}" Height="{Binding Height}">
<Canvas.Clip>
RectangleGeometry RadiusX="5" RadiusY="5" Rect="400, 400, 400, 400"
</Canvas.Clip>


TextBlock x:Name="textBlock1"
Height="{Binding Height}"
Width="{Binding Width}"
TextWrapping="NoWrap"
Foreground="{Binding Foreground}"
Text="{Binding Text}"
FontSize="{Binding MFontSize}"
</Canvas>
/Grid
/UserControl


Marquee Textblock.xaml.cs is


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Andhrajyo
{
public partial class MarqueeTextBlock : UserControl
{
// storyboard
Storyboard storyboard;

// offset when textBlock1 is long, because user want to see smooth scrolling
private double offset =0;

// default offset
private double defaultOffset =100;

// default velocity
int velocity = 0;

// whether control has beed loaded
// because it's when control is loaded do we start marquee
bool isLoaded = false;

public MarqueeTextBlock()
{
InitializeComponent();

Loaded += MarqueeTextBlock_Loaded;
Unloaded += MarqueeTextBlock_Unloaded;
}

// unload event
void MarqueeTextBlock_Unloaded(object sender, RoutedEventArgs e)
{
// stop marquee when control is unloaded
StopMarquee();
}

// set data context in Loaded
void MarqueeTextBlock_Loaded(object sender, RoutedEventArgs e)
{
if (!isLoaded)
{
// control is loaded
isLoaded = true;

LayoutRoot.DataContext = this;

// resize the clipping
Rect rect = new Rect(0,0, canvas.ActualWidth, canvas.ActualHeight);
RectangleGeometry reo = new RectangleGeometry();
reo.Rect = rect;
this.canvas.Clip = reo;

// always vertical alignment
ChangeOffsetTop();

// this is OK for marquee to start
StartMarquee();
}
else
{
// just run it, after user come to this page again
StartMarquee();
}
}


public void StartMarquee()
{
// first stop all marquee
StopMarquee();

if (ShouldStartMarquee())
{
// change offset
ChangeOffsetLeft(true);

DoAnimationFirst();
}
else
{
// change offset
ChangeOffsetLeft(false);
}
}

public void StopMarquee()
{
if (storyboard != null)
{
storyboard.Stop();
}
}

bool ShouldStartMarquee()
{
return textBlock1.ActualWidth > LayoutRoot.ActualWidth;
}


// after
void storyboard_Completed(object sender, EventArgs e)
{
DoAnimationAfter();

// remeber to unsubscribe
storyboard.Completed += storyboard_Completed;
}


////////////////////////////////////////////////////////////////////////////
// calculate offset
void ChangeOffsetLeft(bool isLong)
{
if (isLong)
{
offset = defaultOffset;
Canvas.SetLeft(textBlock1, offset);
}
else
{
offset = (canvas.ActualWidth - textBlock1.ActualWidth)/2;
Canvas.SetLeft(textBlock1, offset);
}
}

// center vertical
void ChangeOffsetTop()
{
double topOffset = (canvas.ActualHeight - textBlock1.ActualHeight) / 2;
Canvas.SetTop(textBlock1, topOffset);
}

int CalculateDurationFirst()
{
int duration = 45000;



return duration;
}

int CalculateDurationAfter()
{
int duration = 40000;



return duration;
}


void DoAnimationFirst()
{
storyboard = new Storyboard();
TranslateTransform trans = new TranslateTransform() { X = 5.0, Y = 1.0 };
textBlock1.RenderTransformOrigin = new Point(0.5,0.5);
textBlock1.RenderTransform = trans;

// we must calculate the Duration
DoubleAnimation moveAnim = new DoubleAnimation();
int durationFirst = CalculateDurationFirst();
moveAnim.Duration = TimeSpan.FromMilliseconds(durationFirst);

moveAnim.From = 0;
moveAnim.To = -textBlock1.ActualWidth - offset;

Storyboard.SetTarget(moveAnim, textBlock1);
Storyboard.SetTargetProperty(moveAnim, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));

// subscribe to Completed event
storyboard.Completed += new EventHandler(storyboard_Completed);

storyboard.Children.Add(moveAnim);
storyboard.FillBehavior = FillBehavior.HoldEnd;

storyboard.Begin();
}



void DoAnimationAfter()
{
storyboard = new Storyboard();
TranslateTransform trans = new TranslateTransform() { X = 5.0, Y = 1.0 };
textBlock1.RenderTransformOrigin = new Point(0.5, 0.5);
textBlock1.RenderTransform = trans;

// we must calculate the Duration
DoubleAnimation moveAnim = new DoubleAnimation();
int durationAfter = CalculateDurationAfter();
moveAnim.Duration = TimeSpan.FromMilliseconds(durationAfter);

moveAnim.From = canvas.ActualWidth;
moveAnim.To = -textBlock1.ActualWidth - offset;

Storyboard.SetTarget(moveAnim, textBlock1);
Storyboard.SetTargetProperty(moveAnim, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));

storyboard.Children.Add(moveAnim);
storyboard.RepeatBehavior = RepeatBehavior.Forever;
storyboard.FillBehavior = FillBehavior.HoldEnd;

storyboard.Begin();
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// dependency property
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(MarqueeTextBlock), null);

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

// this doesnot marquee for the first time
// because textBlock1.ActualWidth is 0 !!
if (isLoaded)
{
StartMarquee();
}

}
}

public static readonly DependencyProperty MFontSizeProperty = DependencyProperty.Register("MFontSize",
typeof(int), typeof(MarqueeTextBlock), null);

public int MFontSize
{
get { return (int)this.GetValue(MFontSizeProperty); }
set
{
this.SetValue(MFontSizeProperty, value);
}
}

public static readonly DependencyProperty MTextTrimProperty = DependencyProperty.Register("MTextTrim",
typeof(String), typeof(MarqueeTextBlock), null);

public String MTextTrim
{
get { return (String)this.GetValue(MFontSizeProperty); }
set
{
this.SetValue(MTextTrimProperty, value);
}
}
}
}



My application code which uses the marquee textblock control is

controls2:marqueetextblock x:name="txtT" xmlns:x="#unknown" xmlns:controls2="#unknown">

MFontSize="25"
Width="1000"
Height="80" Margin="20,-10,0,0"
/>
./controls2:marqueetextblock


the text property was set in the code behind

after executing the marquee textblock is not showing the entire text the last text is missing..
the text is long of more than 500 characters
please help me.

解决方案

SureshMarepalli wrote:


can you please provide your way of doing marquee?

Sure. You can start here: http://msdn.microsoft.com/en-us/library/ms752312%28v=vs.110%29.aspx[^].

These CodeProject article could also be useful:
Beginner's WPF Animation Tutorial[^],
Animation using Storyboards in WPF[^].

But I wanted you: such element can be very annoying; I would prefer to avoid it. Better first test it on yourself and your colleagues, before offering to your users.

—SA


这篇关于WPF中的Marquee Textblock问题,C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆