StateManger - remove 'AlwaysTransitionOnLoaded' hack, replace with real fix
This commit is contained in:
1 parent
49de685cd4
commit
e8adb8fc68
2 files changed
+26
-32
No files matched your search
@@ -50,7 +50,7 @@
|
||||
>
|
||||
<Grid>
|
||||
<u:StateManager.Groups>
|
||||
<u:StateGroupsManager AttachToMembers="True" AlwaysTransitionOnLoaded="True">
|
||||
<u:StateGroupsManager AttachToMembers="True">
|
||||
<u:StateDefinitionCollection>
|
||||
<u:StateDefinition State="{Binding Path='(u:StateDefinition.Group).(u:StateGroupsManager.Target).(Grid.TemplatedParent).(uag:QuantityMeasureControl.QuantityCase)', RelativeSource={RelativeSource Self}, Converter={StaticResource QuantityCaseVisualStateConverter}}"/>
|
||||
</u:StateDefinitionCollection>
|
||||
@@ -111,7 +111,7 @@
|
||||
>
|
||||
<Grid>
|
||||
<u:StateManager.Groups>
|
||||
<u:StateGroupsManager AttachToMembers="True" AlwaysTransitionOnLoaded="True">
|
||||
<u:StateGroupsManager AttachToMembers="True">
|
||||
<u:StateDefinitionCollection>
|
||||
<u:StateDefinition State="{Binding Path='(u:StateDefinition.Group).(u:StateGroupsManager.Target).(Grid.TemplatedParent).(uag:QuantityMeasureControl.QuantityCase)', RelativeSource={RelativeSource Self}, Converter={StaticResource QuantityCaseVisualStateConverter}}"/>
|
||||
</u:StateDefinitionCollection>
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using bts;
|
||||
using mbts.utils;
|
||||
using mbts.ui.extensions;
|
||||
using mbts.ui.shell;
|
||||
using mbts.ui.utility;
|
||||
@@ -123,42 +122,37 @@ namespace mbts.ui {
|
||||
/// If these properties are assigned after <see cref="Target"/>, they will bear no effect.
|
||||
/// If this causes problems, <see cref="_reset(StateGroupsManager, FrameworkElement, StateDefinitionCollection)"/> should be called inside the setters.
|
||||
public bool AttachToMembers { get; set; }
|
||||
public bool AlwaysTransitionOnLoaded { get; set; }
|
||||
|
||||
static void _reset(StateGroupsManager state_group, FrameworkElement target, StateDefinitionCollection state_defs) {
|
||||
if (state_group == null)
|
||||
return;
|
||||
Disposable.dispose(ref state_group._state_definitions_hook);
|
||||
if (state_defs != null) {
|
||||
state_group._state_definitions_hook = ObservableAutoWrapper.connect_item_hooks(state_defs, sd => {
|
||||
StateManager.GoToState(target, sd.State);
|
||||
sd.StateChanged += on_state_changed;
|
||||
return Disposable.Create(() => sd.StateChanged -= on_state_changed);
|
||||
});
|
||||
if (state_group.AttachToMembers) {
|
||||
state_group._state_definitions_hook = new CompositeDisposable(
|
||||
state_group._state_definitions_hook,
|
||||
ObservableAutoWrapper.connect_item_actions(
|
||||
state_defs,
|
||||
sd => sd.SetCurrentValue(StateDefinition.GroupProperty, state_group)));
|
||||
}
|
||||
if (state_group.AlwaysTransitionOnLoaded && target != null) {
|
||||
target.Loaded += on_loaded;
|
||||
state_group._state_definitions_hook = new CompositeDisposable(state_group._state_definitions_hook) {
|
||||
unhook
|
||||
};
|
||||
async void on_loaded(object sender, RoutedEventArgs e) {
|
||||
// The transitions don't always stick if we switch to them immediately; doing it later the dispatcher queue seems to do the trick.
|
||||
await target.Dispatcher.InvokeAsync(transition);
|
||||
void transition() {
|
||||
foreach (var sd in state_defs)
|
||||
StateManager.GoToState(target, sd.State);
|
||||
}
|
||||
if (target == null || state_defs == null)
|
||||
return;
|
||||
state_group._state_definitions_hook = ObservableAutoWrapper.connect_item_hooks(state_defs, sd => {
|
||||
int version = int.MinValue;
|
||||
transition(sd.State);
|
||||
sd.StateChanged += on_state_changed;
|
||||
return Disposable.Create(() => sd.StateChanged -= on_state_changed);
|
||||
void on_state_changed(string oldval, string newval) => transition(newval);
|
||||
async void transition(string state) {
|
||||
version++;
|
||||
if (target.IsLoaded)
|
||||
StateManager.GoToState(target, state);
|
||||
else {
|
||||
int v = version;
|
||||
await target.LoadedAsync();
|
||||
if (v == version)
|
||||
StateManager.GoToState(target, state);
|
||||
}
|
||||
void unhook() =>
|
||||
target.Loaded -= on_loaded;
|
||||
}
|
||||
void on_state_changed(string oldval, string newval) => StateManager.GoToState(target, newval);
|
||||
});
|
||||
if (state_group.AttachToMembers) {
|
||||
state_group._state_definitions_hook = new CompositeDisposable(
|
||||
state_group._state_definitions_hook,
|
||||
ObservableAutoWrapper.connect_item_actions(
|
||||
state_defs,
|
||||
sd => sd.SetCurrentValue(StateDefinition.GroupProperty, state_group)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user