简体   繁体   English

Xamarin.ios:在.Split(“,”)之后列出要跳过的代码的字符串

[英]Xamarin.ios: string to list skipped code after .Split(“,”)

I'm building an iOS app with Xamarin.ios MvvmCross. 我正在使用Xamarin.ios MvvmCross构建一个iOS应用。 And I Accomplished to convert the string into a list with .Split(',') . 我完成了使用.Split(',')将字符串转换为列表的.Split(',') Now when I run my code and I want to see the list in my app. 现在,当我运行代码时,我想在应用程序中查看列表。 The first time I click on an item the viewcell will skip the code after running line string[] namesArray = FavoriteContent.ingredients.Split(','); 我第一次单击某个项目时,运行行string[] namesArray = FavoriteContent.ingredients.Split(',');后,视单元将跳过代码string[] namesArray = FavoriteContent.ingredients.Split(','); . When I go back to the table view and push the viewcell again. 当我回到表格视图并再次按下视单元格时。 It will run the code and display the list. 它将运行代码并显示列表。

The code I display below is in my viewmodel in the .Core project. 我下面显示的代码在.Core项目的视图模型中。 And I call it in de .IOS project in de views. 我在de .IOS项目中用view来称呼它。

Code to make from a string a list and return it: 从字符串组成列表并将其返回的代码:

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;
        }
    }

This is where I call the list from my views in the .ios project to the .Core project in the viewmodels 在这里,我从.ios项目中的视图调用列表到viewmodels中的.Core项目。

ViewController (5th line I call the list): 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();

Because FavoriteContent.ingredients is null you'll get an exception in this line FavoriteContent.ingredients.Split(','); 因为FavoriteContent.ingredientsnull您将在此行中收到一个异常FavoriteContent.ingredients.Split(','); that will be catched in the binding and therefore your viewcell won't display that content. 将会在绑定中被捕获,因此您的视单元将不会显示该内容。 At the second time your FavoriteContent.ingredients is surely not null so it can update the viewcell. 第二次,您的FavoriteContent.ingredients当然不是null,因此它可以更新视单元。

So I think you should just add a null check before doing your logic with FavoriteContent.ingredients to avoid the exception: 因此,我认为您应该在对FavoriteContent.ingredients进行逻辑处理之前添加一个null检查,以避免出现异常:

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;
    }
}

And then when you load FavoriteContent.ingredients you just tell the view to refresh that binding using RaisePropertyChanged : 然后,当您加载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));)

HIH HIH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM