如果将 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>
也一样。只要设置文本块的高度超过一行(在下面的示例中,通过将其绑定到网格单元格),它将正确显示多行,而不需要任何其他属性(如AcceptsReturn
on文本框)。如果将上面 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 after the call to the Mobile Service" Grid.Row="1"
Margin="10" Name="txtDebug"/>
<!--<TextBox AcceptsReturn="True" Name="txtDebug"
Grid.Row="1" />-->
</Grid>