Xamarin.ios:要列出 .Split(",") 后跳过的代码的字符串
Xamarin.ios: string to list skipped code after .Split(",")
我正在使用 Xamarin.ios MvvmCross 构建一个 iOS 应用程序。我完成了将字符串转换为 .Split(',')
的列表。现在,当我 运行 我的代码时,我想在我的应用程序中查看列表。我第一次点击一个项目时,viewcell 将跳过 运行ning 行 string[] namesArray = FavoriteContent.ingredients.Split(',');
之后的代码。当我返回 table 视图并再次按下视单元时。它将运行代码并显示列表。
我在下面显示的代码在我的 .Core 项目的视图模型中。我在 de .IOS project in de views.
中调用它
从字符串生成列表的代码 return 它:
private string _ingredients;
public string Ingredients
{
get
{
string[] namesArray = FavoriteContent.ingredients.Split(',');
List<string> namesList = new List<string>(namesArray.Length);
namesList.AddRange(namesArray);
namesList.Reverse();
_ingredients = string.Join("\n", namesList);
return _ingredients;
}
}
这是我从 .ios 项目中的视图调用列表到视图模型中的 .Core 项目的地方
ViewController(我调用列表的第 5 行):
MvxFluentBindingDescriptionSet<DetailFavoriteView, DetailFavoriteViewModel> set = new MvxFluentBindingDescriptionSet<DetailFavoriteView, DetailFavoriteViewModel>(this);
set.Bind(NameRecipe).To(res => res.FavoriteContent.name);
set.Bind(DetailImage).For(img => img.Image).To(res => res.FavoriteContent.thumbnail).WithConversion<StringToImageConverter>();
set.Bind(DescriptionText).To(res => res.FavoriteContent.description);
set.Bind(IngredientsList).To(res => res.Ingredients);
set.Bind(ConditionText).To(res => res.Conditions);
set.Bind(ButtonShopList).To(res => res.PostShopListCommand);
set.Apply();
因为 FavoriteContent.ingredients
是 null
你会在这行 FavoriteContent.ingredients.Split(',');
中得到一个异常,它会在绑定中被捕获,因此你的 viewcell 不会显示该内容。第二次你的 FavoriteContent.ingredients
肯定不是空的,所以它可以更新视单元。
所以我认为您应该在使用 FavoriteContent.ingredients
执行逻辑之前添加一个空检查以避免异常:
private string _ingredients;
public string Ingredients
{
get
{
if (FavoriteContent?.ingredients == null) // IDK if FavoriteContent is a property or a class, I assumed is a property
return null; // or return string.empty;
string[] namesArray = FavoriteContent.ingredients.Split(',');
List<string> namesList = new List<string>(namesArray.Length);
namesList.AddRange(namesArray);
namesList.Reverse();
_ingredients = string.Join("\n", namesList);
return _ingredients;
}
}
然后当您加载 FavoriteContent.ingredients
时,您只需告诉视图使用 RaisePropertyChanged
:
刷新该绑定
// this is the part where you update FavoriteContent.ingredients in your ViewModel so that it is not null
FavoriteContent.ingredients = "my string, my other string, 2, 3";
RaisePropertyChanged(() => Ingredients); // you tell the view Ingredients has changed (make sure you are invoking this in your main thread if not use InvokeOnMainThread(() => RaisePropertyChanged(() => Ingredients));)
高
我正在使用 Xamarin.ios MvvmCross 构建一个 iOS 应用程序。我完成了将字符串转换为 .Split(',')
的列表。现在,当我 运行 我的代码时,我想在我的应用程序中查看列表。我第一次点击一个项目时,viewcell 将跳过 运行ning 行 string[] namesArray = FavoriteContent.ingredients.Split(',');
之后的代码。当我返回 table 视图并再次按下视单元时。它将运行代码并显示列表。
我在下面显示的代码在我的 .Core 项目的视图模型中。我在 de .IOS project in de views.
中调用它从字符串生成列表的代码 return 它:
private string _ingredients;
public string Ingredients
{
get
{
string[] namesArray = FavoriteContent.ingredients.Split(',');
List<string> namesList = new List<string>(namesArray.Length);
namesList.AddRange(namesArray);
namesList.Reverse();
_ingredients = string.Join("\n", namesList);
return _ingredients;
}
}
这是我从 .ios 项目中的视图调用列表到视图模型中的 .Core 项目的地方
ViewController(我调用列表的第 5 行):
MvxFluentBindingDescriptionSet<DetailFavoriteView, DetailFavoriteViewModel> set = new MvxFluentBindingDescriptionSet<DetailFavoriteView, DetailFavoriteViewModel>(this);
set.Bind(NameRecipe).To(res => res.FavoriteContent.name);
set.Bind(DetailImage).For(img => img.Image).To(res => res.FavoriteContent.thumbnail).WithConversion<StringToImageConverter>();
set.Bind(DescriptionText).To(res => res.FavoriteContent.description);
set.Bind(IngredientsList).To(res => res.Ingredients);
set.Bind(ConditionText).To(res => res.Conditions);
set.Bind(ButtonShopList).To(res => res.PostShopListCommand);
set.Apply();
因为 FavoriteContent.ingredients
是 null
你会在这行 FavoriteContent.ingredients.Split(',');
中得到一个异常,它会在绑定中被捕获,因此你的 viewcell 不会显示该内容。第二次你的 FavoriteContent.ingredients
肯定不是空的,所以它可以更新视单元。
所以我认为您应该在使用 FavoriteContent.ingredients
执行逻辑之前添加一个空检查以避免异常:
private string _ingredients;
public string Ingredients
{
get
{
if (FavoriteContent?.ingredients == null) // IDK if FavoriteContent is a property or a class, I assumed is a property
return null; // or return string.empty;
string[] namesArray = FavoriteContent.ingredients.Split(',');
List<string> namesList = new List<string>(namesArray.Length);
namesList.AddRange(namesArray);
namesList.Reverse();
_ingredients = string.Join("\n", namesList);
return _ingredients;
}
}
然后当您加载 FavoriteContent.ingredients
时,您只需告诉视图使用 RaisePropertyChanged
:
// this is the part where you update FavoriteContent.ingredients in your ViewModel so that it is not null
FavoriteContent.ingredients = "my string, my other string, 2, 3";
RaisePropertyChanged(() => Ingredients); // you tell the view Ingredients has changed (make sure you are invoking this in your main thread if not use InvokeOnMainThread(() => RaisePropertyChanged(() => Ingredients));)
高