Closed FirmianaMarsili closed 6 months ago
好的,我改一下
GenericParameters<int, int,string> example;
ViewModel = new FormattableViewModel();
BindingSet<FormattableView, FormattableViewModel> bindingSet = this.CreateBindingSet(ViewModel);
bindingSet.Bind(Example).For(v => v.Parameter1).To(vm => vm.Month);
bindingSet.Bind(Example).For(v => v.Parameter2).To(vm => vm.Day);
bindingSet.Bind(Example).For(v => v.Parameter3).To(vm => vm.Hour);
protected internal override void OnParameterChanged()
{
try
{
if (m_Text == null || !m_Text.enabled)
return;
m_Text.SetText(BUFFER.Clear().AppendFormat<P1, P2, P3>(m_Text.Format, parameter1, parameter2, Parameter3));
}
catch (Exception e)
{
#if DEBUG
if (Application.isEditor)
Debug.LogWarning(e);
#endif
m_Text.SetText(BUFFER.Clear().Append(m_Text.Format));
}
}
另外在上述示例中,当GenericParameters
存在引用类型时,在绑定Parameter1
后更新视图会因为string
为null
导致AppendFormat
失败。在我两个版本的unity,2021.3.7f1
和2022.3.14f1
中测试一个表现为长时间卡顿,一个崩溃。需要避免混用,或者可以先绑定Parameter3
GenericParameters<int, int,string> example; ViewModel = new FormattableViewModel(); BindingSet<FormattableView, FormattableViewModel> bindingSet = this.CreateBindingSet(ViewModel); bindingSet.Bind(Example).For(v => v.Parameter1).To(vm => vm.Month); bindingSet.Bind(Example).For(v => v.Parameter2).To(vm => vm.Day); bindingSet.Bind(Example).For(v => v.Parameter3).To(vm => vm.Hour); protected internal override void OnParameterChanged() { try { if (m_Text == null || !m_Text.enabled) return; m_Text.SetText(BUFFER.Clear().AppendFormat<P1, P2, P3>(m_Text.Format, parameter1, parameter2, Parameter3)); } catch (Exception e) { #if DEBUG if (Application.isEditor) Debug.LogWarning(e); #endif m_Text.SetText(BUFFER.Clear().Append(m_Text.Format)); } }
另外在上述示例中,当
GenericParameters
存在引用类型时,在绑定Parameter1
后更新视图会因为string
为null
导致AppendFormat
失败。在我两个版本的unity,2021.3.7f1
和2022.3.14f1
中测试一个表现为长时间卡顿,一个崩溃。需要避免混用,或者可以先绑定Parameter3
真是一个奇怪的bug,下面的代码,第一次点击Unity的Play运行时,正确调用 Format(ReadOnlySpan
IFormatter formatter = IFormatter.GetFormatter
使用这2个包吧,修复了
没别的办法,暂时将重载函数void Format(string format, object value, StringBuilder builder)注释掉了,避免调用紊乱
源代码:
重现步骤:
Text.text
会被Initialize
设置为Text.Format