C# 在WPF/XAML中水平滚动长文本和图像的最佳编码方法?
可能重复:C# 在WPF/XAML中水平滚动长文本和图像的最佳编码方法?,c#,.net,wpf,xaml,scroll,C#,.net,Wpf,Xaml,Scroll,可能重复: 我只是想知道是否有一些最好的编码方法可以在WPF中水平滚动文本和图像 基本上,我用了一些代码来做这件事 private void PopulateCanvas() { canvas1.Width = RootGrid.ActualWidth; canvas1.Height = RootGrid.ActualHeight;
我只是想知道是否有一些最好的编码方法可以在WPF中水平滚动文本和图像 基本上,我用了一些代码来做这件事
private void PopulateCanvas()
{
canvas1.Width = RootGrid.ActualWidth;
canvas1.Height = RootGrid.ActualHeight;
foreach (var item in PluginContentItems)
{
if (IsImage(item.IPluginContentItemElements))
{
string imageName = GetImageName(item.IPluginContentItemElements);
if (IsGifImage(item.IPluginContentItemElements))
AddGIFImage(imageName);
else
AddImage(imageName);
}
else // Text
{
string text = GetText(item.IPluginContentItemElements);
SolidColorBrush brush = GetFontColor(item.IPluginContentItemElements);
AddTextBlock(text, brush);
}
}
canvas1.Dispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate(Object state)
{
var node = textBlocks.First;
while (node != null)
{
if (node.Previous != null)
{
if (node.Previous.Value.GetType().FullName.Contains("TextBlock"))
Canvas.SetLeft(node.Value, Canvas.GetLeft((TextBlock)node.Previous.Value) + ((TextBlock)node.Previous.Value).ActualWidth); // + gap
else if (node.Previous.Value.GetType().FullName.Contains("GifImage"))
Canvas.SetLeft(node.Value, Canvas.GetLeft((GifImage)node.Previous.Value) + ((GifImage)node.Previous.Value).ActualWidth); // + gap
else if (node.Previous.Value.GetType().FullName.Contains("Controls.Image"))
Canvas.SetLeft(node.Value, Canvas.GetLeft((Image)node.Previous.Value) + ((Image)node.Previous.Value).ActualWidth); // + gap
}
else
{
if (node.Value.GetType().FullName.Contains("TextBlock"))
Canvas.SetLeft((TextBlock)node.Value, canvas1.Width); // + gap
if (node.Value.GetType().FullName.Contains("Image"))
Canvas.SetLeft((Image)node.Value, canvas1.Width); // + gap
if (node.Value.GetType().FullName.Contains("GifImage"))
Canvas.SetLeft((GifImage)node.Value, canvas1.Width); // + gap
}
node = node.Next;
}
return null;
}), null);
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
canvas1.Dispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate(Object state)
{
var node = textBlocks.First;
var lastNode = textBlocks.Last;
while (node != null)
{
double newLeft = Canvas.GetLeft(node.Value) - move_amount;
double w1 = 0;
if (node.Value is TextBlock)
w1 = ((TextBlock)node.Value).ActualWidth;
if (node.Value is Image)
w1 = ((Image)node.Value).ActualWidth;
if (node.Value is GifImage)
w1 = ((GifImage)node.Value).ActualWidth;
if (newLeft < (0 - w1)) // + gap
{
textBlocks.Remove(node);
var lastNodeLeftPos = Canvas.GetLeft(lastNode.Value);
textBlocks.AddLast(node);
if (lastNode.Value.GetType().FullName.Contains("TextBlock"))
{
// + gap
if ((lastNodeLeftPos + ((TextBlock)lastNode.Value).ActualWidth) > canvas1.Width) // Last element is offscreen
newLeft = lastNodeLeftPos + ((TextBlock)lastNode.Value).ActualWidth; // + gap
else
newLeft = canvas1.Width; // +gap;
}
else if (lastNode.Value.GetType().FullName.Contains("GifImage"))
{ // + gap
if ((lastNodeLeftPos + ((GifImage)lastNode.Value).ActualWidth) > canvas1.Width) // Last element is offscreen
newLeft = lastNodeLeftPos + ((GifImage)lastNode.Value).ActualWidth; // + gap
else
newLeft = canvas1.Width; // + gap
}
else if (lastNode.Value.GetType().FullName.Contains("Controls.Image"))
{ // + gap
if ((lastNodeLeftPos + ((Image)lastNode.Value).ActualWidth) > canvas1.Width) // Last element is offscreen
newLeft = lastNodeLeftPos + ((Image)lastNode.Value).ActualWidth; // + gap
else
newLeft = canvas1.Width; // + gap
}
}
Canvas.SetLeft(node.Value, newLeft);
node = node == lastNode ? null : node.Next;
}
return null;
}), null);
}
private void AddGIFImage(string file)
{
try
{
string pathToImage = System.IO.Path.Combine(Settings.ContentFolderPath, file);
Uri u = new Uri(pathToImage);
GifImage gif = new GifImage(u);
gif.Height = canvas1.Height; // canvas1.Height / koeffImage;
canvas1.Children.Add(gif);
Canvas.SetTop(gif, 0);
Canvas.SetLeft(gif, -999);
textBlocks.AddLast(gif);
}
catch (Exception ex)
{
HasError = true;
ErrorMessage = ex.Message;
}
}
private void AddImage(string file)
{
try
{
string pathToImage = System.IO.Path.Combine(Settings.ContentFolderPath, file);
Image image = new Image();
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri(pathToImage, UriKind.Absolute);
src.EndInit();
double ratio = src.Width / src.Height;
image.Source = src;
image.Stretch = Stretch.Uniform;
image.Height = canvas1.Height;
image.Width = canvas1.Height * ratio;
canvas1.Children.Add(image);
Canvas.SetTop(image, 0);
Canvas.SetLeft(image, -999);
textBlocks.AddLast(image);
}
catch (Exception ex)
{
HasError = true;
ErrorMessage = ex.Message;
}
}
void AddTextBlock(string Text, SolidColorBrush color)
{
try
{
double w = MeasureTextSize(Text, fontFamily, fontStyle, fontWeight, fontStretch, fontSize).Width;
TextBlock tb = new TextBlock();
tb.Text = Text;
tb.FontSize = 28;
tb.FontWeight = FontWeights.Normal;
tb.Foreground = color;
// tb.Background = Brushes.Blue;
tb.FontSize = fontSize = canvas1.Height / koeff;
tb.Width = w;
canvas1.Children.Add(tb);
Canvas.SetTop(tb, 0);
Canvas.SetLeft(tb, -9999);
textBlocks.AddLast(tb);
}
catch (Exception ex)
{
HasError = true;
ErrorMessage = ex.Message;
}
}
private void PopulateCanvas()
{
canvas1.Width=RootGrid.ActualWidth;
canvas1.Height=RootGrid.ActualHeight;
foreach(PluginContentItems中的var项)
{
if(IsImage(item.IPluginContentItemElements))
{
字符串imageName=GetImageName(item.IPluginContentItemElements);
if(IsGifImage(item.IPluginContentItemElements))
AddGIFImage(imageName);
其他的
AddImage(图像名称);
}
else//Text
{
string text=GetText(item.IPluginContentItemElements);
SolidColorBrush笔刷=GetFontColor(item.IPluginContentItemElements);
AddTextBlock(文本,画笔);
}
}
canvas1.Dispatcher.BeginInvoke(DispatcherPriority.Background,新DispatcherOperationCallback(委托)(对象状态)
{
var节点=textBlocks.First;
while(节点!=null)
{
if(node.Previous!=null)
{
if(node.Previous.Value.GetType().FullName.Contains(“TextBlock”))
Canvas.SetLeft(node.Value,Canvas.GetLeft((TextBlock)node.Previous.Value)+((TextBlock)node.Previous.Value).ActualWidth);//+gap
else if(node.Previous.Value.GetType().FullName.Contains(“GifImage”))
Canvas.SetLeft(node.Value,Canvas.GetLeft((GifImage)node.Previous.Value)+((GifImage)node.Previous.Value).ActualWidth);//+gap
else if(node.Previous.Value.GetType().FullName.Contains(“Controls.Image”))
Canvas.SetLeft(node.Value,Canvas.GetLeft((Image)node.Previous.Value)+((Image)node.Previous.Value).ActualWidth);//+gap
}
其他的
{
if(node.Value.GetType().FullName.Contains(“TextBlock”))
Canvas.SetLeft((TextBlock)node.Value,canvas1.Width);//+gap
if(node.Value.GetType().FullName.Contains(“图像”))
Canvas.SetLeft((图像)node.Value,canvas1.Width);//+gap
if(node.Value.GetType().FullName.Contains(“GifImage”))
Canvas.SetLeft((GifImage)node.Value,canvas1.Width);//+gap
}
node=node.Next;
}
返回null;
}),空);
}
无效计时器已过(对象发送器,ElapsedEventArgs e)
{
canvas1.Dispatcher.BeginInvoke(DispatcherPriority.Background,新DispatcherOperationCallback(委托)(对象状态)
{
var节点=textBlocks.First;
var lastNode=textBlocks.Last;
while(节点!=null)
{
double newLeft=Canvas.GetLeft(node.Value)-move\u amount;
双w1=0;
if(node.Value为TextBlock)
w1=((TextBlock)node.Value).ActualWidth;
if(node.Value为图像)
w1=((图像)node.Value).ActualWidth;
if(node.Value为GifImage)
w1=((GifImage)node.Value).ActualWidth;
如果(新左<(0-w1))//+间隙
{
textBlocks.Remove(节点);
var lastNodeLeftPos=Canvas.GetLeft(lastNode.Value);
textBlocks.AddLast(节点);
if(lastNode.Value.GetType().FullName.Contains(“TextBlock”))
{
//+差距
if((lastNodeLeftPos+((TextBlock)lastNode.Value.ActualWidth)>canvas1.Width)//最后一个元素在屏幕外
newLeft=lastNodeLeftPos+((TextBlock)lastNode.Value)。实际宽度;//+gap
其他的
newLeft=canvas1.宽度;//+间隙;
}
else if(lastNode.Value.GetType().FullName.Contains(“GifImage”))
{//+间隙
if((lastNodeEleftPos+((GifImage)lastNode.Value.ActualWidth)>canvas1.Width)//最后一个元素在屏幕外
newLeft=lastNodeLeftPos+((GifImage)lastNode.Value)。实际宽度;//+gap
其他的
newLeft=canvas1.宽度;//+间隙
}
else if(lastNode.Value.GetType().FullName.Contains(“Controls.Image”))
{//+间隙
if((lastNodeLeftPos+((Image)lastNode.Value.ActualWidth)>canvas1.Width)//最后一个元素在屏幕外
newLeft=lastNodeLeftPos+((图像)lastNode.Value)。实际宽度;//+gap