为了限制标签的最大行数并指示文本截断,我们使用MaxLines和控件LineBreakMode的属性Label。
这在 Xamarin.Forms 中工作正常,但在 .NET MAUI 中不起作用。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui_POC.MainPage">
<ScrollView>
<StackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
<Image
Source="dotnet_bot.png"
SemanticProperties.Description="Cute dot net bot waving hi to you!"
HeightRequest="200"
HorizontalOptions="Center" />
<Label
Text="Hello, World!"
SemanticProperties.HeadingLevel="Level1"
FontSize="32"
HorizontalOptions="Center" />
<Label
Text="Welcome to .NET Multi-platform App UI, Cute dot net bot waving hi to you!"
SemanticProperties.HeadingLevel="Level2"
FontSize="18"
MaxLines="2"
LineBreakMode="TailTruncation"
HorizontalOptions="Center" />
<Button
x:Name="CounterBtn"
Text="Click me"
SemanticProperties.Hint="Counts the number of times you click"
Clicked="OnCounterClicked" …Run Code Online (Sandbox Code Playgroud) 将 Xamarin.Forms 版本更新到最新稳定版本 4.6.0.800 后,在发布模式下进行构建时出现错误:
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets(5,5): Error XALNK7000: Mono.Linker.MarkException: Error processing method: 'System.Void Plugin.LocalNotification.Platform.Droid.NotificationServiceImpl::ShowLater(Plugin.LocalNotification.NotificationRequest)' in assembly: 'Plugin.LocalNotification.dll' ---> Mono.Cecil.ResolutionException: Failed to resolve AndroidX.Work.OneTimeWorkRequest/Builder AndroidX.Work.OneTimeWorkRequest/Builder::SetInitialDelay(System.Int64,Java.Util.Concurrent.TimeUnit)
at Mono.Linker.Steps.MarkStep.HandleUnresolvedMethod (Mono.Cecil.MethodReference reference) [0x00013] in <0275dd5bdfa6470181a4d79d0790489d>:0
at Mono.Linker.Steps.MarkStep.MarkMethod (Mono.Cecil.MethodReference reference) [0x0004a] in <0275dd5bdfa6470181a4d79d0790489d>:0
at Mono.Linker.Steps.MarkStep.MarkInstruction (Mono.Cecil.Cil.Instruction instruction) [0x00040] in <0275dd5bdfa6470181a4d79d0790489d>:0
at Mono.Linker.Steps.MarkStep.MarkMethodBody (Mono.Cecil.Cil.MethodBody body) [0x000c2] in <0275dd5bdfa6470181a4d79d0790489d>:0
at Mono.Linker.Steps.MarkStep.ProcessMethod (Mono.Cecil.MethodDefinition method) [0x00186] in <0275dd5bdfa6470181a4d79d0790489d>:0
at Mono.Linker.Steps.MarkStep.ProcessQueue () [0x0001b] in <0275dd5bdfa6470181a4d79d0790489d>:0
--- End of inner exception stack trace ---
at Mono.Linker.Steps.MarkStep.ProcessQueue () [0x00047] in <0275dd5bdfa6470181a4d79d0790489d>:0
at …Run Code Online (Sandbox Code Playgroud) 在我当前的 Xamarin.Forms 项目中,我为不同的需求创建了许多渲染器。现在 9 月毛伊岛即将到来,我们必须将我们的项目迁移到那里。那么渲染器会发生什么?它会按原样工作还是我们不需要渲染器?
考虑我们有课程:
public class Notes
{
public string Id { get; set; }
public string Title { get; set; }
public string Text { get; set; }
}
public class Configuration
{
public List<Notes> ImportantNotes { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后为了处理可能会出现 null 的情况ImportantNotes,我们将执行以下操作:
if (config.ImportantNotes != null)
{
foreach(var note in config.ImportantNotes)
{
....
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经在代码中多次使用了这种模式,但最近我开始了解 Linq 的 Enumerable.Empty() 方法。我可以这样使用:
foreach(var note in config.ImportantNotes ?? Enumerable.Empty<ImportantNotes>())
{
....
}
Run Code Online (Sandbox Code Playgroud)
我已经检查过,它在实际代码中确实运行良好,但我不确定它是否好用。那么,使用它来处理空检查是否更好?如果不是那么为什么不呢?