1

我正在使用 Azure 移动服务将数据存储在我的应用程序中。我的一个字段设置为 nvarchar (max) 并且在该字段中将是一长串时间。

之前,当我的数据存储在应用程序本身中时,我会将字符串格式化为:

“Header Text \n• Item \n• Item \n• Item \n Header \n• Item”

然后会跳到新的遇到的任何“\n”行。现在,当从 Azure 中提取数据并将其绑定到TextBlock时,它不会跳到新行。

我查看了TextBlock是否有 MultiLine 属性,但没有。有任何想法吗?

4

2 回答 2

0

如果将 XAML 控件中的AcceptsReturn属性设置<TextBox>为 true,它将以多行显示文本。下面的示例应用程序就是这样做的:

主页.xaml

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>
        <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="80" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Button Name="btnStart" Content="Click me"
                Click="btnStart_Click" />
        <TextBox AcceptsReturn="True" Name="txtDebug"
                 Grid.Row="1" />
    </Grid>
</Grid>

MainPage.xaml.cs

public partial class MainPage : PhoneApplicationPage
{
    public static MobileServiceClient MobileService = new MobileServiceClient(
        "https://YOUR-APP-NAME.azure-mobile.net/",
        "YOUR-APPLICATION-KEY"
    );

    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }

    private async void btnStart_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            var table = MobileService.GetTable<Test>();
            var item = new Test
            {
                name = "Header Text \n• Item \n• Item \n• Item \n Header \n• Item"
            };

            await table.InsertAsync(item);
            var inserted = await table.LookupAsync(item.id);
            this.txtDebug.Text = inserted.name;
        }
        catch (Exception ex)
        {
            this.txtDebug.Text = ex.ToString();
        }
    }
}

public class Test
{
    public int id { get; set; }
    public string name { get; set; }
}

原帖修改后更新。使用 a<TextBlock>也一样。只要设置文本块的高度超过一行(在下面的示例中,通过将其绑定到网格单元格),它将正确显示多行,而不需要任何其他属性(如AcceptsReturnon文本框)。如果将上面 XAML 文件中的控制面板网格声明替换为下面的声明,您将看到多行将显示在文本块上。

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <Grid.RowDefinitions>
            <RowDefinition Height="80" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Button Name="btnStart" Content="Click me"
                Click="btnStart_Click" />
        <TextBlock Text="This will be replaced&#10;after the call to the Mobile Service" Grid.Row="1"
                   Margin="10" Name="txtDebug"/>
        <!--<TextBox AcceptsReturn="True" Name="txtDebug"
                 Grid.Row="1" />-->
    </Grid>
于 2013-06-11T03:40:30.033 回答
0

我无法通过应用程序代码完成此操作。相反,我构建了一个从数据库加载数据的简单应用程序。加载数据后,我应用了必要的格式并保存了数据。然后它反映在客户端应用程序中。

于 2013-07-06T00:14:44.320 回答