LoginSignup
1
0

More than 1 year has passed since last update.

【tModLoader 1.4】 UIListが勝手に並び替えを滅茶苦茶にする問題の解決策

Posted at

ModListを使っていたら、配列/リストの順々に要素を追加したのに、
(ある一定の数以上の要素があると[要検証])順番をめちゃくちゃにする現象が発生した。

現象が発生したコード

だいたいこんな感じのコード

_uiList.Clear();

for (var i = 0; i < 18; i++)
{
    _items[i] = new UIListItem(i);
}

_uiList.AddRange(_items);

Recalculate();

最終的になんかいろいろ試したりしたものの

_uiList.Clear();

for (var i = 0; i < 18; i++)
{
    _items[i] = new UIListItem(i);
    _uiList.Add(_items[i]);
}

Recalculate();

結果は変わらなかった。(めちゃくちゃにされた)

原因

Terraria(tML)のコードを見たら分かった。

UIListは自動的に並び替えを行うようになっていた。

※ ちなみにRecalculate();があろうとなかろうと実行された

解決策

こちらも、Terraria(tML)のコードを見たら分かった。

UIListには、リストの並び替えのアルゴリズムを上書きできるようなメンバuiList.ManualSortMethod(※メソッドではない)があった。
Action<List<UIElement>>型。

並び替えをしてほしくなかったので

uiList.ManualSortMethod = (_ => { });

と書けばOK。

ちなみにこれを書いた場所はOnInitialize()

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0