Вам просто нужно добавить оператор объединения для защиты от нулевых значений.
private TodoItem _todoItem;
public TodoItem TodoItem
{
get => _todoItem;
set
{
_todoItem = value;
Title = value?.name ?? "";
}
}
Если вы хотите изменить свой подход на более декларативный, вы можете использовать конвертер.
public class PrefixConverter : IValueConverter
{
public string Prefix { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Prefix + value?.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Итак, вы можете использовать в своем XAML
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:conv="clr-namespace:Sample.Converters"
x:Class="Sample.MainPage"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
Title="{Binding TodoItem.name,Converter={StaticResource PrefixConverter}}">
<ContentPage.Resources>
<ResourceDictionary>
<conv:PrefixConverter x:Key="PrefixConverter" Prefix="Test"/>
</ResourceDictionary>
</ContentPage.Resources>