Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使标签文本自动滚动?_C#_Xamarin.forms_Marquee - Fatal编程技术网

C# 如何使标签文本自动滚动?

C# 如何使标签文本自动滚动?,c#,xamarin.forms,marquee,C#,Xamarin.forms,Marquee,我有一个按钮,我想在一些文字,但有些文字可能太长,无法很好地适应按钮。我想让文本像HTML中的字幕一样水平滚动一行。我可以让它在一行中滚动,但是,测试文本在按钮的边缘被切断,那里的文本实际上会从按钮上移开,而不是在按钮的边缘消失 我在谷歌上搜索我的问题的答案,几个小时后,我想是时候问我的问题了 <Grid HeightRequest="400" Grid.Column="0" Grid.Row="0" > <Grid.RowDefinitions>

我有一个按钮,我想在一些文字,但有些文字可能太长,无法很好地适应按钮。我想让文本像HTML中的字幕一样水平滚动一行。我可以让它在一行中滚动,但是,测试文本在按钮的边缘被切断,那里的文本实际上会从按钮上移开,而不是在按钮的边缘消失

我在谷歌上搜索我的问题的答案,几个小时后,我想是时候问我的问题了

<Grid HeightRequest="400" Grid.Column="0" Grid.Row="0" >
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <Button BackgroundColor="#006633" Opacity="0.7" Grid.RowSpan="3" Grid.ColumnSpan="1">

    </Button>
    <Label x:Name="Label1" StyleClass="button" Grid.Row="1" Grid.Column="0" >

    </Label>
</Grid>

public void Marque1()
{
    Label1.Text = "This is to simulate a really long sentence for testing purposes";
    Label1.HorizontalOptions = LayoutOptions.Start;
    Label1.VerticalTextAlignment = TextAlignment.Center;
    Label1.LineBreakMode = LineBreakMode.NoWrap;

    Label1.TranslateTo(-50, 0, 8000, Easing.Linear);
}

公共无效标记1()
{
Label1.Text=“这是为了模拟一个非常长的句子进行测试”;
Label1.HorizontalOptions=布局选项.Start;
Label1.VerticalTextAlignment=TextAlignment.Center;
Label1.LineBreakMode=LineBreakMode.NoWrap;
标签1。平移到(-50,0,8000,1。线性);
}

我希望整个文本从右向左移动并重复,而不是离开按钮的边界。

您可以检查这一点,这是您需要的效果:

public partial class MaqueText : ContentPage
{
    private bool Execute { get; set; }
    public MaqueText ()
    {
        InitializeComponent ();
        Label1.Text = "This is to simulate a really long sentence for testing purposes";
        Label1.HorizontalOptions = LayoutOptions.Start;
        Label1.VerticalTextAlignment = TextAlignment.Center;
        Label1.LineBreakMode = LineBreakMode.NoWrap;
    }
    protected override void OnAppearing()
    {
        base.OnAppearing();
        Execute = true;

        Device.StartTimer(TimeSpan.FromMilliseconds(50), () =>
        {
            Label1.TranslationX -= 5f;

            if (Math.Abs(Label1.TranslationX) > Width)
            {
                Label1.TranslationX = Label1.Width;
            }

            return Execute;
        });
    }
    protected override void OnDisappearing()
    {
        base.OnDisappearing();

        Execute = false;
    }
}

这并没有达到一个品牌实际上是有用的-显示文本,否则会溢出控制。由于LineBreakMode设置为NoWrap,因此将永远不会显示溢出的内容。减少TranslationX只会移动最初显示的任何文本,而不会呈现初始溢出文本。这实现了一种品牌效果,而不会显示超出控件边界允许的更多内容。